What happens when I pull from a different branch while I am on the master branch

时间:2018-07-25 05:16:52

标签: git

1) In local repo, I created and switched to a branch postacl by running git checkout -b postacl


2) Then I ran git push -u origin postacl


3) From the remote repo, while I was still on the master branch there, I accidentally ran git pull origin postacl. Then I realized that I did not switch to postacl.


What happened due to the above-mentioned actions?

3 个答案:

答案 0 :(得分:1)

In the general case, most likely, you just added a merge commit on top of your local master branch. You may verify this by running git log from your master branch. You would probably see a new merge commit, with a commit message mentioning something about a merge. If you don't see any merge commit, then you need not do anything. If you do see one, then keep reading.

In this particular case, you should be safe just doing a hard reset to remove the merge commit:

git reset --hard HEAD~1

This should nuke that accidental merge commit, leaving your local master as it was. You could also try resetting your local master to the tracking branch:

git reset --hard origin/master

答案 1 :(得分:1)

git pull means:

  1. Run git fetch, passing (most of) any additional arguments on to git fetch
  2. Run git merge (default—you can change this), merging the commit(s) brought in via step 1

So this meant:

git fetch origin postacl

which had your Git call up the Git that you call origin and obtain from them their latest commits on their postacl branch. Then, your Git ran:

git merge --edit -m <message> <commit-hash-ID>

where the <message> part is the string:

Merge branch 'postacl' of <url> [into <branch>]

(using the <url> stored with your name origin), and the <commit-hash-ID> is whatever commit hash ID their Git told your Git that their postacl branch names. (The into <branch> part appears if and only if you are on some branch other than master.)

This is almost the same as if you ran:

git fetch origin && git merge origin/postacl

except that the default merge message in this case would be slightly different.

Note that if you did all this while logged in to some other machine ("the remote repo", as you put it), "your" Git is the one on that remote machine, and "their" Git is the one that the remote machine's Git has set as its origin.

答案 2 :(得分:1)

首先,您只是从主数据库创建了一个新分支(假设)。

第二,您未做任何更改,而是使用-u更改了分支以跟踪更改,因此,每当您要使用较少的参数git pull时,它将考虑postacl的头像表示用作FETCH_HEAD

第三步,它将显示以下内容:

From <url>:<org>/<branch>
 * branch              <tracking_upstream_branch>     -> FETCH_HEAD
Already up to date.