Git常用命令总结
source link: https://allenwind.github.io/blog/4892/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
本文总结常用的Git命令和用法。关于Git的相关概念,诸如分支、工作区、暂存区、版本控制系统、分布式和集中式、工作流等参考其他资料。这里推荐《Git for Teams》
提一下历史
当然,这里提一提Git的历史。Linux内核开发最初使用手工合并,累吧!后来,使用专有的版本控制系统BitKeeper。但这个系统一直我Linux的开源精神有冲突,后来Linus干脆自己开发了一个版本管理系统来替代BitKeeper,于是就有了Git。有趣的是,Git的分布式实现使用了rsync,而这个工具的作者正是将BitKeeper推下历史舞台的Linux开发者。
提交历史查看
git log --oneline # 只看commit
git log --stat # 文件变更列表
git log -p # 文件的具体变更情况
git log --author ${author} # 只看指定${author}
配置全局命令
--global
表示设置参数是全局。刚按安装完Git后需要配置用户名和email。
git config --global user.name "yourname"
git config --global user.email "yourname@yourmail"
利用配置命令也可以创建命令的别名。下面把status命令设置别名st。这样,可以用git st
代替git status
。
git config --global alias.st status
平常常用的命令也可以设置更短的别名以提高效率。
git config --global alias.lol "log --pretty=oneline"
为git
添加HTTPS
和HTTP
代理
git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
如果取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
事实上这些修改都保存到文件~/.gitconfig
中,直接修改该文件也可以:
[user]
email = yourname@yourmail
name = yourname
[https]
proxy = https://127.0.0.1:1080
[http]
proxy = http://127.0.0.1:1080
Git配置全局ignore
git config --global core.excludesfile ${your_ignore_file}
本节的命令需要要掌握的基本概念:工作区、版本库(包括暂存区、分支)
git init
创建Git版本管理创库
git status
查看创库当前状态
git add target-file
把目标文件从工作区添加到暂存区
git add --all
把当前工作区的所有文件添加到暂存区
git rm filename
remove file
git commit -m "commit file"
把暂存区提交到当前分支
git commit -am "commit file"
从工作区直接提交到分支
git diff target-file
比较工作区目标文件和本文库分支中的差别
git diff HEAD --target-file
比较工作区target-file和版本库里面最新的版本,同上
git log
显示记录提交历史
git log --pretty=oneline
简单显示提交历史
git log --graph
查看分支合并情况图
git log --abbrev-commit
commit-id以短名称显示
这三个log命令可以合并:
git log --graph --pretty=oneline --abbrev-commit
git reflog
记录和本地版本库有关的操作,比如接下来的版本退回操作
git reset --hard HEAD^
把当前版本退回到上一个版本,如果HEAD后有两个^,就表面退回上两个本文
git reset --hard commit-id
把本文退回到指定的commit-id版本
git checkout -- target-file
把工作区中的目标文件的修改(包括删除)丢弃
git reset HEAD target-file
把暂存区的修改撤掉,把其从新放到工作区
git restore --staged <文件>...
以取消暂存
git restore <文件>...
丢弃工作区的改动
当然我们也可以通过配置简单化这个命令:
git config --global alias.unstage "reset HEAD"
git rm target-file
把目标文件删除。被Git跟踪的文件最好使用这个命令而不是Linux中的rm命令。如果误删可以通过git checkout -- target-file
恢复
git clone https://github.com/resp/resp.git
克隆远程创库
git remote -v
查看远程创库信息
git branch -r
查看远程创库分支
git fetch
拉取远程分支
git pull
拉取远程分支内容并合并本地分支
git remote set-url origin https://github.com/allenwind/resp.git
为origin设置或更新地址
git remote add origin https://github.com/allenwind/resp.git
关联本地创库和远程创库resp.git
git push -u origin master
把本地master分支推送到远程创库origin(这是远程创库的默认名字)-u
用于首次推送
git checkout -b feature origin/feature
创建远程origin的feature分支到本地
git branch --set-upstream develop origin/develop
建立本地develop分支和远程origin/develop分支的连接
git push origin tag-name
推送指定标签到远程创库
如果是多人协作开发,
1. 首先,可以试图用git push origin branch-name推送自己的修改;
2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
3. 如果合并有冲突,则解决冲突,并在本地提交;
4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!
git branch --set-upstream branch-name origin/branch-name
如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令
git push origin --tags
推送所有标签到远程创库
git push origin :refs/tags/tagname
删除远程创库的tagname标签
有时候提交错误的commit到远程仓库,可以通过rebase来rollback这个commit,
git log # 查看你要回撤的commit的前一个commit的${id},有时候要配合grep,注意是前一个
git rebase -i ${id} # 进入编辑模式,把要回撤的commit那行pick改为drop
# 之后推出编辑模式,然后根据提示解决冲突,这样历史commit中就删掉了指定的commit
git branch
查看分支列表,带星号表示当前分支,-r
参数表示远程创库分支列表
git branch develop
创建develop分支
git branch -d develop
删除develop分支
git branch -D develop
强制删除develop分支,用于删除没有合并的分支
git checkout develop
撤换到分支develop
git checkout -b develop
创建develop分支并切换到该分支
git merge develop
合并develop分支到当前分支,默认为fast-forward
模式,即直接设置HEAD指针指向,删除分支后丢掉分支信息
git merge --no-ff -m "merge with no-ff" develop
禁用fast-forward模式合并分支,由于要重新提交,所以要在-m参数后指定提交信息
git stash
把工作区的内容暂存起来,用于快速恢复,不同于add到暂存区。
git stash list
查看暂存的列表
git stash apply
恢复暂存stash到工作区
如果有多个暂存stash可以指定stash:
git stash apply stash@{id}
git stash drop
把暂存stash删掉
前两条命令可以合并为:
git stash pop
恢复工作区内容并把stash删掉
cherry-pick
cherry-pick命令用于指定的提交(commit)应用于其他branch中。
git cherry-pick <commit-id>
上面命令就会将指定的提交<commit-id>
,应用于当前分支。
git tag
查看标签
git tag tag-name
在最新提交上打tag-name标签
git tag -d tag-name
删除标签
git tag tag-name commit-id
指定commid-id上打上tag-name标签
git show tag-name
指定标签version上显示提交信息
git tag -a tag-name -m "tag message" commit-id
创建带有说明的标签
以上就是Git的基本用法,以后发现有新的常用方法会更新。
更新fork创库的内容
$git remote add upstream url
$git fetch upstream
$git checkout master
$git rebase upstream/master
$git push -f origin master
url
为创库的地址。
(更新于180204)
git submodule init
初始化子模块
git submodule add
添加子模块
git submodule update
更新子模块
持续更新~
转载请包括本文地址:https://allenwind.github.io/blog/4892
更多文章请参考:https://allenwind.github.io/blog/archives/
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK