您的位置:首页 > 其它

Git常用命令及技巧

2013-01-18 09:30 447 查看


git commit:

git commit --amend
撤销上一次提交


git push:

git push [remote-name] [master]
推送数据到远程仓库

git push origin :[branch-name]
删除远程分支.


git diff:

git diff --name-only 73a79c 2d49d2
查看两个版本中间改动过的文件列表

git diff : workspace and index file.

git diff HEAD: workspace and commint

git diff --cached: index file and commit


git fetch:

git fetch [remote-name]
取回远程仓库的所有提交信息


git clone:

git clone [url]
获取远程仓库的 master 分支


git log:

git log -1 HEAD
显示最后一次提交信息

git log -p
显示每次提交的内容差异, 可加参数 -num 显示最近num次提交差异

git log --stat
仅显示增改行数统计.

git reflog
查看rest, checkout 等操作纪录.
git log --pretty=oneline
用一行显示每次提交信息.

git log --pretty=fuller
额外显示提交日期.

git log --since=2.weeks
显示最近两周修改

git log --pretty=format:"%h - %an, %ar : %s"


选项 说明

%H 提交对象(commit)的完整哈希字串

%h 提交对象的简短哈希字串

%T 树对象(tree)的完整哈希字串

%t 树对象的简短哈希字串

%P 父对象(parent)的完整哈希字串

%p 父对象的简短哈希字串

%an 作者(author)的名字

%ae 作者的电子邮件地址

%ad 作者修订日期(可以用 -date= 选项定制格式)

%ar 作者修订日期,按多久以前的方式显示

%cn 提交者(committer)的名字

%ce 提交者的电子邮件地址

%cd 提交日期

%cr 提交日期,按多久以前的方式显示

%s 提交说明


git checkout:

git checkout -- <file>
撤销对文件file的修改

git checkout -b branch-name
创建并切换到分区.


git branch:

git breanch -d branch-name
删除分支.

git branch -D branch-name
强制删除分支.

git merge master
合并主分支到当前分支.


git remote:

git remote
列出当前项目的远程库

git remote -v
显示当前项目对应的克隆地址

git remote add [shortname] [url]
添加远程仓库

git remote show [remote-name]
查看远程仓库的详细信息

git remote [remote-old-name] [remote-new-name]
修改远程仓库名称

git remote rm [remote-name]
删除远程仓库


git reset:

git reset HEAD <file>
撤销已经被暂存(git add)的文件

git reset --soft: 撤销并回退 commit, 不影响 index file, 撤销到哪个位置由最后一个参数指定. git reset --soft HEAD^

git reset --hard: 撤销 commit, index file and workspace

git reset --mixed: 默认选项, 撤销 commit and index file, 只保留workspace.

git reset --: 删除登记在 index file 里的某个文件.


git show-branch:

+(加号)表示所在分支包含此行所标识的commit

(空格)表示所在分支不包含此行所标识的commit

-(减号)表示所在分支是经过merge得到的,而所在行的内容即是merge的基本信息。

*(星号)表示如果需要在某列标识+(加号),且此列为当前分支所在列,那么则将+(加号)转变为*(星号)。


git:

git gc
收缩空间

git count-objects -v
查看占用空间大小.


git恢复删除文件

git 创建分支恢复文件步骤:

git branch [recover-branch]


git checkout [recover-branch]


git checkout master


git branch -D recover-branch


要查看删除的文件: git ls-files –deleted

恢复则需要从新checkout: git checkout – <deleted_file>

多个文件同时操作可以使用xargs

git ls-fies -d | xargs git checkout --

git checkout -f 恢复删除文件


忽略提交某些文件或文件夹

1. 在 .gitignore 文件内写入文件名或目录名即可忽略提交, 但只对只对没有被 track 的文件或目录有效, 对于已经加入版本管理的文件是无效的.

2. 已经加入版本管理的文件或目录可使用如下命令忽略:

git update-index --assume-unchanged PATH


常见问题

1. 执行 git remote add origin... 时出现错误: fatal: remote origin already exists.

执行如下指令后再git remote add origin... :

git remote rm origin
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: