参考资料
Open Source Guides
Git教程
廖雪峰Git教程: 入门和快速上手
Pro Git:适合有一定基础的同学。
git recipes:
Git recipes in Chinese. 高质量的Git中文教程.
Git Workflow
A successful Git branching model
Git and Xcode
Versioning with Xcode and git: Using a script in your build phase, you can run a shell to determine the version number and inject this into the Info.plist of a build. It will never modify your local project, just the created build.
gitignore: 生成各种gitignore
gitignore: A collection of useful .gitignore templates
Git 命令快速查询
git config
$ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit $ git config --global alias.st status $ git config --global alias.last 'log -1 HEAD' $ git config --global alias.unstage 'reset HEAD --' $ git config user.name "donganyuan" $ git config user.email "donganyuan@baidu.com" $ git config --global core.editor "atom --wait"
git commit
$ git commit -a -m "comment" //添加并提交 $ git commit --amend //修改上次commit, do not amend your last commit if you have already pushed it
git merge
$ git merge [branch name]
禁用fast forward merge
$ git merge --no-ff -m "comment" branch_namegit log
$ git log -p -2 //查看每次提交的diff $ git log --stat // summary of the information $ git log --pretty=oneline // prints each commit on a single line $ git log --pretty=format:"%h - %an, %ar : %s" //Abbreviated commit hash - Author name, Author date : Subject $ git log --since=2.weeks $ git log --author=AnYuan $ git log --grep=keywords $ git log --oneline --decorate // show you where the branch pointers are pointing. $ git log -S function_name // only show commits adding or removing code matching the string. //this command shows you any commits in your current branch that are not in //the master branch on your origin remote. $ git log origin/master..HEAD
git remote
查看远程仓库信息: $ git remote -v $ git remote add dev https://github.com/ddd/git $ git remote show origin //inspecting a remote
git stash
$ git stash --保存未提交修改
$ git stash list --查看当前stash
$ git stash apply stash@{0}
$ git stash drop
等价于
$ git stash popgit branch
分支管理: $ git checkout -b dev //-b 表示创建并切换分支 等价于 $ git branch dev $ git checkout dev git branch //查看当前分支 *表示当前分支 $ git branch -d dev //删除dev分支 $ git branch -D dev //强制删除未merge分支 $ git branch -vv //to see what tracking branches you have set up 禁用fast forward merge $ git merge --no-ff -m "comment" branch_name //share branch $ git push origin branch_name //delete remote branch $ git push origin --delete [branchname]
undo
1.未add 修改文件时(工作区),撤销可以: $ git checkout -- [filename] 把文件在工作区的修改全部撤销, 把这个文件会退到最近一次commit或add状态 这个操作很危险,checkout后无法恢复了 2.add修改文件后(修改进入暂存区),撤销可以: $ git reset HEAD [filename] 3.commit后,撤销: $ git reset --hard HEAD^
git reflog
git diff
$ git diff //to see what you have changed but not yet staged $ git diff --staged or cache // compares your staged changes to your last commit.
git reset
$ git reset --hard HEAD^ 回退到上一次commit $ git reset --hard 3628164 //HEAD^^ 回退到上两个版本 //HEAD~100 回退到上100个版本 $ git reset --soft //update HEAD pointer $ git reset --soft HEAD~ 等价于git commit --amend $ git reset --mixed //default 参数,将commit的修改,放到stage区 $ git reset --hard //update working copy //squashing $ git reset --soft HEAD~2
git rm
$ git rm [filename]
//keep the file in your working tree but remove it from your staging area.
$ git rm --cached filenamegit tag
打标签,tag默认在本地,必须显示的push到服务器
$ git tag v1.0
$ git tag v0.9 commit-id
$ git tag -a v0.1 -m "version 0.1 released" commit-id
$ git push origin v1.0 //推送标签到远程服务器
$ git push origin --tags //一次性推送全部尚未推送到远程的本地标签
//删除标签
//1.删除本地
$ git tag -d v0.9
//2.推送到远程
$ git push origin :refs/tags/v0.9git grep
git grep will look through the files in your working directory.
you can search through any tree in Git, not just the working directory.
// print out the line numbers where Git has found matches. $ git grep -n/--line-number // summarize the output by showing you only which files contained // the search string and how many matches there were in each file $ git grep -c/--count // display the enclosing method or function for each matching // string with either of the -p or --show-function options $ git grep -p self.window // ensures that multiple matches must occur in the same linie of text. $ git grep --break --heading \ -n -e '#define' --and \( -e LINK -e BUF_MAX\) v1.8.0
git bisect
git bisect use binary search to find the commit that introduced a bug.
$ git bisect start // Current version is bad git bisect bad // v2.6.13-rc2 is known to be good $ git bisect good v2.6.13-rc2
Once you have specified at least one bad and one good commit, git bisect selects a commit in the middle
of that range of history, checks it out.
You sholud now compile the checked-out version and test it. If that version works correctly, type
If that version is broken type
reset
Squash all commits related to a single issue into a single commit
$ git rebase -i HEAD~4 // To squash four commits into one // commit, then change 'pick' -> 'squash' next to the commmits you want to squash
Submodules
$ git submodule add [url] $ git submodule init // cloning a project with submodules $ git submodule update --remote --merge // fetch all the data from subproject $ git submodule foreach 'git stash' // run arbitrary command in each submodule