Basic configuration:
$ git config --global user.name "Guo Song"
$ git config --global user.email "songguo.hit@gmail.com"
$ git config --global core.editor vim
$ git config --global merge.tool vimdiff
1 Initializing a Repository in an Existing Directory
git init
git add .
git add mydir/
git commit -m "initial project version"
2 Cloning an Existing Repository
git clone git://github.com/songuo/learninggit.git
git clone git://github.com/songuo/learninggit.git mygit
3 Ignoring Files
cat .gitignore
*.[ao] # no .a and .o files
*~
# a comment - this is ignored
!lib.a # but do track lib.a, even though you're ignoring .a files above
/TODO # only ignore the root TODO file, not subdir /TODO
build/ # ignore all files in the build/ directory
doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt
*/target/* # ignore any directory named target and anything under
4 View Staged and Unstaged Changes
git status
git diff
5 Removing and Moving Files
git rm dele_file
git mv file_from file_to
6 View the Commit History
git log
git log -p -2
git log --since=2.weeks
git log --since=2013-05-01
7 Undoing things
7.1 Changing Your Last Commit
git commit --amend
7.2 Unstaging a Staged File
For example, let’s say you’ve changed two files and want to commit
them as two separate changes, but you accidentally type git add *
and stage them both. How can you unstage one of the two? The git status command reminds you:
$ git add .
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: README.txt
# modified: benchmarks.rb
Right below the “Changes to be committed” text, it says use git reset HEAD <file>... to unstage.
So, let’s use that advice to unstage the benchmarks.rb file:
$ git reset HEAD benchmarks.rb
7.3 Unmodifying a Modified File
$ git status
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: benchmarks.rb
#
It tells you pretty explicitly how to discard the changes you’ve made.
Let's do what it says:
$ git checkout -- benchmarks.rb
8 Working with Remotes
8.1 Showing Your Remotes
git remote
git remote -v
8.2 Adding Remote Repositories
git remote add [shortname] [url]
git remote add origin https://github.com/songuo/learninggit.git
git remote add pb git://github.com/songuo/learninggit.git
8.3 Fetching and Pulling from Your Remotes
git fetch [remote-name]
git fetch origin
It’s important to note that the fetch command pulls the data to your
local repository — 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.
If you have a branch set up to track a remote branch,
you can use the git pull command to automatically fetch and then
merge a remote branch into your current branch.
8.4 Pushing to Your Remotes
git push [remote-name] [branch-name]
git push origin master
8.5 Removing and Renaming Remotes
git remote rename [old_name] [new_name]
git remote rename pb paul
git remote rm paul
9 Tagging
9.1 Listing Tags
git tag
9.2 Creating Annotated Tags
git tag -a v1.5 -m "my version 1.5"
git show v1.5
9.3 Creating Lightweight Tags
git tag v1.5-lw
git tag v2.0
10 Git Branching
Create a new branch called tesing:
$ git branch testing
Swich to an existing branch:
$ git checkout tesing
Create a new branch and switch to it:
$ git checkout -b testing
Merging
$ git checkout master
$ git merge testing
Branch Management
$ git branch
$ git brach -v