有了前面两篇的git操做学习教程: git场景操做学习教程(一) 和 git场景操做学习教程(二),这里咱们再推出第三篇场景学习教程。html
本文同步发表在豆米的博客,若是喜欢,欢迎关注。git
你发现这个commit是错的,想要撤回,那么如何操做呢?bash
咱们将这种状况分为两种:xss
git update-ref -d HEAD
git reset commit_id
git revert -m 1 commit-id
git branch -c feature/test
来新建一个分支git push
操做执行第二步的时候发现问题,feature/test
分支的upstream
不是origin/feature/test
,而是以前fork出来的分支master
,这是为啥?怎么修复这个问题?并且在push的时候报下面的这个提示:学习
而咱们指望的提示应该是这样的:ui
fatal: The current branch feature/test1 has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/test1
复制代码
首先执行这个命令查看当前git的配置:git config --list
spa
关注红圈圈的两个配置,这俩配置就是致使问题的缘由3d
查看当前分支的关联分支: git branch -vv
code
能够看到新建的分支默认的关联分支错乱了。cdn
修改配置以下:
git config --global branch.autoSetupMerge true
git config --global push.default upstream
复制代码
便可恢复到以前的提示状态。那么接下来讲一下这俩配置吧。
该属性决定了当执行git branch
或git checkout
的时候如何设置新分支以便执行git pull
的时候能够恰当地和起始点分支进行合并。即便该选项没有设置,该行为也能够在每一个分支上使用--track
和--no-track
进行设置。该配置有如下几个可选值:false/true/always
false: 不会自动设置新分支
true: 当以一条远程分支为起始分支进行新建分支的时候,会自动设置新分支
always: 不管起始分支是本地的分支仍是远程的分支,都会自动设置该新分支
复制代码
怎么解释这些配置呢?举个例子:
假设当前本地有master分支,远程有master(origin/master)
分支和feature/B(origin/feature/B)
分支:
1. 当设置为false的时候,不管新建的分支是否在远端存在,都不会设置该分支的track,也就是你查看分支的upstream的时候是这样的:
❯ git checkout feature/B
Switched to a new branch 'feature/B'
❯ git branch -vv
* feature/B 7255919 feat(B): 添加featureBB文件
master 1ab9353 [origin/master] fixssssss
复制代码
从上图打印能够看出,虽然不会设置feature/B
,可是确实是以feature/B
这个远端分支进行fork的。
2. 当设置为true的时候,若是新建的分支在远端存在,那么自动设置该分支的upstream为远端分支,若是不存在,就不设置,以下:
❯ git checkout feature/B
Branch 'feature/B' set up to track remote branch 'feature/B' from 'origin'.
Switched to a new branch 'feature/B'
❯ git branch -vv
* feature/B 7255919 [origin/feature/B] feat(B): 添加featureBB文件
master 1ab9353 [origin/master] fixssssss
❯ git checkout -b feature/A
Switched to a new branch 'feature/A'
❯ git branch -vv
* feature/A 7255919 feat(B): 添加featureBB文件
feature/B 7255919 [origin/feature/B] feat(B): 添加featureBB文件
master 1ab9353 [origin/master] fixssssss
复制代码
能够看到切换到新分支的时候,若是新分支在远端存在,那么会自动track该分支的upstream为远端分支。
可是若是切换到的新分支远端和本地都不存在,那么该新分支不会自动设置
3. 当设置为always的时候,以下:
❯ git checkout feature/B
Branch 'feature/B' set up to track remote branch 'feature/B' from 'origin'.
Switched to a new branch 'feature/B'
❯ git branch -vv
* feature/B 7255919 [origin/feature/B] feat(B): 添加featureBB文件
master 1ab9353 [origin/master] fixssssss
❯ gc -b feature/A
Branch 'feature/A' set up to track local branch 'master'.
Switched to a new branch 'feature/A'
❯ git branch -vv
* feature/A 1ab9353 [master] fixssssss
feature/B 7255919 [origin/feature/B] feat(B): 添加featureBB文件
master 1ab9353 [origin/master] fixssssss
❯ git branch feature/B
Branch 'feature/B' set up to track local branch 'master'.
❯ git branch -c feature/X
❯ git branch -vv
feature/B 1ab9353 [master] fixssssss
* feature/X 1ab9353 [origin/master] fixssssss
master 1ab9353 [origin/master] fixssssss
复制代码
设置为always的时候,若是新分支在远端有的话,那么自动track,若是没有那就自动track到你当前所处的分支。因此你们能够看到feature/B和feature/A的upstream分别是远端的和本地的master。
另外使用git branch
的时候,也会这样,由于等价于git checkout -b
。这里有一个小小的知识点:
当使用git branch -c的时候,由于-c是copy的含义,因此feature/X是整个复制了master分支,因而连master的upstream都是复制过来的,因此这个时候feature/X的upstream是随master同样的,都是origin/master
最后,我的倾向的配置是true
,这样提交代码的时候不会乱设置upstream。
该属性决定了git push
操做的默认行为。 push.default
有如下几个可选值: nothing, current, upstream, simple, matching
nothing: 直接push会出错,须要显式的指出推送的远程分支,例如:git push origin <remote_branch>;
current: 推送时只会推送当前所在的分支到远程同名分支,若是远程分支不存在相应的同名分支,则建立该分支;
upstream: 推送当前分支到它的upstream分支上,这个模式只适用于推送到与拉取数据相同的仓库(好比central workflow);
simple(默认): simple和upstream是类似的,只有一点不一样,simple必须保证本地分支和它的远程 upstream分支同名,不然会拒绝push操做。
matching:推送本地和远程都存在的同名分支。
复制代码
这个我的倾向的配置是upstream
,这样push的时候都是push到本身本仓库的远端。