GIT Rebase on Remote branchesEver finished up work on some code, commit it with git only to realize that you forgot to create you feature branch before you started?
If this has happened to you at some point you know that all your work is now on your local master brache while work on the remote master has continued at steady pace, with commits from other team members stacking up. You can not push because git wont let you. And if you pull you will end up with a fat ugly merge commit that is pretty far from the nice rebase your were aiming at that would make the commit history a joy to read.
What you want
Your usual modus operandi looks something like this:
1. Get to work in the morning, get current and branch off before starting today's tasks:
git checkout master
git checkout -b work
(Throughout the text I will continuously checkout branches instead of writing which branch I am on.)
2. Finish up your nice coding work and then:
git commit -a -m "Finished cool feature"3. Now, before pushing, you want to get current again and put your own commits "on top" of any other ones that were pushed while you where busy finishing up your feature. So:
git checkout master4. And now that your commits will not cause a merge commit but instead can be fast forwarded, it is time to share with rest of the team:
git checkout work
git rebase master
git checkout master5. Go for coffee.
git merge work
What you haveBut right now all that is Utopia. You forgot all about creating that work branch and all your nice code is committed on (what by now is) your outdated local master branch.
Your world looks like this:
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'some-repo'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
How to fix itThere is still a chance to avoid ridicule if you remember that there is very little difference between local and remote branches it git. You ought to be able to rebase your local master on its remote counter part. Here is how:
1. If you are the slightest bit uncomfortable with the operation. Back up your repository!
It is dirt cheap and very simple.
cp -a your-repo-dir your-repo-dir.bak2. Download the objects and refs from the remote repo
git checkout master3. Now that your index is in sync with the remote repository's you can rebase master on origin/master.
git checkout master4. After resolving any conflicts in the same way as you would normally to your ready to push.
git rebase origin/master
git push5. Go for coffee and forget all about this little mishap!
(There is also a short hand combining steps 3 and 4:
git pull --rebase.)
NotesI might be helpful to know that when you do a
git pullit is actually a shortcut for doing a
git fetchfollowed by a
git branchwill list your local branches dy default. Use
git branch -rto list remote branches or
git branch -ato list all branches.