the correct way of git rebase

六眼飞鱼酱① 提交于 2019-12-11 08:48:27

问题


for make a rebase normaly I make

$ git checkout branchA
$ git rebase master 
$ git checkout master 
$ git merge branchA

ok.

my problem is with my fork from other repo, I add three commits and when I make

git pull --rebase otherRepo master 

get otherRepo commits and my commits go to HEAD of log, but when I try push

 ! [rejected]        HEAD -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:juanpabloaj/homebrew.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

with

 $ git push --force 

I can push my commits to my remote repo, but every time of pull is same thing,
which is the correct way to do it?


回答1:


git fetch otherRepo master
gitk --all

Do this and you'll be able to see the exact state of otherRepo/master. This will help you figure out what's going on. git fetch updates your view of otherRepo/master (so you can see its most recent state) but doesn't merge it with anything.




回答2:


Your problem is here:

git pull --rebase otherRepo master 

When you use git pull --rebase, your local commits on master are replayed on top of the new commits in the otherRepo's master. As a consequence of this, when you push to your origin, this becomes a non-fast-forward push (see here for an explaination of fast-forward merges). And so git disallows this by default.

The solution is simple, don't use --rebase with git pull when pulling from otherRepo:

git pull otherRepo master



回答3:


If you pull from a repo, then immediately try to push, and it fails with "non-fast-forward", then something else is going on. Maybe somebody else has pushed commits, or else maybe you're not pulling/pushing what you think you are. By forcing the push to proceed, you're destroying history, and unless you're perfectly clear about what you're doing, you might be losing work without realizing it.



来源:https://stackoverflow.com/questions/7163457/the-correct-way-of-git-rebase

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!