Git commands to know just after you get the basics
codersbible.com
codersbible.com
git rev-parse branch-name: gets the hash of a branch name, useful for verifying two branches are at the same place (so "git rev-parse HEAD" gets the current hash)
The ^ and ~ notations for navigating commits: my_branch^ points to the commit 1 before my_branch, and can be repeated multiple times, ie. my_branch^^^ to get the commit 3 from the top. Likewise the tilda notation allows one to jump a fixed number of commits up: my_branch~10 is the commit 10 above my_branch.
git rebase -i HEAD~10: interactively rebase the last 10 commits. Easy way to re-order, squash, drop, and/or edit the commit messages of recent history in a single git command.
The difference is in their numerical notion. ~N means go to my Nth first parent. Where ^N means my Nth parent, which is really only valuable/useful on merge commits (Or more specifically octopus merges that merge more than two commits)
This is why ~ and ^ in the single form mean the same thing.
So, in one mental model of the DAG you can think of ^ as horizontal navigation (Right) and ~ as vertical (Down)
I believe the same thing can be done by running git log --oneline -n1 branch-name (though you will also get the commit message title in addition to the sha).
> my_branch^ points to the commit 1 before my_branch, and can be repeated multiple times, ie. my_branch^^^ to get the commit 3 from the top.
The ^ notation only references the first parent of the commit. You would have to do something like commitish^2 to reference the second parent (though this would only be an issue for merge commits or any commit that has more than one parent).
Rebasing cleans up my commits so much. It is nice being able to have a chain of nice, logical commits even in the middle of a hairy refactor.
git diff branch1 branch2
git show displays any additional branches that are the same. $ git show -q
commit 2ee0fd3e2717a352b6ded617184be280cb55e78c (HEAD -> master, foo)
...git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
'git for-each-ref --sort=-committerdate'
Helps me keep my head around what branches are what ..Flow might be something like:
git checkout master
git checkout -b my_branch
git push origin my_branch
...
git fetch
git log -p origin/my_branch
git reset --hard origin/my_branchKnow reflog and you stop being afraid of losing your work if you mess up, know reflog and reset and you stop being afraid of not being able to fix things if you mess up.
git diff # see diff
git diff master..develop # see diff between branches
git show <commit> # see changes in specific commit
git log -p <file> # see log for specific file with diff
git checkout - # checkout in previous branchHow is that better?
"Hmm, how to start from scratch? I guess I could save a few files, delete the workspace with good old trusty rm -rf, clone again, and restore those few files."
If you can't see the difference between those two...
Yes, the Git power user will know how to "start from scratch" without really starting from scratch and without losing extant work they'd rather save. And beginner users can do the second of the above. But teach a beginner to use git reset --hard and they'll hate Git and they'll hate you.