0% found this document useful (0 votes)
3 views2 pages

Working With Branches in Git Cheat Sheet

The document provides a comprehensive guide on working with branches in Git, including creating, renaming, switching, publishing, and deleting branches. It also covers tracking branches, comparing branches, merging, and rebasing, offering essential commands for each task. The guide emphasizes best practices such as meaningful branch naming and periodic cleanup of obsolete branches.

Uploaded by

Max
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Working With Branches in Git Cheat Sheet

The document provides a comprehensive guide on working with branches in Git, including creating, renaming, switching, publishing, and deleting branches. It also covers tracking branches, comparing branches, merging, and rebasing, offering essential commands for each task. The guide emphasizes best practices such as meaningful branch naming and periodic cleanup of obsolete branches.

Uploaded by

Max
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

WORKING WITH BRANCHES IN GIT

presented by
TOWER — the best Git client for Mac and Windows

Creating a New Branch


This is where the fun starts. Simply type this command to If you’d like to rename a different branch, you should first
create a new branch, so that you can work on a feature or fix insert the current name, like in this example:
a bug in isolation:
$ git branch -m <current-name> <new-name>
$ git branch <new-branch-name>

Keep in mind that you can only create new branches in your Publishing Branches
local repository (you will see them remotely when published). While you can’t create new branches on remote repositories,
you can most definitely publish an existing local branch by
By default, the new branch will be based on your currently typing this command:
checked out revision. If you’d like to start at a specific revision,
simply add that revision’s hash at the end of the command,
$ git push -u origin <local-branch>
like so:

Since you’re not allowed to rename remote branches, you


$ git branch <new-branch-name> 2b504bee
can delete the old one and then push a new one by typing
these two commands:
Tip: Make sure you give meaningful names to your branches
- something like “feature-being-worked-on” could be a good
starting point.
$ git push origin --delete <old-name>

Switching Branches $ git push -u origin <new-name>

To switch to a different branch and make it active (setting


it as the HEAD branch), you can rely on either “switch” or Tracking Branches
“checkout”. We like “switch”, as it is self-explanatory and
“checkout” can be used for other tasks, but both are valid. This is an important command to create relationships between
local and remote branches - since initially, they don’t have any!

$ git switch <other-branch> The most common example is having a local branch track a
remote one, so that you can simply type “git push” or “git pull”
OR
without additional parameters to keep everything in sync. This
can be quickly achieved by typing the command below:
$ git checkout <other-branch>
$ git branch --track <new-branch> origin/<base-branch>

Renaming Branches You can also use the “git checkout” command to achieve this.
This command assumes you’ve already checked out the If you want to name the local branch after the remote one, you
branch you’d like to rename (your local HEAD branch): only have to specify the remote branch’s name:

$ git branch -m <new-name> $ git checkout --track origin/<base-branch>

30-day free trial available at


www.git-tower.com The best Git Client for Mac & Windows
WORKING WITH BRANCHES IN GIT
presented by
TOWER — the best Git client for Mac and Windows

Comparing Branches Deleting Branches


This is a very useful command when you need to decide if When a local branch is no longer needed, you can delete it
you should integrate or delete a branch, as it presents you the by typing the command below:
commits that were created exclusively in there.
$ git branch -d <branch-name>
$ git log <main>..<feature-branch>
Tip: If that branch contains unmerged changes, you might
Tip: You can also use this command to compare local and also need the “-f” option to force the deletion - if so, proceed
remote states, like in the example below: with caution!

$ git log <origin/main>..<main> To delete a remote branch, keep in mind that the command
is totally different:

Merging Branches $ git push origin --delete <branch-name>


When it is time to integrate the new changes into your
current HEAD branch, it is time to merge! To avoid a messy repository, make sure you periodically look
for obsolete branches and delete them.
Merging consists of two steps: you will need to switch to the
branch that will receive the new changes first, and then type
the “git merge” command:

$ git switch <main>

$ git merge <feature-branch>

Rebasing Branches
Rebasing is an alternative to merging - both achieve the
same goal, but the Rebase option re-writes the project history,
creating a straight line. As a result, you get a linear history,
which may be preferred by some teams.

Rebasing consists of two steps: you will need to switch to the


feature branch first, and then type the “git rebase” command:

$ git switch <feature-branch>

$ git rebase <main>

30-day free trial available at


www.git-tower.com The best Git Client for Mac & Windows

You might also like