git基本命令
2017-01-14 22:41
302 查看
基本流程
stash ----> work ----> index ----> local repo ----> remote repo
stash 暂存空间
work 当前工作空间
index 为git追踪文件目录,通过git add将文件加入index(精心准备提交的文件)
local repo 本地仓库
remote repo 远程仓库
基本命令
CONFIG
git config --global color.ui true git config --global core.editor vim git config --global user.name "spch2008" git congig --global user.email "spch2008@foxmail.com" # 配置命令缩写 git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status
INIT
git init
or
git clone url git clone https://www.spch2008.com/path/examle.git #只读 git clone user@www.spch2008.com:path/example.git #读写
FILE
# 查看文件变化 git status # 类似svn的显示,概要显示文件变化 git status -s # 添加要提交的文件 git add filename # 添加当前目录的所有变动文件 git add . # 添加某一类文件(通配符) git add '*.txt' # 删除文件 git rm filename # 提交文件 git commit -m "fist comment" # 修改最近一次条件的comment git commit --amend "this is first comment" # 查看提交历史 git log
DIFF
# 工作目录的文件与Stage中文件的异同比对 git diff # (Stage中文件和工作目录中文件)与LocalRepo中文件异同比对 git diff HEAD # 两次提交的异同比对 git diff commit_version1 commit_version2 # 当前分支与brachname分支的异同比对 git diff branchname # diff的一个统计结果(几个修改,几个删除等) git diff --stat
BRANCH
# 查看本地分支 git branch # 切换到本地分支 git checkout branchname # 基于当前分支checkout一个新分支 git checkout -b new_branchname # 将branchname合并到当前分支 git merge branchname # 删除本地分支(若未合并到其它分支,会报错) git branch -d branchname # 强制删除一个分支 git branch -D branchname
REMOTE
# 查看远程仓库 git remote # 查看远程仓库(含url) git remote -v # 添加远程仓库 git remote add remote_repo_name <url> # 本地仓库推送到远程同名仓库 git push # 指定仓库名 git push origin branchname # 拉取远程同名仓库 git pull # 指定拉取仓库 git pull origin branchname # 仅拉取内容不与本地仓库merge git fetch # git pull 拉取远程仓库,并与本地做merge,实际等于 git fetch && git merge origin/remote-branchname # 查看远程分支 git branch -a
高级命令
stash ---> work ---> stage ---> local repo ---> remote repo
版本回退
reset
reset 用于版本回退,可以将提交到本地仓库但没有推送至远程的一些提交进行回退,如下表所示,初始版本都是5,使用不同的参数
hard,
soft,
mix后,流程中版本情况。
参数 | work | stage | local repo |
---|---|---|---|
版本 | 5 | 5 | 5 |
hard | 3 | 3 | 3 |
soft | 5 | 5 | 3 |
mix | 5 | 3 | 3 |
git reset --hard commit_id git reset --hard 6c9dcada31dfb76688854f280d843ec132b93912 git reset --hard HEAD~1 #由版本5回退至4 git reset --hard HEAD~2 #由版本5回退至3
revert
reset 不适用于远程仓库的回退,因为推送至远程,别人就有依赖的可能。revert会创建一个新的提交,来达到删除的目的。比如原始版本号为:1->2->3->4,现在我想回退至版本3,则最终为:1->2->3->4->5。版本5的内容与版本3一致。git revert commit_id git revert 6c9dcada31dfb76688854f280d843ec132b93912 git revert HEAD~2
checkout
创建分支git co -b branchname
文件回退
checkout
从本地库checkout文件,覆盖工作空间相应文件,本地库文件版本以及内容不受影响。# 丢弃当前工作空间filename的修改 git checkout filename # 以commit_id的文件替换当前工作空间filename文件 git checkout commit_id filename # 距离head n个版本的filename替换当前空间空间filename git checkout HEAD~n filename
reset
从本地库checkout文件,覆盖stage空间相应文件,本地库文件版本以及内容不受影响。git reset filename git reset commit_id filename git reset HEAD~n filename
当git add filename的时候,想从stage中移除,可以使用git reset filename来恢复,使得stage中文件内容与本地库一致。
其它用法
stash
场景:正在分支new_feature上coding,突然接到通知,master有bug,此时git co master -b fix_bug失效,因为new_feature上有未commit的修改,但是coding没有完成,尚不能提交,于是,stash来大显身手。
git checkout master -b new_feature #创建功能分支 do some work git commit -m "fist commit" #提交一次 do some work git checkout master -b fix_bug #创建分支修复bug error: You have local changes to 'file'; cannot switch branches. git stash #收藏修改 git checkout master -b fix_bug #创建分支修复bug do fix work git checkout new_feature #切回功能分支 git stash pop #还原修改,同时删除保存的修改
git stash apply 还原修改,但不删除保留的修改
**提交一次**,如果new_feature分支没有提交,那么new_feature与master版本一致,git
checkout 新分支,本地修改会带到新分支中。
blame
如果出现代码纠纷,通过git blame filename查看某行代码被谁修改。
rebase
场景:创建一个分支,进行开发,开发完后,会有多次提交。然后,在合并到主干的时候,这些comment也会一并被合并到主干。对于主干来说,你开发过程中的一些提交它并不关心,但是却被合并进去了,造成冗余。每次提交都会创建镜像,造成空间浪费,因此,我们在开发完成,测试通过后,可以将之前多个comment压缩成一个进行合并,减少主干压力。git co -b new_feature #创建分支 git commit -m "first commit" git commit -m "second commit" git commit -m "third commit" git rebase -i master
git rebase 提示信息如下:
pick 5d783e3 first commit pick 92de2cf second first pick 130af52 third commit # Rebase cd083a5..130af52 onto cd083a5 # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted.
将后两个提交压缩到第一个上,修改命令如下:
pick 5d783e3 first commit squash 92de2cf second first squash 130af52 third commit
保存后,提示修改commit信息,可以保留原始,也可是新加commit信息
Rebasing (3/3) # This is a combination of 3 commits. # The first commit's message is: first commit # This is the 2nd commit message: second commit # This is the 3rd commit message: third commit
如果已经推送到远程分支,还没有合并到主干,但由于bug需要修正,此时可以删除远程分支,重新将本地分支rebase后,推送到远端。
数据流动
stash -> work: git stash applyor
git stash pop
work -> stash:
git stash
work -> index:
git add filename
index -> work:
git reset filename
index -> local repo:
git commit
相关文章推荐
- GDB总结
- ZCMU-1766-Virtual Friends
- 数据结构实验之链表九:双向链表
- 平衡方案
- 数字下变频和数字上变频
- CentOS6.5 安装Python3.6 (一个小白的安装过程全纪录!)
- CodeForces 754C Vladik and chat (DP+暴力)
- CCF201312试题
- 吃一口肥肉之小程序牛刀小试(一)
- 数据结构实验之链表八:Farey序列
- 重拾java基础(二):java基础语法总结
- dubbo-spring-boot-starter
- 用Visual Studio 2015 编写第一个UMDF驱动遇到的问题!!
- word分节符,分页符的区别,链接到上一页
- opencv学习(二十三)之方框、均值、高斯、中值、双边滤波综合比较
- Java 多线程:yield
- Java中接口的特点
- JDBC连接Mysql Log提示 According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be
- EL表达式
- 二维码Data Matrix的解码实现(zxing-cpp)