Reputation: 33926
Which one of these lines is correct?
git checkout 'another_branch'
or
git checkout origin 'another_branch'
or
git checkout origin/'another_branch'
And what is the difference between them?
Upvotes: 449
Views: 1155646
Reputation: 906
Switching to another branch in Git can be done with a single command.
git switch branch-name
Upvotes: 5
Reputation:
To switch to an branch with your changes, you should do a fetch first. This is to save the changes like your package.json or your .env files.
So:
git fetch
And then:
git checkout <new branch>
This answer is for those which where stuck for a while, like me.
Upvotes: 5
Reputation: 486
I am using this to switch one branch to another one you can use. It works for me like charm.
git switch [branchName]
or
git checkout [branchName]
Example:
git switch develop
or
git checkout develop
Upvotes: 3
Reputation: 799
What worked for me was the following:
Switch to the needed branch:
git checkout -b BranchName
And then I pulled the "master" by:
git pull origin master
Upvotes: 9
Reputation: 61
Check: git branch -a
If you are getting only one branch, then do below steps.
git config --list
git config --unset remote.origin.fetch
git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
Upvotes: 6
Reputation: 4920
Switching to another branch in Git. Straightforward answer,
git-checkout - Switch branches or restore working tree files
git fetch origin # <---- This will fetch the branch
git checkout branch_name # <--- Switching the branch
Before switching the branch, make sure you don't have any modified files. In that case, you can commit the changes or you can stash it.
Upvotes: 97
Reputation: 605
These are the steps I follow:
You can check the status and which branch you are on using:
Note: Here if you make changes in your local repo before moving to the new branch, the following steps should still work.
If "git branch" shows master, and you want to create+move to another branch:
Check branch again using "git branch" It should now show that you are in the new branch.
Now add, commit and push:
The above steps work for me in both the situation when I have made changes before moving to the new local branch or making changes after moving to the new branch. I hope it helps people running into similar situations and it is also the solution to the question mentioned here: Link
Upvotes: 0
Reputation: 517
Check remote branch list:
git branch -a
Switch to another Branch:
git checkout -b <local branch name> <Remote branch name>
Example: git checkout -b Dev_8.4 remotes/gerrit/Dev_8.4
Check local Branch list:
git branch
Update everything:
git pull
Upvotes: 1
Reputation: 121
If you want the branch to track the remote branch, which is very important if you're going to commit changes to the branch and pull changes etc, you need to add a -t
for the actual checkout like so:
git checkout -t branchname
Upvotes: 6
Reputation: 599
Useful commands to work in daily life:
git checkout -b "branchname" -> creates new branch
git branch -> lists all branches
git checkout "branchname" -> switches to your branch
git push origin "branchname" -> Pushes to your branch
git add */filename -> Stages *(All files) or by given file name
git commit -m "commit message" -> Commits staged files
git push -> Pushes to your current branch
If you want to merge to dev from feature branch, First check out dev branch with command "git branch dev/develop" Then enter merge commadn "git merge featurebranchname"
Upvotes: 53
Reputation: 30858
If another_branch
already exists locally and you are not on this branch, then git checkout another_branch
switches to the branch.
If another_branch
does not exist but origin/another_branch
does, then git checkout another_branch
is equivalent to git checkout -b another_branch origin/another_branch; git branch -u origin/another_branch
. That's to create another_branch
from origin/another_branch
and set origin/another_branch
as the upstream of another_branch
.
If neither exists, git checkout another_branch
returns error.
git checkout origin another_branch
returns error in most cases. If origin
is a revision and another_branch
is a file, then it checks out the file of that revision but most probably that's not what you expect. origin
is mostly used in git fetch
, git pull
and git push
as a remote, an alias of the url to the remote repository.
git checkout origin/another_branch
succeeds if origin/another_branch
exists. It leads to be in detached HEAD state, not on any branch. If you make new commits, the new commits are not reachable from any existing branches and none of the branches will be updated.
UPDATE:
As 2.23.0 has been released, with it we can also use git switch
to create and switch branches.
If foo
exists, try to switch to foo
:
git switch foo
If foo
does not exist and origin/foo
exists, try to create foo
from origin/foo
and then switch to foo
:
git switch -c foo origin/foo
# or simply
git switch foo
More generally, if foo
does not exist, try to create foo
from a known ref or commit and then switch to foo
:
git switch -c foo <ref>
git switch -c foo <commit>
If we maintain a repository in Gitlab and Github at the same time, the local repository may have two remotes, for example, origin
for Gitlab and github
for Github. In this case the repository has origin/foo
and github/foo
. git switch foo
will complain fatal: invalid reference: foo
, because it does not known from which ref, origin/foo
or github/foo
, to create foo
. We need to specify it with git switch -c foo origin/foo
or git switch -c foo github/foo
according to the need. If we want to create branches from both remote branches, it's better to use distinguishing names for the new branches:
git switch -c gitlab_foo origin/foo
git switch -c github_foo github/foo
If foo
exists, try to recreate/force-create foo
from (or reset foo
to) a known ref or commit and then switch to foo
:
git switch -C foo <ref>
git switch -C foo <commit>
which are equivalent to:
git switch foo
git reset [<ref>|<commit>] --hard
Try to switch to a detached HEAD of a known ref or commit:
git switch -d <ref>
git switch -d <commit>
If you just want to create a branch but not switch to it, use git branch
instead. Try to create a branch from a known ref or commit:
git branch foo <ref>
git branch foo <commit>
Upvotes: 457
Reputation: 483
With Git 2.23 onwards, one can use git switch <branch name>
to switch branches.
Upvotes: 20
Reputation: 356
[git checkout "branch_name"
]
is another way to say:
[git checkout -b branch_name origin/branch_name
]
in case "branch_name" exists only remotely.
[git checkout -b branch_name origin/branch_name
] is useful in case you have multiple remotes.
Regarding [git checkout origin 'another_branch'
] I'm not sure this is possible, AFAK you can do this using "fetch" command
-- [git fetch origin 'another_branch'
]
Upvotes: 23