Liangfu Chen's Homepage
  • Home
  • About
  • Categories
  • Tags
  • Archives

Notes on using Git

Contents

  • Custom git log command
  • Undo a commit and redo
  • Renamed a branch and create new branch

Custom git log command

Add the following line to your ~/.bashrc would provide gl command for printing pretty git-log output.

alias gl='git log --graph --pretty=format:"%Cred%h%Creset . %an: %s %Cgreen(%cr)%Creset" --abbrev-commit --date=relative'

When gl command is executed, an output look like the following would be generated:

* 11a76be . Liangfu Chen: fix problem #26; (5 days ago)
* d375136 . Liangfu Chen: fix problem #31; (5 days ago)
*   9d3f1de . Liangfu Chen: Merge branch 'master' of blah ... (5 days ago)
|\
| *   c254636 . blah ... (6 days ago)
| |\
| * | 9e2f1a4 . blah ... (6 days ago)
* | | a24bf14 . Liangfu Chen: fix problem 29; (5 days ago)
| |/
|/|
* | 1b13634 . Liangfu Chen: blah ...

Undo a commit and redo

A post on SO explains how to undo previous git commit.

$ git commit -m "Something terribly misguided"              (1)
$ git reset --soft HEAD~1                                   (2)
# << edit files if necessary >>                             (3)
$ git add ...                                               (4)
$ git commit -c ORIG_HEAD                                   (5)

Say, there are three cases:

  1. previous commit is a disaster, then reset with argument --hard to REMOVE the file changes. (This can be dangerous if your file changes are important!!)
  2. previous commit is a NOT disaster, just undo the commit but keep the changes, then reset WITHOUT argument --hard or --soft to keep the file changes.
  3. previous commit is a NOT disaster, in case you might needed it after all, then reset with argument --soft to keep the file changes and the index.

Renamed a branch and create new branch

First, we need to switch to another branch to rename the current one:

$ git checkout testing

To rename a branch, e.g. rename master branch to a date:

$ git branch -m master master-r`date +%F`

Then create a brand new branch, to replace the previous master branch:

$ git checkout --orphan master

the --orphan argument generates an empty branch, instead of the current one. And finally, pull remote content to current master branch, which would not lead to any conflict, obviously.

$ git pull origin master
Comments
comments powered by Disqus

  • « Setup PPTP server on Linux for iOS devices
  • Notes on Programming with OpenMP on MacOSX »

Published

Jun 7, 2015

Category

Articles

Tags

  • git version-control 1

Stay in Touch

  • Liangfu Chen's Homepage - Keep Calm and Carry On!
  • Powered by Pelican. Theme: Elegant by Talha Mansoor