Git
https://git-scm.com/
Notes
When you run the
git init
command, Git creates a.git
directory in the directory from where the command was launched. Everything Git needs to work is in there.When you run the
git add
command, Git writes files to the staging area of the repository. When you rungit commit
, Git creates a commit object (with an ID), and it applies references (= labels) to that commit (HEAD
and the branch you are on).When you run the
git branch
command, Git just created a new reference with the name of the newly created branch, and applies it to theHEAD
commit.When you run the
git checkout
command, Git switches the branch it's going to advance when you commit.An ID in Git is a checksum of the content of the commit, the author, the date, the log message, and the previous commit ID. Every ID is unique, so every commit is unique (as changing even slightly just one of the variables above will generate a completely different checksum).
When you run the
git merge <branch>
command, two situations are possible:Git recognises the current branch is an ancestor of the branch to be merged: in this case, Git will perform the merge by fast-forwarding, i.e. it will just update references;
Git recognises the current branch is not an ancestor of the branch to be merged: in this case, Git will create a new commit (with two parents), and will move the
master
and theHEAD
references (but not the branch one).
Thanks to the fact that every ID is unique, every commit is unique, commits do not change, and IDs depend on previous commits, Git is able to retrieve the full history of a commit just given its ID. This is very useful when you run the
git push
command, as it will send to origin only the minimum set of commits to make the two repositories the same.When you run
git tag
, Git will create a new reference and attach it to a commit. The main difference of a tag is that it never moves after being created.The reflog is a summary of the latest commits Git has interacted with. It can be useful in rescue situations when some of the commits have been done from a
detached HEAD
situation, meaning there was no branch associated with them.
Useful commands
Add a new remote repository
git remote add <remote_name> <remote_url>
Create a fixup commit
git commit --fixup <hash>
Rename current branch
git branch -m <new_name>
Resources
Alternatives
Sapling - Meta
Articles
A look under the hood: how branches work in Git - Tobias Günther
Better Git Conflicts with zdiff3 - Michael Gattozzi
Commits are snapshots, not diffs - Derrick Stolee
Confusing git terminology - Julia Evans
git - the simple guide - no deep shit! - Roger Dudler
git branches: intuition & reality - Julia Evans
Git's database internals - Derrick Stolee
How HEAD works in git - Julia Evans
In a git repository, where do your files live? - Julia Evans
Inside .git - Julia Evans
Modern Git Commands and Features You Should Be Using - Martin Heinz
Picturing Git: Conceptions and Misconceptions - Matt Neuburg
Popular git config options - Julia Evans
The "current branch" in git - Julia Evans
What is in that .git directory? - Abin Simon
When to Use Each of the Git Diff Algorithms - Lup Peng
Books
Git from the Bottom Up - John Wiegley
Pro Git - Scott Chacon, Ben Straub
Images
Tools
Videos
Git For Ages 4 And Up - Michael Schwern
So You Think You Know Git - Scott Chacon
So You Think You Know Git Part 2 - Scott Chacon
Websites
Last updated