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

What to do when you commit to the wrong branch?

ThePokyWizard July 11, 2022

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
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 13, 2022

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

ThePokyWizard July 13, 2022

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.

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 13, 2022

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
AUG Leaders

Atlassian Community Events