tags:

views:

934

answers:

2

My usual workflow when working with git, is something like this:

  1. create a local repository
  2. do some work in that repository, add/change files etc.
  3. decide that I want a central remote location for the repository, and create one
  4. 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:

  1. 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.
  2. 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?

+12  A: 

git help remote should show you what you need to know. I think what you want is

git remote add [remote-name] [remote-url]
git config branch.[branch-name].remote [remote-name] # Set a local branch to follow the remote
git config branch.[branch-name].merge [remote-master] # Set it to automatically merge with a specific remote branch when you pull

You can also manually edit .git/config to set these up.

Mike
Another option is to make your central repository, then delete your working directory and recreate it by cloning from the central repository. Editing .git/config will be more informative, though.
William Pursell
cloning the central repository and editing .git/config are the two options I already mentioned in my question.
SpoonMeiser
+1  A: 

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
bcolfer
git checkout --track -b branchname origin branchnamefatal: git checkout: updating paths is incompatible with switching branches.Did you intend to checkout 'origin/branchname' which can not be resolved as commit?
edebill