Git hygiene

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:

  1. All the tutorial teach you to git pull to get the updates, but this is terrible because if you have changes locally, it generates a merge. points out the correct thing to do is to git fetch origin --prune  which 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
  2. 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!
  3. I’ve accidentally been in master and then I’m screwed. Instead, you should certainly turn on protected branch in which prevents things like push -f origin:master
  4. And finally, explains you can use a .git/hook/pre-push script which can read what you are trying to push and ask you if you really want to do this.

I’m Rich & Co.

Welcome to Tongfamily, our cozy corner of the internet dedicated to all things technology and interesting. Here, we invite you to join us on a journey of tips, tricks, and traps. Let’s get geeky!

Let’s connect