Programster's Blog

Tutorials focusing on Linux, programming, and open source

Git - Move Recent Commits To A Branch

Today I will show you how to perform the following change to your git repository:

5VvRkqMoFP2avG4FiCZ57KRndl9ma6v6YWcfiRJjjZEuQk/S+/WLAYxy1Y6JGnu2u6pLLgJyDvcA91ZPyHp/+l3Q1903HrJkgqfhaUKeJxjP8UL9zQzv2uARTxsiEYfahC6Gl/hfZoxTY32LQ3YovSg5T2T8WjYGPE1ZIEs2KgQ/ll/b8qQ86iuNGDC8BDSB1r/jUO60dYH9i/0PFkc7OzLyl7pmQ4MfkeBvqRlvgsn2/KOr99T2ZSZ62NGQHwsm8mVC1oJzqZ/2pzVLMmgtbLrd15ra/LsFS+U1DbBu8JMmb2bqT+bD5LsFQ7VQuKvC6riLJXt5pUFWc1TMK9tO7hNVQupxGyfJmidcnNuRkLLFNlD2gxT8ByvU+MGCbbZZjZ1+1tx8ChOSnWqng3KQ1NpjfM+keFev2Aa+wdWsOzI16+54YZGYV3YFAq2NmnUT5T1fsFMPBr5qKAmAcnU7lCVgHFy3XvZbiev5pxsoc0wMlHg2HJQzAOX6M0NJpo+D0gNQQiTT8CnTTFUKEno4xEEZQDVL8f5dFaa28E9W+C3DjZ1i+d2+p54vNXoYFjpCe+BvImAl8ZFURMxOubXHLx1srQIUsPUqsPUasDWD/cVj9Q312mJZtF3oqZlWRc11Osp3OfvJS6cjDQjo6Ex1jsBV7PsjZp9A9metRcr1rHlf7NeO1Jr92Qcddcf+HLD//Kll1H+cjC5G7Egz6Ejz1thiV0Yhtt04Elk4I3k3yihYDn5vMrrsl/0ajouE4raEOijn/HbOZ91A9+6KxF0X3dFpRyrw+Y0eJBOAVSVW0pHBksKlPGWOHBoTTeIozRaD4kt1TFaZ9MXqhvlkKvZxGCZ1utuBWiJ3w1pCj55VrACXv1vUEiEA8J/sqAwrQdNg90vB7G7nM+hoi75ghrf3CfaTM5TZUyTPc9SWjTU8bc8rXVvVEBv3TWUrNv91uMoPDI9wCRgeANBmgaNsqpGgYazm5OBXPoHxVL6YpqgWGxMfoxs7xPTOfSRXleKZC1VgNvc6wAzGAcYenSJtwBzw/IpgHGDs0almKJH3OCzhrXrs4almLDF+HJbwaH3HHfVB8D3OrTE8aH3Kk+wHALs3g8F2bbtJD3zzuwYbm1lbAjDuvNc5Hbe9xznO4HbT3S2uIuEy9tNB847mxDPQkDoCT1pjPx40KwZ5HJYIhl071YzWsUIAm5OFqQjE6/UA4b1SZQRLqIx/lj/j9kwL7irTgvrLtFQk2sZ+KGz0IDdnOagHjS3a/pEHVUTg9Xq4OnGFBktcoa4SV6CjDp1pzHnLer28J2+NestbuxGcW8UUOXtqj1o6th30OmdvRb9zp+nN/esGas2+39xPh+xD9X++fScdRaZ60KM8TG6Ne/P0Kvxp0QwvdrVzqGQ16ipZjfpLVvedrb5PPWvZbpfIKaPZH//O4elW+t1NuD/2CXT/TxkRbN493TPtgCFBAkOunbrXtSo6YESw5iB5a4QQBLr6ixESXEHWhxnyFdtywf6fKXJcTpFX5Xs78ixVvPyfg+b28r8k5Mt/">
A
A
B
B
C
C
D
D
Master
Master
New Branch
New Branch

After

[Not supported by viewer]
A
A
B
B
C
C
D
D
Master
Master

Before

Before

You may need to do this if you created a new branch, but forgot to switch to it before subsequently making your commits to master instead.

If You Haven't Yet Pushed

Run the following commands

# Create the branch you want the changes in.
git branch myNewBranch

# Move master back by 3 commits
git reset --hard HEAD~3

# Switch to this new branch 
git checkout myNewBranch

If Already Pushed

# Create the branch you want the changes in.
git branch myNewBranch
git push -u origin myNewBranch

# Move master back by 3 commits
git reset --hard HEAD~3

# Force to push these steps to the remote server 
# (and apologize to all the other developers in your team).
git push --force origin

# Switch to this new branch 
git checkout myNewBranch

Extra Steps For Gitlab Users

If you are using Github, the previous command should have succeeded and your all done. In my case, because I am resetting master on a Gitlab server, this was rejected with the following message:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitlab.xxx.org:Page/package-xxx-xx.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

This was easy enough to fix. Master branches are marked as "protected" by default. Simply unprotect them before running your force push again and then re-protect the branch.

References