I cloned a git repo and it had, among many others, these two remote branches:
When I cloned it, I was on master. I wanted to switch to the foo-bar
branch, ie I wanted to get the code from the remote origin/foo-bar
branch into a local branch of the name foo-bar
, so I did:
$ git checkout foo-bar
Note that I wasn't sure if I was to say git checkout origin/foo-bar
or just git checkout foo-bar
.
Now, I am wondering which of the two did I end up doing?
Did I just create a new local branch named foo=bar
with all the contents of the master
branch? OR
Did I create a new branch named foo-bar
and automatically pull all contents from the remote origin/foo-bar
into it?
If I did the wrong thing, ie I actually wanted to do #2 but if I did something else, how do I delete this local branch named foo-bar
?
Note that I wasn't sure if I was to say
git checkout origin/foo-bar
or justgit checkout foo-bar
.
Both of these are valid, but they mean different things. Tl;dr, you wanted the latter in this case.
origin/foo-bar
(where origin
in this case is the name of a remote) is a so-called remote tracking branch. Remote tracking branches are read-only and cannot be checked out, so git checkout origin/foo-bar
will detach HEAD
and set it to the commit that origin/foo-bar
refers to.
git checkout foo-bar
checks out the branch foo-bar
if it exists, or it creates it anew as described below.
- Did I just create a new local branch named
foo-bar
with all the contents of themaster
branch?
If your version of Git is not ancient, by default git checkout foo-bar
will create branch foo-bar
and set it up to track in your case origin/foo-bar
.
This behaviour is documented in the man page for git-checkout .
- Did I create a new branch named foo-bar and automatically pull all contents from the remote
origin/foo-bar
into it?
See 1.
- If I did the wrong thing, ie I actually wanted to do #2 but if I did something else, how do I delete this local branch named foo-bar?
To delete a branch, check out a different branch (eg git checkout master
) and then do git branch -d foo-bar
.
See man page for git-branch .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.