Notes on Pro Git written by Scott Chacon
and Ben Straub
Qianqian Shan
Contents
1 Getting Started 2
2 Git Basics 2
2.1 Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Commands with Remotes . . . . . . . . . . . . . . . . . . . . 3
2.3 Tagging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Branching 4
1
1 Getting Started
Git thinks about its data more like a set of snapshots of a miniature file
system. The mechanism that Git uses for checksumming is SHA-1 hash, a 40-
character string composed of hexadecimal characters. Git stores everything
in its database not by filename but by the hash value of its contents.
Three States files can reside in:
1. modified : changed the file but not committed yet.
2. staged: have marked a modified file in its current version to go into
commit.
3. committed:data is safely stored in your local database.
Check the settings by git config –list.
Getting help by git help [verb] or git [verb] –help or man git-[verb].
2 Git Basics
2.1 Basic Commands
1. Initialize a repository (repo), git init.
2. Commit files, first use git add [file] to stage/add files, then use git
commit to commit. Or use git commit -a to do stage and commit at a
time. git add [file] is multi-purpose, it can be used to begin tracking
new files, stage files, and mark merge-conflicted files as resolved.
3. Clone an existing repository, git clone [url] [optional, new repo name].
4. Check status of the files, git status.
5. Ignore files, a file called .gitignore in the repo. Use regular expressions
to specify patterns to be ignored.
6. Remove files,
Use git rm [file] to remove a file from the tracked files, and removes
it from working directory.
Use git rm –cached [file] to keep the file on hard drive but not have
Git to track it anymore.
2
7. Move files, git mv [oldfile] [newfile].
8. Commit history, git log, use git log -p -2 to show the difference intro-
duced in each commit, and only output the last two entries. Use git
log –pretty=oneline to show the output in an easier to read format.
9. Undoing things, git commit -amend to redo the commit.
10. Unstage a staged file, git reset HEAD [file].
11. Unmodify a modified file, git checkout – [file]. Don’t use unless you
absolutely know that you want to discard the modifications.
2.2 Commands with Remotes
1. Clone a remote repo by using git clone [url/ssh], list the shortnames of
each remote by git remote -v.
2. Add a shortname for a remote by git remote add [shortname] [url/ssh].
3. Fetch and pull from remotes,
Use git fetch [remote-name] to fetch new work that has been pushed
to the server (for example, github) , but it doesn’t automatically merge
with your work or modify you are currently working on.
Use git pull [remote-name] to automatically fetch and merge a re-
mote branch into your current branch.
4. Push to the remotes, use git push [remote-name] [branch-name].
5. Inspect a remote, git remote show [remote-name].
6. Remove or rename remotes, use git remote rename [oldname] [newname]
to rename, use git remote rm [remote-name] to remove a local mirror of
the remote.
2.3 Tagging
Tagging is typically used to mark release points.
1. List tags, git tag, or git tag -l ”[somepattern]”.
3
2. Create tags,
Annotated tags with tagger name, email date, git tag -a [tagname].
Lightweight tag with just a checksum stored in the file and no other
information, git tag [tagname].
3. Show tags, git show [tagname].
4. Tag previous commit, first find the commit checksum or part of it by,
git log –pretty==oneline, then git tag -a [tagname] [SHA-1 hash].
5. Push tags to remote, git push [remote-name] [tagname] or git push
[remote-name] –tags to push all tags.
2.4 Aliases
Aliases can be created by git config –global alias.[alias] [command].
3 Branching
A branch in Git is a lightweight movable pointer to one of the commits. The
default branch name in Git is master.
Git keeps a special pointer HEAD, a pointer to the local branch you’re cur-
rently on.
1. Create branch, git branch [branchname].
2. Switch branch, git checkout [branchname], create and switch at the same
time by git checkout -b [branchname]. It’s best to have clean working
state when switching branches, i.e., leave no uncommitted changes in
working directory or staging area.
3. Basic merging. Git does a simple three way merge using the two snap-
shots pointed to by the branch tips and the common ancestor of the
two. First switch back to the branch you want to merge into and use git
merge [anotherbranch] to merge anotherbranch into the current branch.
4.
4