简体   繁体   中英

git: all local branches are behind the remote. How can I bring them all up to the remote state?

I've cloned a remote repository which has several branches, worked on one of those branches, committed the changes and pushed. After a time interval during which colleagues have modified the remote repository further (and during which I've possibly forgotten how to use git), I need to make more changes. I don't need to keep any local changes. First I try to bring my local repo up to date by doing

git fetch --all

then I try to switch to the required branch by

git checkout -f branchname

but git says

Your branch is behind 'origin/branchname' by 85 commits, and can be fast-forwarded.

So I take that to mean that my local branch is stuck somewhere behind the commit corresponding to the equivalent remote branch, despite doing a fetch that I hoped would make my repo equal to the remote one. What's the clearest and most straightforward way to do this? I could start with a new clone, but that seems a bit crude.

git checkout -f branchname ,执行git pull origin branchname

You just have to run :

git merge origin/branchname

Why ?

With git fetch --all , you've already retrieve all commits made by your colleagues. You just have to integrate them into your local branch. Since it says that your branch can be fast-forwarded, you simply have to run git merge origin/branchname .

Remainder : even if branchname and origin/branchname are linked together (we say that branchname tracks origin/branchname ), they are not the same. So your have to merge (or rebase) origin/branchname on branchname to have all commits in origin/branchname appears in branchname

I use git up for this. Apart from updating all your local branches, it will also stage and unstage any local changes you have. Saved me quite a few key strokes since I've been using it.

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