Switch branches. By passing in the -b option when executing the checkout command, a new branch will be created and you will be switched over thereafter. $ git checkout -b branch Once you are on the 'issue1' branch, you can start adding commits to it. Let's add the bold text below to myfile.txt and commit the change. Instead of copying files from directory to directory, Git stores a branch as a reference to a commit. In this sense, a branch represents the tip of a series of commits—it's not a container for commits. The history for a branch is extrapolated through the commit relationships.
This page is an examination of the
git checkout
command. It will cover usage examples and edge cases. In Git terms, a 'checkout' is the act of switching between different versions of a target entity. The git checkout
command operates upon three distinct entities: files, commits, and branches. In addition to the definition of 'checkout' the phrase 'checking out' is commonly used to imply the act of executing the git checkout
command. In the Undoing Changes topic, we saw how git checkout
can be used to view old commits. The focus for the majority of this document will be checkout operations on branches.Checking out branches is similar to checking out old commits and files in that the working directory is updated to match the selected branch/revision; however, new changes are saved in the project history—that is, it’s not a read-only operation.
Checking out branches
The
git checkout
command lets you navigate between the branches created by git branch
. Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch. Think of it as a way to select which line of development you’re working on.Having a dedicated branch for each new feature is a dramatic shift from a traditional SVN workflow. It makes it ridiculously easy to try new experiments without the fear of destroying existing functionality, and it makes it possible to work on many unrelated features at the same time. In addition, branches also facilitate several collaborative workflows.
The
git checkout
command may occasionally be confused with git clone
. The difference between the two commands is that clone works to fetch code from a remote repository, alternatively checkout works to switch between versions of code already on the local system.Usage: Existing branches
Assuming the repo you're working in contains pre-existing branches, you can switch between these branches using
git checkout
. To find out what branches are available and what the current branch name is, execute git branch
.The above example demonstrates how to view a list of available branches by executing the
git branch
command, and switch to a specified branch, in this case, the feature_inprogress_branch
.New Branches
Git checkout
works hand-in-hand with git branch
. The git branch
command can be used to create a new branch. When you want to start a new feature, you create a new branch off master
using git branch new_branch
. Once created you can then use git checkout new_branch
to switch to that branch. Additionally, The git checkout
command accepts a -b
argument that acts as a convenience method which will create the new branch and immediately switch to it. You can work on multiple features in a single repository by switching between them with git checkout
.The above example simultaneously creates and checks out
<new-branch>
. The -b
option is a convenience flag that tells Git to run git branch <new-branch>
before running git checkout <new-branch>
.By default
git checkout -b
will base the new-branch
off the current HEAD
. An optional additional branch parameter can be passed to git checkout
. In the above example, <existing-branch>
is passed which then bases new-branch
off of existing-branch
instead of the current HEAD
.Switching Branches
Switching branches is a straightforward operation. Executing the following will point
HEAD
to the tip of <branchname>.
Git tracks a history of checkout operations in the reflog. You can execute
git reflog
to view the history.Git Checkout a Remote Branch
When collaborating with a team it is common to utilize remote repositories. These repositories may be hosted and shared or they may be another colleague's local copy. Each remote repository will contain its own set of branches. In order to checkout a remote branch you have to first fetch the contents of the branch.
In modern versions of Git, you can then checkout the remote branch like a local branch.
Older versions of Git require the creation of a new branch based on the
remote
.Additionally you can checkout a new local branch and reset it to the remote branches last commit.
Detached HEADS
Now that we’ve seen the three main uses of
git checkout
on branches, it's important to discuss the “detached HEAD”
state. Remember that the HEAD
is Git’s way of referring to the current snapshot. Internally, the git checkout
command simply updates the HEAD
to point to either the specified branch or commit. When it points to a branch, Git doesn't complain, but when you check out a commit, it switches into a “detached HEAD”
state.This is a warning telling you that everything you’re doing is “detached” from the rest of your project’s development. If you were to start developing a feature while in a detached
HEAD
state, there would be no branch allowing you to get back to it. When you inevitably check out another branch (e.g., to merge your feature in), there would be no way to reference your feature:The point is, your development should always take place on a branch—never on a detached
HEAD
. This makes sure you always have a reference to your new commits. However, if you’re just looking at an old commit, it doesn’t really matter if you’re in a detached HEAD
state or not.Summary
This page focused on usage of the
git checkout
command when changing branches. In summation, git checkout
, when used on branches, alters the target of the HEAD
ref. It can be used to create branches, switch branches, and checkout remote branches. The git checkout
command is an essential tool for standard Git operation. It is a counterpart to git merge
. The git checkout
and git merge
commands are critical tools to enabling git workflows
.Ready to try branching?
Try this interactive tutorial.
Next up:
Git Merge
Start next tutorial gistfile1.sh
git branch -m old_branch new_branch # Rename branch locally |
git push origin :old_branch # Delete the old branch |
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote |
commented Dec 15, 2014
Mahalo. |
commented Dec 16, 2014
On the first step, it also works to rename the currently checked out branch, per https://www.atlassian.com/git/tutorials/using-branches/git-branch. |
commented Jan 30, 2015
cool!!!!!!!!!!!!! thank's a lot |
commented Mar 31, 2015
commented Apr 26, 2015
Remember to pull before! Or you may lose every commit not yet on your local branch |
commented May 5, 2015
For some reason my version of git (v2.3.2 on osx) keeps reusing the same remote branch name whether using --set-upstream or -u. You could of course edit the gitconfig file but if this is happening to you, try using git branch --unset-upstream before doing the final push. |
commented Jun 22, 2015
Nice one, ty! |
commented Jun 23, 2015
Good tip! |
commented Jun 26, 2015
Thank you!! |
commented Jun 26, 2015
Thank you! |
commented Jul 6, 2015
Thank you! |
commented Jul 7, 2015
commented Jul 10, 2015
Thanks. |
commented Jul 14, 2015
+100 |
commented Jul 26, 2015
+1 |
commented Jul 28, 2015
Of great help, thanks a lot. |
commented Jul 28, 2015
Perfect! |
commented Aug 6, 2015
Much appreciated. |
commented Aug 19, 2015
+1 |
commented Aug 23, 2015
Great! |
commented Aug 25, 2015
Thanks but is there a way to rename a branch remotely without deleting it? As example if you are having an open Pull Request? |
commented Aug 28, 2015
I don't think there's any way to do a rename. I'm guessing this is because in plain git there's no difference between renaming and deleting then creating a new branch. It's only with pull-requests that you start to have problems, so Github would need to build the feature themselves. If you've accidentally made a pull request from your master and want to keep using it as a master branch, you can always make a new branch, change the default in the repository settings, then use that one. |
commented Sep 9, 2015
to the author: its people like you that make the internet great! |
commented Sep 11, 2015
1+ this works like charm. |
commented Sep 14, 2015
+1 |
commented Sep 18, 2015
Noiceeee |
commented Sep 21, 2015
+1 |
commented Sep 23, 2015
Thanks ! |
commented Sep 23, 2015
Thanks, you made my day! |
commented Sep 24, 2015
Awesome!! |
commented Sep 29, 2015
Thanks a lot. |
commented Oct 12, 2015
It's working! But for me, I need to run as @chrisivens said. |
commented Oct 15, 2015
thanks for this! |
commented Oct 22, 2015
Awesome, thanks! |
commented Oct 31, 2015
Thanks a lot! |
commented Nov 4, 2015
works like a nah !!! |
commented Nov 21, 2015
The original git push origin :old_branch syntax doesn't work for me on OS X with a recent git (version 2.6.2). For anyone running into this, the new syntax is: |
commented Nov 25, 2015
+1 |
commented Nov 25, 2015
@markmevans, it worked on my El Capitan with git 2.6.3. The old syntax that is. |
commented Dec 3, 2015
@markmevans, also works on Slackware with git 2.6.3 . |
commented Dec 7, 2015
thanks |
commented Dec 10, 2015
Great! |
commented Dec 15, 2015
@markmevans the :old_branch syntax worked perfectly on Mavericks with git 2.6.3 |
commented Dec 17, 2015
This is absolutely incredible! |
commented Jan 16, 2016
+1 |
commented Jan 16, 2016
Thank you so much! |
commented Jan 27, 2016
Many thanks, very useful. |
commented Jan 27, 2016
Thanks! |
commented Jan 27, 2016
Thanks! Worked like a charm |
commented Feb 2, 2016
Thanks! |
commented Feb 3, 2016
Thanks. |
commented Feb 3, 2016
+1 |
commented Feb 4, 2016
Thanks..!! |
commented Feb 16, 2016
Thanks so much! |
commented Feb 24, 2016
Thanks! |
commented Feb 29, 2016
Thank you! |
commented Mar 5, 2016
+1 |
commented Mar 5, 2016
commented Mar 17, 2016
:-) |
commented Mar 21, 2016
Thanks. Works like a charm. |
commented Mar 25, 2016
commented Mar 30, 2016
'git branch -m new_branch' --> this one worked. But both the branches will actually exist in git. I think this command just creates a copy of the branch with new name. |
commented Apr 14, 2016
commented Apr 27, 2016
Thanks |
commented Apr 27, 2016
git push origin :oldbranch
|
commented May 2, 2016
Can we do like this?. git push -u origin :oldbranch newbranch |
commented May 5, 2016
Cool, worked like a charm |
commented May 11, 2016
I actually would remove the remote branch after pushing the new local one in order to make sure we don't lose the data. If the disk drive gets corrupted between the current command 2 and 3, the data is lost. By inverting them, that case is prevented. |
commented May 18, 2016 • edited
edited
## If you are using SourceTree.
|
commented May 25, 2016
Brilliant, thanks! |
commented May 25, 2016
Excellent. thanks. |
commented Jun 13, 2016
@astromahi You cant delete master. You have to set a new default branch in the project settings first. |
commented Jun 14, 2016
It's very useful. Thank you. |
commented Jun 17, 2016
Thank you! |
commented Jun 29, 2016
Thank you |
commented Jun 30, 2016
commented Jul 4, 2016
thank you |
commented Jul 8, 2016 • edited
edited
commented Jul 11, 2016
commented Jul 13, 2016
commented Jul 29, 2016
thanks |
commented Aug 10, 2016
dope |
commented Aug 11, 2016
cool...thanks |
commented Aug 22, 2016
Thanks. |
commented Aug 22, 2016
+1 |
commented Aug 23, 2016
Thanks! |
commented Aug 26, 2016
Nice, thanks! |
commented Aug 29, 2016
+1 |
commented Aug 30, 2016
thank you! |
commented Sep 7, 2016
Thanks! :) |
commented Sep 12, 2016
It worked, Thanks a lot |
commented Sep 25, 2016
Can't believe it was so simple. Thanks! |
commented Sep 28, 2016
Thanks |
commented Oct 6, 2016
Great, thanks. |
commented Oct 6, 2016
Is recommended that if you have a pull request wait to rename, until is accepted. |
commented Oct 13, 2016
commented Oct 14, 2016 • edited
edited
I start branch names with the PR number I am addressing (keep things organized and readying myself for waffle bot). In this case it was #520-application-fee-refunds . I kept getting an error of fatal: branch name required when executinggit branch -m #520-application-fee-refunds #509-application-fee-refunds Simple fix was to wrap quotes around the branch names git branch -m '#520-application-fee-refunds' '#509-application-fee-refunds' Also, when you are pushing the branch, you will need the quotes too: git push --set-upstream origin '#509-application-fee-refunds' Perhaps common sense for some, but hope it can help someone. |
commented Oct 17, 2016
commented Nov 9, 2016 • edited
edited
commented Nov 10, 2016 • edited
edited
commented Nov 18, 2016
great staff man!! |
commented Nov 24, 2016
You should reverse the last two steps and do:
otherwise you end up with potentially losing all your work between the delete and write:
|
commented Dec 5, 2016
It's also useful to use rebase, and use the reword message to change commit message https://help.github.com/articles/changing-a-commit-message/ |
commented Dec 5, 2016
Thank you very much. What if there is an open PR in the old branch. would that point to the new branch or get deleted along the old branch? |
commented Dec 8, 2016
commented Dec 15, 2016
it's not rename, it's delete and create new |
commented Dec 16, 2016
Thaaaanks!!! The most useful link for today)) |
commented Dec 16, 2016
commented Dec 17, 2016
+1.41 |
commented Dec 21, 2016
Great..It worked for me |
commented Dec 21, 2016
Thank you very much |
commented Dec 23, 2016
One of the most useful tricks. |
commented Jan 8, 2017
Thank you! |
commented Jan 16, 2017
commented Jan 25, 2017
Thanks! |
commented Jan 26, 2017
Thanks |
commented Jan 30, 2017
+1 |
commented Feb 6, 2017
thanks |
commented Feb 8, 2017
+1 |
commented Feb 10, 2017
thanks a lot! |
commented Feb 27, 2017
Thanks |
commented Mar 6, 2017
Simple yet useful! Thanks a lot! |
commented Mar 8, 2017
Thanks! |
commented Mar 15, 2017
+1 |
commented Mar 17, 2017
Thanks. |
commented Mar 20, 2017
Goog job ! |
commented Mar 24, 2017
Thank you! |
commented Apr 6, 2017
Thank you kind person.... kind soul. Accurate information is where it's at. So much better than that inaccurate information junk. |
commented Apr 9, 2017
Thank you! |
commented Apr 30, 2017
Thanks!!! Saved me time |
commented May 5, 2017
Thanks!!! Saved me life |
commented May 17, 2017
Thank you! |
commented Jun 2, 2017
Say yeah |
commented Jun 12, 2017
Thank you! |
commented Jun 17, 2017
Thanks! |
commented Jun 22, 2017
commented Jul 1, 2017
Thanks 1+ |
commented Jul 5, 2017
Google patiently keeps leading me back here, thank you! :D |
commented Jul 5, 2017
This doesn't entirely work; it renames locally but maintains a mapping to the old github branch name such that it still recreates the branch on github with the old name. |
commented Jul 12, 2017 • edited
edited
@lttlrck please update your gist. If the upstream is still set, it will be set to track again the remote with the original name. git version 2.11 on windows if it's relevant |
commented Jul 13, 2017
@lmurta05 Working as described. Thank you very much. |
commented Jul 14, 2017
Such an excellent and concise solution. I've googled for this and come back to it more than ten times now. Thank you. |
commented Jul 15, 2017
Thanks +1 |
commented Aug 17, 2017
It works, thank you! |
commented Aug 28, 2017
@lttlrck be careful. First of all make sure the new branch is created, then delete the old branch. This will ensure everything is fine even if something crashes between creating the new branch and deleting the old one. |
commented Sep 6, 2017
Thx! |
commented Oct 21, 2017
It works.Thanks! |
commented Oct 25, 2017
@lmurta05 Thanks a lot! |
commented Oct 31, 2017
Thanks a lot |
commented Nov 9, 2017
commented Nov 9, 2017
Awesome script! |
commented Nov 20, 2017
commented Nov 29, 2017 • edited
edited
commented Dec 8, 2017
wow, thanks a lot! Saved my life ;p |
commented Dec 13, 2017
Thanks a lot ! This is clean. |
commented Dec 14, 2017
Thanks! |
commented Dec 19, 2017 • edited
edited
I think you should add git checkout old_branch as the first step to make sure the local tracking branch actually exists. If you don't do it you may get the following error: |
commented Jan 11, 2018
Thanks a billion! |
commented Jan 16, 2018
Thank you !! |
commented Jan 16, 2018
Thanks! |
commented Jan 29, 2018
thankssssssss |
commented Feb 8, 2018
tysm |
commented Feb 9, 2018
thanks a lot |
commented Feb 15, 2018
thank you! |
commented Feb 17, 2018 • edited
edited
commented Mar 8, 2018
thank you! |
commented Mar 12, 2018
Thank u |
commented Mar 15, 2018
thank you |
commented Mar 15, 2018
Thank You! |
commented Mar 15, 2018
thanks man! |
commented Mar 19, 2018
Delete the old branch git push origin --delete :old_branch |
commented Mar 29, 2018
@TorvaldsDB good catch |
commented Apr 4, 2018
You can combine the push operations : git branch -m old_branch new_branch # Rename branch locallygit push origin :old_branch new_branch # Delete the old branch and push the new one |
commented Apr 11, 2018
This will only work if the :oldbranch is not the active branch of the remote repo. If it is, and you have write access to the repo (such as through a server), you can do git symbolic-ref HEAD refs/heads/newbranch , and then delete the repo: http://feeding.cloud.geek.nz/2008/12/setting-default-git-branch-in-bare.htmlIf the remote branch is a Github repo, you wont be able to do this. So you will have to change it through the Github interface: https://stackoverflow.com/a/2962737/4752883 |
commented Apr 26, 2018
FWIW you can use an alias to make it simpler. In .gitconfig :Then simply run git rename old_branch new_branch , and it's done. |
commented Apr 27, 2018
Thank you! |
commented May 1, 2018
Thank you. This worked perfectly. I ran the 3 steps at top of this gist. |
commented May 11, 2018
Rename a local and remote branch in git this article describe details about git branch rename locally and remotely |
commented May 17, 2018
Thank a lot!! |
commented Jun 7, 2018
we @ALL are crank here |
commented Jun 20, 2018
thanks man! |
commented Jun 20, 2018
Cheers! |
commented Jun 26, 2018
Thanks! |
commented Jun 28, 2018
Very helpful |
commented Jul 6, 2018
Thank you! |
commented Jul 13, 2018
thx! |
commented Jul 17, 2018
+1 |
commented Jul 22, 2018
+1 |
commented Aug 3, 2018
thanks alot |
commented Aug 15, 2018
commented Sep 19, 2018
Awesome, thank you =) |
commented Sep 27, 2018
+1 |
commented Oct 3, 2018
commented Oct 24, 2018
Nice! I especially like @ffoodd 's alias. |
commented Oct 26, 2018
thanks a lot |
commented Nov 22, 2018
awesome, very helpful |
commented Dec 5, 2018 • edited
edited
git config push.default upstream It's more convenient to use with it |
commented Dec 12, 2018 • edited
edited
commented Dec 25, 2018
commented Dec 26, 2018
Cool! Thanks. ) |
commented Dec 27, 2018
Cool, thank you! |
commented Jan 22, 2019
My local branch was still tracking the old_branch on the remote. This command works to solve that. |
commented Feb 13, 2019
Thanks. It's by far the most concise, clear, and easy to understand and use answer to 'how to rename my branch, both local and remote' I've seen so far. Some sites sport elaborate explanations, and some even got it dead wrong... but this one is closest to perfect for vast majority of situations. |
commented Feb 13, 2019
awesome |
commented Feb 28, 2019
worked like a charm, thanks! |
commented Mar 6, 2019
Thank you! |
commented Mar 8, 2019
You still deserve more 'Thank Yous'. |
commented Mar 13, 2019
Thankyou! Cheers |
commented Mar 13, 2019
Thank you very much :) |
commented Mar 14, 2019
Thanks !!! |
commented May 7, 2019
After all this time, I bookmark this and I literally search on 'mahalo' to help me find this again and again :) |
commented Jun 25, 2019
A very useful gist indeed, thank you. |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment