Getting existing git branches to track remote branches
GitGit Problem Overview
My usual workflow when working with git, is something like this:
- create a local repository
- do some work in that repository, add/change files etc.
- decide that I want a central remote location for the repository, and create one
- push all the commits from my local repository to this new remote repository
Now, however, I want to be able to push
and pull
from this remote repository without having to specify where I'm pushing to or pulling from; I want my local master to track the remote master.
The proper way to do this isn't clear to me, and I've been unable to determine it from the documentation, even though it shouldn't really be more than one command.
Because it's something that's only ever done once per repository, I've generally employed one of two simple, but hacky, solutions:
- used
git clone
to make a new local repository, and deleted the old one. After git cloning, the new repository is setup to track the origin. - manually edited .git/config to make master track origin.
I think I should be able to run a command, probably some form of git remote
to setup an existing repository to have master track a remote master. Can anyone tell me what that command is?
Git Solutions
Solution 1 - Git
Use the set-upstream arg:
git branch --set-upstream local-branch-name origin/remote-branch-name
Running the above command updates your .git/config file correctly and even verifies with this output:
"Branch local-branch-name set up to track remote branch remote-branch-name from origin."
EDIT: As [martijn][1] said: "In version Git v1.8.0, --set-upstream is deprecated. Use --set-upstream-to instead."
git branch --set-upstream-to local-branch-name origin/remote-branch-name
See this for more information. [1]: https://stackoverflow.com/users/951064/martijn
Solution 2 - Git
git help remote
should show you what you need to know. I think what you want is
git remote add [remote-name] [remote-url]
# Set a local branch to follow the remote
git config branch.[branch-name].remote [remote-name]
# Set it to automatically merge with a specific remote branch when you pull
git config branch.[branch-name].merge [remote-master]
You can also manually edit .git/config to set these up.
Solution 3 - Git
You can also use this if you want to create a new local branch to track a remote branch:
git checkout --track -b [branch_name] --track origin[or other remote name]/[remote_branch_name]
or even better:
git checkout -t origin/branch_name
Solution 4 - Git
On newer versions of git you can use
git branch --track origin/branch_name
Solution 5 - Git
The --set-upstream flag is deprecated and will be removed.
git branch master --set-upstream-to myupstream/master
Solution 6 - Git
Fast forward three years (see what I did there :-) ), I tried to pull an untracked branch using Git Bash and received
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> develop
The following achieved what I needed:
$ git branch --set-upstream-to=origin/develop develop Branch 'develop' set up to track remote branch 'develop' from 'origin'.
Solution 7 - Git
Use this command:
git clone [<options>] [--] <repo> [<dir>]