简体   繁体   中英

git: weird force push behavior

When I develop on a branch at some point before the Pull Request I do:

$> git checkout myBranch
$> git pull
$> git rebase origin/master
# fix conflicts and --continue
$> git push --force 
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 21, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1.59 KiB | 0 bytes/s, done.
Total 11 (delta 8), reused 0 (delta 0)
To git@github.com:bar/foo.git
+ 64f1387...ed6f9be myBranch -> myBranch (forced update)
+ ccaadf5...42e0c8d master -> master (forced update)

The problem here is the last line, a force push to master. This shouldn't have happend, I only wanted to force push to myBranch . But thats not all, the force push to master results in a master branch which now misses the last couple of commits. Which, I think, is weird, because I just did a pull . Can someone explain to me what just happend ? I think the cure is to do

$> git config --global push.default simple

and/or

$> git push --force origin/myBranch

About pushing all the branches, you are right, that is because of push.default=matching . From the docs of push.default :

matching - push all branches having the same name on both ends...

That may be surprising, and that's why the default is changing to simple .

About the master going back, that's what happens when you use --force ... you have to be very careful.

The problem is that you did git pull , that is equivalent to git fetch and git merge . The fetch synchronized all the origin/* branches, and the merge advanced your myBranch to origin/myBranch . But as it happened, master was not updated to origin/master (as it was not the current branch when doing the merge). So when you force-pushed, you rolled back origin/master !

The morale of the story is:

When using --force , never trust the defaults!

That's valid for many commands, not just git .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM