Some of my git knowledge and where it came from.
Before starting to use git one should take the few minutes to set it up.
Set up your personal information that will end up on your commits:
Set the default branch name to main instead of master The reasons I do this are because there was some controversy around the word "master" in this particular context and and I quickly realised that "main" is way easier to type.
You could set your editor for git commit messages (and other git features) using:
If you (like me) have one editor you want to use for everything: Set the
EDITOR variable in your
~/.profile and git along with a whole range of other programs will pick up on that.
Situation: You have made changes to one file but they should be in two different commits.
git will then chop up the changes into hunks and ask interactively if you want to add/stage (
y) a change, ignore/not stage it (
n), further subdivide (
s) or even edit (
e) it. You can use
? to get summary of all options currently available.
Undo last Commit
Situation: You just made a git commit but you committed too much by accident.
Note: If you just messed up the commit message or need to add changes use
git commit --amend.
git reset is your friend here, without any arguments it helps when you
git added too much, it makes git go into the state it was in at the last commit.
HEAD^1 resets git to the parent commit of the last commit. Effectively acting as if the last commit never happened. (You can still recover it using the revlog)
git reset will only change your git history, not the files you have currently checked out so when using it as described above you won't loose any changes.
git reset --hard is intended to throw changes away and it is possible to loose uncommitted data when using it with the
--hard flag enabled.
Restore File from last Commit
Situation: You messed up a file and want to throw away the changes since the last commit.
This is less destructive than
git reset --hard which throws away all your changes in a repository since the last commit.
Other Peoples Git Guides
Disclaimer: I'm (at the time of writing) not affiliated with any of the parties providing the guides linked below or elsewhere on this page. I just think they are useful.
In My unorthodox, branchless git workflow Drew DeVault explains how to make use of the fact that the git history of a repository isn't set in stone, I mainly use these tricks to completely avoid a lot of merge conflicts in my personal projects.
Learn to change history with git rebase! is Drew DeVault's cheatsheet for altering git history in general and the rebase based workflow in the blogpost liked above.
Ein kurzer Blick in .git/ is a talk held at DiVOC in 2020 by Florian Hars diving into the
.git directory, unfortunately no one has translated it to English yet.