Sync all branches with git
I push code from two main locations: my PC at home and my laptop at work. I use Github to store my repos.
This is the scenario: I did some work in my PC on a repository I’ve been working on for some time (both in my PC and in my laptop), and ended with the following branches:
$ git branch * master * v123 * test-b
which I pushed to Github. So far so good.
Now I’m on my laptop and this is what I see before attempting to pull anything:
$ git branch * master * v_123
This is an old version of my repo in my laptop (since I’ve been working in my PC) where the differences are: a branch is missing (
test-b), another one has been re-named, or equivalently deleted and re-created with a new name (ie:
v_123 is now
v123), and lots of things have changed possibly in all branches.
I want to sync all my branches into my laptop and have them correctly tracked. I’ve looked at two of the most up-voted questions regarding branch cloning/fetching (How to clone all remote branches in Git?; How to fetch all git branches) and right now I’m a bit lost.
Is there some easy to use
git sync-branch --all command that can be used to sync my laptop with the latest state of the repo in Github?
2 Solutions collect form web for “Sync all branches with git”
Not sure, this is what you expect.
git fetch origin && git reset --hard origin/master && git clean -f -d
The above command will synchronize the remote repo with the local repo. After the above command execution, your local repo will be like the mirror image of your remote repo.
If you want to retain the changes as unstaged files, use
--soft instead of
IMPORTANT: All your untracked files will be gone when you do git clean -f -d. Please be aware of that.
Lemme know, if any questions.
You cannot see any changes made on remote before attempting to pull anything. You must fetch from remote first.
There is a reason for this. Git is decentralized. Every repository contains it’s full copy. It means that you may create a full clone from repository on your disk. And it also means that you can be working entirely offline, unless you command it to sync. Actually, the way git is made forces you to work almost entirely offline, not counting moments you push or fetch. On the other hand, you have full control and nothing happens without you explicitly saying so.
How to do it? That’s why
git pull exists. It performs two operations. First, it fetches all branches from remote (equivalent to
git fetch). They are saved as
<remote>/<branch> and not listed on branch list by default (override with
-a). Then, it merges currently active branch with it’s tracked branch.
Now, if you want to have a remote branch tracked, then simply checkout it after pull. It will create it from last synced version and should set everything up. If you want to set nonpublished local branch tracked on remote, use
git push with
-u flag. It will set upstream for the branch as push target.
So your goal is to update all local tracked branches at once, with one command? Not those
<remote>/<branch>, but your local branches, without calling pull on every single one of them, now I get it right?
Unfortunately, it’s impossible to do with git alone. But there is an extension that does exactly that. From project description
git-up – fetch and rebase all locally-tracked remote branches
You can find it here http://aanandprasad.com/git-up/
When you use it, with command
git up, it will stash your current changes, fetch all remotes, then rebase all tracked branches to state from tracked remote and unstash changes. I believe this is the way of synchronization you want.