简体   繁体   中英

Update all local branches with all remote branches

How do I override all local branches with all remote branches?

Here are my local branches:

client-side-js-framework
client-side-js-framework-2
client-side-js-framework-3
handlebars
master

And my remote branches:

client-side-js-framework
handlebars
master
no-network-connection

I want my local branches to mirror my remote branches as my remote branches are more up to date. In other words, my local branches should be:

client-side-js-framework
handlebars
master
no-network-connection

Can this be done with one (or two) commands, rather than basically deleting my local repo, and re cloning it?

Most likely, you should run:

git fetch origin

and then simply delete (most or all of) the local branches. The next time you run, eg:

git checkout handlebars

your Git will search for your local name handlebars , fail to find it, search some more to see if there's exactly one name like origin/handlebars and upstream/handlebars and so on (one for each possible remote).

As long as there's exactly one such name, your Git will then say: Aha, I should create local name handlebars using the same commit as remote-tracking name origin/handlebars . In that instant you will get a local branch named handlebars that matches origin/handlebars , Git will check it out, and you will be ready to work. There's no need to drag half a dozen not-being-worked-on local names around like a set of balls-and-chains .

If you really do need each branch updated, you must use a loop, with one update command per branch to do an appropriate merge and push, unless they're all fast-forward not-really-a-merge operations, in which case:

git fetch origin 'refs/heads/*:refs/heads/*'

will work—but don't do it (see the ball and chain link).

(It's a little trickier if you do have multiple remotes that use the same names. In this case, git checkout --track origin/handlebars will achieve the desired result. Meanwhile it's also tricky, and unwise, to let git fetch delete local names, but I think it can be done with --prune .)

Basically git fetch origin will update your local branch as well as add extra branch into your local repo, which is available on remote.

Alternative solution is

1. git checkout client-side-js-framework
2. git pull

That's all :)

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