Git 分支利器不得不说的事

一、基本动作

Git 中的分支实际上仅是一个包含所指对象校验和(40 个字符长度SHA-1 字串)
的文件,所以创建和销毁一个分支就变得非常廉价。

Git 是如何知道你当前在哪个分支上工作的呢?其实答案也很简单,它保存着一个
名为HEAD 的特别指针

创建:git branch iss53

删除:git branch -d iss53

切换:git checkout iss53

创建并切换:git checkout -b iss53

查看哪些分支已被并入当前分支:git branch --merge 或者相反:git branch --no-merged

二、切换分支必须遵循的状态

只有在以下状态下才可以切换分支:

1、当前分支中的所有文件都已经提交到本地版本库。

2、把当前的还未提交的文件进行储藏,保证工作目录是干净的之后,才可以切换分支。

储藏的主要命令是:

git status
# On branch test
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   b.txt
#   modified:   mx
#
no changes added to commit (use "git add" and/or "git commit -a")

再执行git stash之后

git stash
Saved working directory and index state WIP on test: 32211b0 add b.txt
HEAD is now at 32211b0 add b.txt
git status
# On branch test
nothing to commit (working directory clean)

要查看现有的储藏,你可以使用git stash list:

git stash list
stash@{0}: WIP on test: 32211b0 add b.txt

你可以重新应用你刚刚实施的储藏,所采用的命令就是之前在原始的stash 命令的帮助输出里提
示的:git stash apply。如果你想应用更早的储藏,你可以通过名字指定它,像这样:git
stash apply stash@2。如果你不指明,Git 默认使用最近的储藏并尝试应用它。

git stash apply
# On branch test
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   b.txt
#   modified:   mx
#
no changes added to commit (use "git add" and/or "git commit -a")
git stash list
stash@{0}: WIP on test: 32211b0 add b.txt

可以看出:

apply 选项只尝试应用储藏的工作——储藏的内容仍然在栈上。要移除它,你可以运行
git stash drop,加上你希望移除的储藏的名字:

git stash drop stash@{0}
Dropped stash@{0} (2bb9fe1993cf55843152025ae2bd79d5f7d8974c)

你也可以运行git stash pop 来重新应用储藏,同时立刻将其从堆栈中移走。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值