You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
In our company we work with branches within the feature for new features and hotfix for bug fixes (gitflow).
It turns out that a few days ago an developer forgot to create a branch on a feature and committed locally to master, but didn't push it to Bitbucket Cloud. And to make matters worse, there were new commits on the master branch at the source, which the developer had not yet pulled.
Luckily for us, he made few changes. In these cases what should be done in these situations, to push his code to the correct branch (eg.: feature/new-super-feature) without losing any changes. ?
If the changes haven't been pushed to Bitbucket Cloud, and if you want to move e.g. the last 3 commits from master (locally) to a new branch, then you can do the following:
git checkout master
git checkout -b feature-branch HEAD~3
where feature-branch replace with the name of your branch
The 3 in HEAD~3 is the number of commits you want to move starting from the HEAD
The second command will create a new branch 3 commits before the HEAD, omitting them for now
git branch --set-upstream-to=master
git cherry-pick ..master
The second command will cherry-pick the last 3 commits from master and apply them to feature-branch
git branch --force master master~3
This command will discard the last 3 commits from master (you already moved these to feature-branch)
You can then push feature-branch to Bitbucket Cloud with
git push origin feature-branch
You may also want to consider setting up branch permissions for master branch and prevent all or some users from pushing directly to master and allowing only merges via pull request. You can check the following documentation if this is something you'd be interested in:
This is correct, branch permissions only apply to the Bitbucket Cloud repo, not to the users' local repos
(I don't think it's possible to prevent users from writing on a certain branch of their local clone).
With branch permissions, if a user makes such a mistake, they won't be able to push to master on the Bitbucket Cloud repo (if they don't have permissions to do so), they will just need to fix the issue in their local repo.
Otherwise, if they accidentally push to master on the Bitbucket Cloud repo, you will need to fix the issue for the remote repo, and other users who have pulled these changes will need to fix their local repos as well.