tags:

views:

248

answers:

3

I've seen this command floating around on various sites.

git checkout --track -b <...>

If I create a bare repo on a remote server and work from two different location. What's the quickest and "approved" way of doing so?

What I did was, I created the initial repo on my laptop and then pushed the changes to the "origin" where my VPS repo is (the bare). Now, on my desktop, should I be cloning my repo? I ask because I have two branches, "dev" and "master." Once I'm on my desktop, I wasn't sure if I should be "tracking" the repo (see above) or should I be cloning first? What if I wanted to work on the dev branch, is that when I checkout using the --track directive?

Here's what I've done so far.

On laptop

cd devproject
git init
git add .
git commit -m "My first commit"

On VPS Repo

mkdir /home/sam/devproject.git
cd /home/sam/devproject.git
git --bare init
exit

Back to laptop

cd devproject
git remote add origin ssh://myserver.com/home/sam/devproject.git

On Desktop (??)

git clone <..>
+6  A: 

You clone a repository, but you track a branch. The checkout command you posted is not complete:

git checkout --track -b new_local_branch_name origin/remote_branch_name

Thus the required steps would be:

  1. Clone the remote repository.
  2. Track the remote branches.
innaM
Right, I removed it on purpose. So, if I have 2 branches (master/dev) I have to track both? Everytime I clone a repo, I have to track the branches. Then make my changes to my clone and git push/pull accordingly. Yes?
luckytaxi
Basically, yes. But you don't have to explicitly track the master branch.
innaM
To clarify Manni's comment, the repository automatically sets up the master branch for tracking when it is cloned. (To be precise, it sets up the initially checked-out branch, which is the HEAD on the remote repo you cloned from, but this is generally master.)
Jefromi
Thanks for the clarification.
innaM
+3  A: 

The command above will not work if you're not in a repository. To work with git, you must always first create a repository, either by cloning one which already exists or using git-init and starting from scratch.

git checkout --track -b <branch> <remote-branch>
git checkout --track <remote-branch>

These two commands create a new local branch to track <remote-branch>. The first one manually names it <branch>; the second uses the same name as the remote.

Remember that tracking doesn't mean automatic update - it simply does things like specifying where the branch should push/pull from and letting git status give those "your branch is behind origin/master by 5 commits, and can be fast-forwarded" messages.

Jefromi
So on my desktop, do I run git init . (it'll be empty) and then run git checkout? Or should I be cloning?
luckytaxi
Do you already have the desired content in a repo somewhere? Clone it. Are you creating it for the first time? Use git init. Again, git-checkout merely checks out (and possibly creates) some content which is already in the repository.
Jefromi
Yes, let me update my the main message thread.
luckytaxi
Either way, the point of my answer was that `git-init` and `git-clone` are the two ways to create a repository, and to describe the function of `git checkout --track`.
Jefromi
A: 

When you use

git checkout --track -b local_branch_name origin/remote_branch_name

(usually with 'local_branch_name' the same as 'remote_branch_name', for which shortcut exists:
"git checkout -b --track origin/branch_name"), it means that you create local branch named 'local_branch_name', on which you can create commits, for which the upstream branch would be remote-tracking branch named 'remote_branch_name' (which tracks/follows this remote-tracking branch).

You need to remember that you can't commit directly on 'origin/remote_branch_name'; this remote-tracking branch is meant to track progress of 'remote_branch_name' branch in remote 'origin' ('origin' is default name of remote you cloned from).

Jakub Narębski
With that said, should I be doing this as well from my laptop (where it all started)? I'm not understanding what the difference would be on my desktop as supposed to my laptop. On my laptop, I created the local repo and then pushed it to my bare repo on a remote server.Are you saying I should be "tracking" the repo on my laptop from the bare repo?
luckytaxi
Err... although Git documentation uses "tracking" to refer to two different concepts in its documentation (remote-tracking branches, and branch tracking some other branch), it doesn't use this word when describing relations between repositories. One repository can be clone of other repository. You can fetch from some repository, you can push to some repository.
Jakub Narębski