Download - Shawn's amazing git tutorial
Git is composed of “commits”. When you “commit” code you are making a copy of the code that you can
retrieve anytime no matter how old it is or how big or complicated the history gets.
A git commit is identified by a long random-looking string of characters called the “SHA ID”. It looks like this:
a116bec2f74dbf3d0c5cf8322837043221b1b0d5
Each commit also has a comment associated with it that you write.
Commits are sequential, but can be reordered with a little work. (I won’t cover that. If you need to reorder the
commits let me know and I’ll help)
A
B
C
This is how I will represent commits. Each circle is a revision and you can
retrieve any of these three you want, change it, branch from it, move it
around in the history, etc.
Each of these commits would have its own unique SHA ID and associated
comment.
Oldest
Newest
A
B
C
S
work
In addition to commits, git also has a “staging” area.
When you make changes to your code it is just lingering in the
“working area” until you use this command:
git add -A .
to add it to the “staging area”.
The “staging area” is temporary and cannot be shared with other
people. It is meant for you to keep ‘tweeking’ the changes before
you’re ready to use this command:
git commit -m “[comment]”
to create a “commit” from the “staging area”.
You can always see the state of the “working area” and the “staging area”
with this command:
git status
This command would not show any info about commits.
You can see the state of the commits using this command:
git log -5
Change the number ‘5’ to see more or fewer commits.
A
B
C
S
work
}
}
A
B
C HEADGit also keeps track of which commit you are working from with a pointer
called the HEAD (all caps).
If you want to work from a different commit you use this command to move
the HEAD:
git reset --hard [B-SHA ID]
In this case perhaps you want to move the head to commit-B. You would
issue a reset with the SHA ID for commit B.
A
B
C
HEAD
A
B
But if you make changes now you risk losing
commit C unless you write down the SHA ID for
it.
workC
?HEAD
A
B
C HEAD In addition to the HEAD pointer Git also has branches. You are always on a
branch. The default branch is named master.
Here HEAD and master are pointing to the same commit.
You can create a new branch with this command:
git checkout -b NewBranch
Now HEAD pointer and both branches point to the C commit.
Then you can make changes and commit them to the new branch:
git add -A .
git commit -m “Branch on the new branch.”
master
A
B
C HEAD
master
NewBranch
A
B
C HEADmaster
NewBranch D
At some point you’ll want to “merge” your new branch in with the master or
another branch. You use this command to do that:
git checkout [destination branch]
git merge [source branch]
A
B
C
HEAD
Dest
Source
D
E
F
X
Y
A
B
C
HEADDest
Source
D
E
F
X
Y
A
B
C
HEADDest
Source
D
E
F
X
Y