Concepts
master | default developement branch |
origin | default upstream repository |
HEAD | current branch |
HEAD^ | parent of HEAD |
HEAD~4 | the great-great grandparent of HEAD |
create
mkdir project && cd project
git init
touch README.md
touch .gitignore
git add .
git commit -m 'Initial Commit'
git remote add origin git@git.com:<username>/<repository>.git
view
git status
git diff # changes to trackced files
git diff $id1 $id2 # what changed between $id1 and $id2
# History of changes for file with diffs
git log -p $file $dir/etc/tory/
# who chaned what and when in a file
git blame $file
git show $id # $id is commit identify
git show $id:$file
revert
#return to the last committed state
git reset --hard
git revert HEAD #revert the last commit
git revert $id #revert specified commit
git checkout HEAD $file #checkout the last commit of a file
git checkout $id $file #checkout the $id version of a file
push
git pull # pull from remote repository
git push # update remote repository
git push -u origin master # the first time that you push a branch, add -u
more details:
branch
git branch # view all local branch
git branch 1.x # add branch 1.x
git checkout -b <new branch> # create new branch locally
git checkout master # switch to master branch
git branch -d 1.x # delete branch 1.x
git push origin :1.x #delete remote branch 1.x
tagging
$git tag #listing your tags
$git tag v0.1.1 <commit_id> #create lightweight tag point to commit
$git tag -a v0.1.1 -m 'my version 0.1.1' #create annotated tags
$git push --tags #push tag to remote repository
$git push -d <tagname> #delete local tag
$git push origin :refs/tags/<tagname> #delete remote tag
sparse checkout
clone repository’s sub-directory only
git init
git remote add origin <url>
git config core.sparsecheckout true
echo some/dir/ >> .git/info/sparse-checkout
git pull origin master
misc
git status # view file status
git log
git log --stat #view commit info and updated file
git commit --amend -m 'commit message' # amend the last commit
git archive --format tar --output project.tar master
gitignore
build/ #ignore build directories no matter how deep are these directories
*/build/ # ignore build directories that are exactly one level deep
*.obj # ignore any file end of .obj
每个 commit 只改一件事情。如果一个文档有多个变更,使用 git add –patch 只选择文档中的部分变更进入stage
reference:
- http://git-scm.com/book
- http://lugir.com/git-basic.html