Git常用命令
参照GitPro整理了一些常用命令:http://git.oschina.net/progit/index.html
1、添加仓库(需要跟踪路径至该文件下,方可执行init命令)
git init
$ git clone git://github.com/schacon/grit.git
or $ git clone git://github.com/schacon/grit.git mygrit // mygrit 为别名
2、跟踪新文件/把跟踪的文件放入暂存区域/冲突标记为已解决
git add
git add . 所有的文件加入到暂存区域
3、克隆仓库
git clone [url]
4、查看当前文件状态
git status
5、查看已暂存和未暂存的更新
git diff
git diff --cached 查看上次提交和已暂存的文件差异
6、提交更新
git commit
git commit -m 提交注释
git commit -v 注释修改的每一行
git commit -a 把所有的暂存的文件一并提交(跳过add步骤)
git commit --amend 撤销操作
7、移除文件
git rm
git rm --cached 从暂存区/仓库中移除
$ git commit -m 'initial commit’
$ git add forgotten_file
$ git commit --amend
8、重命名
git mr
9、查看提交历史
git log
git log -p 显示每次提交内容的差异
git log -2 显示最近2次的更新
git log --stat 显示增改行的统计
git log --pretty 指定不同的显示方式
git log --pretty=oneline 按照一行显示
$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test code a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
git log --pretty=format 指定显示的格式
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 11 months ago : changed the version number
085bb3b - Scott Chacon, 11 months ago : removed unnecessary test code
a11bef0 - Scott Chacon, 11 months ago : first commit
选项 说明
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 -date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明
git log --since=2.weeks 限制输出长度:2周內的提交
选项 说明
-(n) 仅显示最近的 n 条提交
--since, --after 仅显示指定时间之后的提交。
--until, --before 仅显示指定时间之前的提交。
--author 仅显示指定作者相关的提交。
--committer 仅显示指定提交者相关的提交。
ex:
2000条的记录过滤之后:
$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
--before="2008-11-01" --no-merges -- t/
5610e3b - Fix testcase failure when extended attribute
acd3b9e - Enhance hold_lock_file_for_{update,append}()
f563754 - demonstrate breakage of detached checkout wi
d1a43f2 - reset --hard/read-tree --reset -u: remove un
51a94af - Fix "checkout --track -b newbranch" on detac
b0ad11e - pull: allow "git pull origin $something:$cur
10、取消已暂存的文件
git reset HEAD <file>
11、取消文件的修改(抛弃了文件的修改)
git checkout -- <file>
12、查看当前远程仓库
git remote 列出每个远程库的简短名字
git remote -v 显示对应的克隆地址
git remote add [shortname] [url] 添加一个新的远程仓库
ex:
要添加一个新的远程仓库,可以指定一个简单的名字,以便将来引用
$ git remote
origin
$ git remote add pb git://github.com/paulboone/ticgit.git
$ git remote -v
origin git://github.com/schacon/ticgit.git
pb git://github.com/paulboone/ticgit.git
现在可以用字符串 pb 指代对应的仓库地址了。比如说,要抓取所有 Paul 有的,但本地仓库没有的信息,可以运行 git fetch pb:
$ git fetch pb
remote: Counting objects: 58, done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 44 (delta 24), reused 1 (delta 0)
Unpacking objects: 100% (44/44), done.
From git://github.com/paulboone/ticgit
* [new branch] master -> pb/master
* [new branch] ticgit -> pb/ticgit
现在,Paul 的主干分支(master)已经完全可以在本地访问了,对应的名字是 pb/master,你可以将它合并到自己的某个分支,或者切换到这个分支。
13、从远程仓库抓取数据
$ git fetch [remote-name] fetch 命令只是将远端的数据拉到本地仓库,并不会合并
14、推送到远程仓库
git push (远程仓库名) (分支名)
ex:把本地的 master 分支推送到 origin 服务器上
$ git push origin master
15、查看远程仓库信息
git remote show [remote-name]
16、远程仓库的删除和重命名
git remote rename
ex:比如想把 pb 改成 paul
$ git remote rename pb paul
$ git remote origin paul
git remote rm 移除远程仓库
git push [远程名] :[远程分支] 删除远程分支
git push [远程名] [本地分支]:[远程分支] ,如果省略 [本地分支],那就等于是在说“在这里提取空白然后把它变成[远程分支]”
17、标签
git tag
git tag -1 “tag” 特定标签
git tag -a v1.4 -m 'my version 1.4’ 含附注的标签
git show 查看标签信息
ex:
$ git show v1.4
tag v1.4
Tagger: Scott Chacon <schacon@gee-mail.com>
Date: Mon Feb 9 14:45:11 2009 -0800
my version 1.4
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7... a6b4c97…
Author: Scott Chacon <schacon@gee-mail.com>
Date: Sun Feb 8 19:02:46 2009 -0800
Merge branch ‘experiment'
git push origin [tagname] 分享标签
18、自定义命令
git config --global alias.unstage 'rest HEAD --' 相当于 git unstage fileA
git config --global alias.last 'log -1 HEAD’ 查看最后已提交的信息
git config --giobal alias.co checkout
git config --giobal alias.br branch
git config --giobal alias.ci commit
git config --giobal alias.st status
19、分支
git branch [分支名称] 创建分支
git checkout [分支名称] 切换分支
git merge [分支名称] 合并分支
git branch -d [分支名称]删除分支
git branch —D [分支名称] 强制删除
20、衍合
把一个分支中的修改整个到另一个分支有2个办法 merge和rebase
优点:
衍合的能产生一个更整洁的历史
通过合并一个分支来整合分叉了的历史
git rebase --onto master server client
意思就是:取出client分支,找出client分支和server分支的共同祖先变化,把它们在master重新演示一遍
注:一旦分支中的提交对象发布到公共仓库,就千万不要对该分支进行衍合操作。