Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,464,130
Community Members
 
Community Events
176
Community Groups

What to do when you commit to the wrong branch?

Edited

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. ?

 

 

1 answer

1 accepted

0 votes
Answer accepted

Hi @ThePokyWizard,

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:

Kind regards,
Theodora

Hi @Theodora Boudale,

 

In Cloud I already configured to prohibit modifications in Master. But that only applies at the origin, correct? Locally they can still write to Master.

Hi @ThePokyWizard,

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.

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS

Atlassian Community Events