Pro Git is an excellent book about Git, and these are my reading notes.
This is a brief notes about some points of Pro Git. It only contains first 3 chapters (contains most commonly used commands of Git). I will maintain this article and keep revising it. Hope it might helpful to you.
##Before Running Git
Snapshots, Not Differences
The major difference between Git and any other VCS (Subversion and friends included) is the way Git thinks about its data. Conceptually, most other systems store information as a list of file-based changes.
Nearly Every Operation Is Local
Most operations in Git only need local files and resources to operate ¡ª generally no information is needed from another computer on your network.
Git Has Integrity
Git use checksum, namely, SHA-1, as a reference.
Git has three main states that your files can
reside in: committed, modified, and staged. Committed means that the data is safely stored in your local database. Modified means that you have changed the file but have not committed it to your database yet. Staged means that you have marked a modified file in its current version to go into your next commit snapshot.
The basic Git workflow goes something like this:
- You modify files in your working directory.
- You stage the files, adding snapshots of them to your staging area.
- You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
git clone git://github.com/USERNAME/REPO.git
Trace New File
git add FILE(The FILE edited after git add need to redo git add to store the changes)
if you want to ignore some files, add thier patter to .gitignore. Like *.[oa] (generate while compiling)
Glob patterns are like simplified regular expressions that shells use. An asterisk (*) matches zero or more characters; [abc]matches any character inside the brackets (in this case a, b, or c); a question mark (?) matches a single character; and brackets enclosing characters separated by a hyphen([0-9]) matches any character between them (in this case 0 through 9) .
Diff between staged and last commit file
git diff --cached
git commit. add
-mflag to add commit message,
-vto explicit remind every place you modified,
-ato automatically stage every file that is already tracked before doing the commit, letting skip the git add part.
git rm FILEto delete local files.
git rm --cached FILEto remove files in repository.(Could use glob)
git mv OLD NEWto change the names.
git log, add
-pto show differences in every commits, use
-2to see lasted 2 updates,
git commit --amendto add forgetted files or reedit commit messages. (See Book 2.4.1)
Unstaging Staged File
git reset HEAD FILE
Unmodifying a Modified File
git status(See Book 2.4.3)
Fetching and Pulling
git fetch REMOTE-NAMEpull down all the data from that remote project that you don’t have yet.
git fetch originfetches any new work that has been pushed to that server since you cloned (or last fetched from) it. It’s important to note that the fetch command pulls the data to your local repository and it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.
git push origin masterwill push local master branch to origin server.
git tagto add tags.
git showto check version info.
You can set up an alias for each command with git config. For example, using
git config --global alias.ci committo instead
git ci. Useful Tips:
git config --global alias.last 'log -1 HEAD'to check last commit by
In Git, this is apointer to the local branch you’re currently on.
Because a branch in Git is in actuality a simple file that contains the 40 character SHA-1 checksum of the commit it points to, branches are cheap to create and destroy. Creating a new branch is as quick and simple as writing 41 bytes to a file (40 characters and a newline).
Basic Branching and Merging
switch to a certain branch
git Check -b BRANCH-B,
git mergeto merge to master.
git branch -dto delete branches. It could be failed if it was unmerged. Thus, you could use
git branch -D BRANCH-Bto force deleting it, and
git branch --no-mergedto check unmerged work.
You’d better read the related part of this book! It is a little comfusing but important.
With the rebase command, you can take all the changes that were committed on one branch and replay them on another one. For example,
git rebase master serverchecks out the topic branch (in this case,server) for you and replays it onto the base branch (master).It quite cool, read Book 3.5.1 to know more. Summed up in one line Do not rebase commits that you have pushed to a public repository.