First contact with Git

Because my shell history will not remember all those commands forever I dump some of my favorites here. Over time I surely will add more...

Last updated on 2012-12-21 21:54

Git reset

git reset the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

git reset --hard HEAD

Or revert latest local commit:

git reset HEAD~1

Git push

With git push you can update a remote repository.

The command falls back to "the current branch is pushed to the corresponding upstream branch", if no special configurations or options are present.

git push

Delete remote branch

A highly voted question How do I delete a Git branch locally and remotely?

The push-way:

$ git push origin --delete my-feature-branch
 - [deleted]         my-feature-branch

The branch-way:

$ git branch --delete --remotes origin/my-feature-branch
Deleted remote-tracking branch origin/my-feature-branch (was abcdefg).

Either way the local branch still exists. You can check the status with:

$ git branch --no-color -v
  main                   abcdefg Add fancy feature
* my-feature-branch      0123456 [gone] Add my feature

Hint: Deleting the local branch will keep you posted in case you haven't merged already.

$ git branch --delete my-feature-branch
warning: deleting branch 'my-feature-branch' that has been merged to
         'refs/remotes/origin/my-feature-branch', but not yet merged to HEAD.
Deleted branch my-feature-branch (was 0123456).

Git tag

Make an unsigned, annotated git tag object, delete and list tag objects.

git tag -a -m "Release 0.1.8" release_0_1_8
git tag -a -m "Release 0.1.8" v0.1.8
git tag -d release_0_1_8
git tag -l

git remote

How to show the currently used git remote:

$ git remote
$ git remote -v
origin git:// (fetch)
origin git:// (push)

git clone

Git clone and checkout a single branch:

git clone -b docs --single-branch git://

git fetch

With git fetch you can fetch branches and tags.

  • Fetch from all remotes
  • Prune tracking branches, if they are no longer present on remote(s)
git fetch --all --prune

--all --prune

Also discussed on Stack Overflow:

Git submodule

Working with Git submodules...

The following sequence shows how to create (and checkout) all submodules recursively with foreach:

git submodule foreach git branch 3.7.0.M02
git submodule foreach git checkout 3.7.0.M02

Update Git submodule to latest commit on origin

git submodule update --remote --merge


Get the short commit hash: git rev-parse --short HEAD

Git bisect

To be continued...