Page tree
Skip to end of metadata
Go to start of metadata

基本功能

快速上手

git - the simple guide

命令行方式的快速指南

命令

以下是一些常用命令

命令示例说明
git merge-basegit merge-base master develop 
git reset --hard  
git show  
git reflog reflog是git用来记录引用变化的一种机制,比如记录分支的变化或者是HEAD引用的变化。git reflog命令可用于
找回因不当使用git reset 而丢失的commit. 先跑git reflog, 结果列表中的HEAD@{1}就是前一次HEAD所在位置,
然后 git merge <commit_hash> (或git reset --hard <commit_hash>)可以恢复。
参考 restoring lost commits http://gitready.com/advanced/2009/01/17/restoring-lost-commits.html
 
git merge ... -s oursgit merge branch1 -s ours合并,但不引入源分支的任何change,实际上是一个dummy merge
git merge <branch> -s recursive -X theirs/oursgit merge feature/0.6/dummy_feature_2 -s recursive -X theirs合并分支,自动解决冲突,如果不能解决的,用theirs/ours指定用哪一边的改动
git show-branch --remotes --listgit show-branch --list | grep deploy列出所有分支,还可以跟grep命令结合用于查找分支
git merge --no-commit  
git update-index --[no-]assume-unchanged  

git push origin --delete <branch>

 删除服务器端的分支
git merge --abort
  
git merge --no-ff  
git revert -m 1 commit_hash
 

See chapter 4 in the git book and the original post by Linus Torvalds.

To undo a merge that was already pushed:

出处:http://stackoverflow.com/questions/2389361/undo-a-git-merge

git cat-file -p <hash>git cat-file -p 0ad9c330014dd375e4c560f725bf8895c74739c7显示文件的内容
git show HEAD^1

git show HEAD^2

git show HEAD^^2

显示merge节点的parent
git log -S'source_code_to_be_searched'git log -S't_vplus_member_with_location'搜索包含指定源代码片段的提交
git log --pretty=format:"%h%x09%an%x09%ad%x09%s" 一行显示commit log,包含hash, author, date, message等信息
git log --oneline | grep "Merge branch" | grep "'develop' " 搜索从develop到其他分支的合并
git log --no-merges --stat --author="Pattern" --name-only --pretty=format:"" | sort -u
 
显示某个作者修改过的所有文件
参考:
http://stackoverflow.com/questions/6349139/can-i-get-git-to-tell-me-all-the-files-one-user-has-modified 
git log --author="<authorname>" --oneline --shortstat

git log --author="jacob.wu" --pretty=tformat: --numstat

 列出某位作者的修改,带行数,用于代码行数统计
http://stackoverflow.com/questions/1265040/how-to-count-total-lines-changed-by-a-specific-author-in-a-git-repository/7010890 
git blame --reverse START..END filename
git blame --reverse aefdcd..HEAD /path/filename

可用于找出谁删除了某一行代码,先要找到某个commit包含你想追踪的代码,把这个commit作为START;也可以用git log -S'code'达到同样目的,但git log -S'code'可能会找到很多重复的代码。

Walk history forward instead of backward. Instead of showing the revision in which a line appeared, this shows the last revision in which a line has existed. This requires a range of revision like START..END where the path to blame exists in START.
参考:http://stackoverflow.com/questions/4404444/how-do-i-blame-a-deleted-line

git gc --prune=now

git fetch -p

 清除失效的本地remote tracking分支
http://stackoverflow.com/questions/1072171/how-do-you-remove-an-invalid-remote-branch-reference-from-git
git archive --format zip --output "output.zip" master 参考 http://stackoverflow.com/questions/160608/do-a-git-export-like-svn-export
$ git --no-pager log --author="tanke.wyj" --oneline --stat=400 --since=6.month
 > /d/Dev/stats.txt
计算过去一段时间某个账号提交的代码量参考 https://stackoverflow.com/questions/1265040/how-to-count-total-lines-changed-by-a-specific-author-in-a-git-repository

本地忽略文件

http://stackoverflow.com/questions/1753070/git-ignore-files-only-locally

实测编辑.git/info/exclude后就生效了

修改远程分支名字

Rename master branch for both local and remote Git repositories

http://stackoverflow.com/questions/1526794/rename-master-branch-for-both-local-and-remote-git-repositories

git branch -m master master-old 
git push remote :master # delete master,应为git push origin :master
git push remote master-old # create master-old on remote

回退远程分支

git reset <commit-hash>

git push --force

( git push --force-with-lease )

Gitflow

Smart branching with SourceTree and Git-flow
https://blog.sourcetreeapp.com/2012/08/01/smart-branching-with-sourcetree-and-git-flow/

nvie/gitflow - Git extensions to provide high-level repository operations for Vincent Driessen's branching model. http://nvie.com/posts/a-successful-git-branching-model/
https://github.com/nvie/gitflow

相关页面:Git分支模型S项目Git分支管理模型

参考资料

Git Hooks

Customizing Git - An Example Git-Enforced Policy
http://www.git-scm.com/book/en/v2/Customizing-Git-An-Example-Git-Enforced-Policy

StackOverflow - Can I enforce a merge-only branch in git?
http://stackoverflow.com/questions/4083724/can-i-enforce-a-merge-only-branch-in-git 

Git问答

Find the parent branch of a git branch

http://stackoverflow.com/questions/3161204/find-the-parent-branch-of-a-git-branch

Git branch naming conventions

http://www.guyroutledge.co.uk/blog/git-branch-naming-conventions/

Git 安装

How to Install Git 2.8.1 on CentOS/RHEL 7/6/5 & Fedora 23/22
可以安装最新版本 

 

 

 

 

  • No labels