Working with Branches
Listing Branches
To know a bit about the context of your repository, you can let the terminal show a list of all your branches.
Listing all local branches
To list all your local branches, you can use the command git branch
.
Listing all local and remote branches
If you want to see all branches, even the ones that exist in your remote repository (on GitHub), you can use the command git branch -a
.
Creating and Deleting Branches
When you want to work on a new functionality, it's good to do that in a separate branch. After the branch has been merged on to the master branch, you might want to delete it again to stop your repository from getting cluttered.
Creating a branch
To create a new branch, simply use the command git branch [new branch name]
.
Deleting a branch
To delete the branch, you can use the command git branch -d [branch name]
.
Deleting a remote branch
You can also delete a branch that is no longer in your local repository, but instead is in your remote repository. You can use the following command to delete remote branches:
git push origin --delete [branch name]
Renaming a local branch
If you made a spelling error or decide a different name might be better for a branch, you can change the name of a local branch by using the following command:
git branch -m [old branch name] [new branch name]
Checking Out Branches
You might want to switch branches to start working on something else or to just check out something. You can do this by checking it out.
Switching to a branch
Switching to a named branch
When you know the name of the branch you want to switch to, you can use the following command:
git checkout [branch name]
Last branch
If you want to switch back to the branch you last checked out, you can simply just use the following command:
git checkout -
Switching and creating a new branch
Locally
You can also kill two birds with one stone by creating a new branch and checking it out immediately. This can be done with the following command:
git checkout -b [branch name]
Cloning remotely
The same can be done with a remote branch you want to clone. Use the following command:
git checkout -b [branch name] origin/[branch name]