开发者

GIT - Difference between tracking a branch versus cloning

开发者 https://www.devze.com 2022-12-09 01:41 出处:网络
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 locations, what is the quickest a

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 locations, what is 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 repo). 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 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

开发者_JS百科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 <..>


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.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消