Well I’ve been using git for (three?) years now and this last week I made three horrible mistakes by accidentally pushing to master and also merging when I didn’t mean to. This happens much more often when I use multiple machines at home and at work.
So here’s to better git hygiene for all:
- All the tutorial teach you to
git pullto get the updates, but this is terrible because if you have changes locally, it generates a merge. adamcod.es points out the correct thing to do is to
git fetch origin --prunewhich gets the changes from your remote repo and run
git merge --ff-only origin/<your-branch>your branch. This says only merge the local changes in if it is a pure fast forward
- If you get an error then you do a
git rebase --preserve-merges origin/<your_branch>.This last command is a miracle, it basically rewinds the origin and your local branch to a common point then fast forwards all the remote changes and then applies your local changes one by one!
- I’ve accidentally been in master and then I’m screwed. Instead, you should certainly turn on protected branch in github.com which prevents things like push -f origin:master
- And finally, ghost.org explains you can use a
.git/hook/pre-pushscript which can read what you are trying to push and ask you if you really want to do this.