git cheatsheet

Branches

Read the offical documents here

Creating and checking out a branch

$ git checkout -b Test2
Switched to a new branch 'Test2'

This command is shorthand for:

$ git branch Test2
$ git checkout Test2

Where git branch Test2 creates a new branch and git checkout Test2 switches to that branch.

The git branch command lets you create, list, rename, and delete 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.

Show all branches. The current branch is highlighted with an asterix.

$ git branch
  Test1
* Test2
  master

To only show the name of the current branch use the command below: As explained here.

$ git rev-parse --abbrev-ref HEAD
Test2

Show all remote branches

$ git branch -r
  origin/HEAD -> origin/master
  origin/Test1
  origin/initial
  origin/master
$

Our local branch is not yet on the remote repository. The git push command below pushes the local branch to the remote repository.

git push -u origin "Test2"
$ git branch -r
  origin/HEAD -> origin/master
  origin/Test1
  origin/Test2
  origin/initial
  origin/master
$

Remove the newly created branch

$ git branch -D "Test2"
Deleted branch Test2 (was d5e990c).

This removes the branch only locally, deleting the remote branch can’t be done with git branch but must be done using git push.
As explained here

$ git branch -r
  origin/HEAD -> origin/master
  origin/Test1
  origin/Test2
  origin/initial
  origin/master
$
git push origin --delete Test2
 - [deleted]         Test2
$ git branch -r
  origin/HEAD -> origin/master
  origin/Test1
  origin/initial
  origin/master
$

Leave a Reply

Your email address will not be published. Required fields are marked *