Friday, February 19, 2016

Off topic, but my favorite GIT tips

You're not a real programmer until you've had a nervous breakdown by accidentally erasing your files with git,
or at least had a nervous breakdown from some other git-related reason.
But perhaps there is no point in 5 or more git-triggered nervous breakdowns.
My favorite tips, so I won't forget:

add all and commit in same line:
git commit -am "bla"

Note: Something I've understood while writing this. What is the "all" in "add all"?
All files in the directory? No! It's all files that are currently *tracked*.
As I understand, this means files you have added manually, i.e. used
git add filename
at some point in the past.
You can stop a file from being tracked by
git rm --cached filename
(If you don't use --cached file will actually be erased!)

change a commit name after you've called it ``fuck git'':

git commit --ammend -m "hug git"


Save before pulling! Otherwise this could happen:
You pull, before you've saved a file.
When you go back to the editor it asks
you ``The file has changed, do you want to save this one?"
Instinctively, you say yes..
Now git thinks your old file, contains newer updates than
what you've just pulled!
If you retry to merge, it will just give you your old file, cause it think it's newer..
actually wonder if someone had solution for this?


You've done a lot of nonsense, and just want to go back to state at last commit without saving anything:

git reset --hard.

You've done a lot of nonsense, commited it. You want to go back to the state you were in 2 commits ago, but want to save what you have cause some of this nonsense might be useful someday:

git branch wip
git reset --hard HEAD~2

To prove the point of this post - I did accidentally erase some work while trying the above lines:
After I created the branch wip, I assumed it had all my latest work. - but I did not *commit* before running the first line. So branch wip had my latest *comitted* work.
And when I ran the second line, my newest changes after my latest commit where erased for good as far as I know. arghhh    gotta be careful with that --hard flag
In this context
Save before any git operation!! or weird shit will happen!

You want to see what's different now compared to j commits ago (This *includes changes you've made you haven't committed to)

git diff HEAD~j

You want to see what's different in the latest commit, compared to 4 commits ago:

git diff HEAD HEAD~4

You just want to see  what's changes since you've last commited

git diff

You want to temporarily go back to the situation you were in 3 commits ago, without actually changing anything about the branches

git checkout HEAD~3

Stuff I've just learned: You want to go back to a commit, but without deleting the history, rather by adding a new event in the history that you have gone back:

Say you had commits titled


You've come to the conclusion you prefer to fuck git, but don't want to erase the memory of a nicer time. So you use revert as follows:



Now you feel like you just want to erase all the emotions you went through,
and leave only that first moment, so you use reset as follows

Warning: If you try to use git revert on complex projects. where there have been merges, weird shit happens that I am working on understanding





No comments: