Some git commands for amending commits and showing short logs

I noticed a friend of mine tweet about git commit --amend yesterday, and it occurred to me that some people probably haven't used git very much and are still learning the ins and outs. I thought I'd share some commands I find really useful.

The first thing to mention is that you can set up your own command aliases by editing a file called .gitconfig in your home directory. This is where I keep all my custom aliases. (I use them so often that I'm often lost when using git on a different system where my aliases aren't set up.) Here's an extract from my .gitconfig with some explanation of the aliases:

[user]
  # sets up your details so that commits carry the right user info;
  # I use my primary github email address for mail, so that
  # all my github commits are associated with the right account
  mail = <you primary github email address>
  name = <your real name>
[smtp]
[sendemail]
[color]
  # turns on the default terminal colouring for git output
  ui = auto
[alias]
  # A good resource on git aliases is https://git.wiki.kernel.org/index.php/Aliases

  # these are just shortcuts to full git commands
  st = status
  ci = commit
  br = branch
  co = checkout
  cp = cherry-pick

  # I use this one when I am working locally, miss some changes, then want to
  # add them onto the last commit I made; it stages all the files which have
  # changed (NB NOT new files or deleted files), merges those changes to HEAD,
  # and retains any existing comment from HEAD
  cim = commit -a --amend -C HEAD --reset-author

  # this is similar to the cim command, but instead of automatically adding any
  # changed files, it only merges staged files into the HEAD commit; this is useful
  # where you just want to add one or two files you've changed to the existing
  # HEAD commit, before making a new commit with the other changes
  fix = commit --amend -C HEAD

  # sl: short log showing author and changed files; useful for checking
  # what I've patched in each commit
  sl = log --format=format:'%h %s%n%an, %ar (%ai)' --name-status

  # author: Show all the commits a specified author has done
  # Usage: git author <email_address|name>
  author = "!sh -c 'git log --tags --source --oneline --author=\"$1\"' -"

[merge]
  tool = meld
  keepBackup = false

[push]
  default = current

Note that I have others, but these are the ones I use most often.

Next, to give a flavour of how you might use them, here's an example session making use of these commands.

First I init a git repo and add a README file:

[ell@lp shiny]$ git init .
Initialized empty Git repository in /tmp/shiny/.git/
[ell@lp shiny]$ vim README
[ell@lp shiny]$ git st
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	README
nothing added to commit but untracked files present (use "git add" to track)
[ell@lp shiny]$ git add README
[ell@lp shiny]$ git ci -m "Add README"
[master (root-commit) a2584b9] Add README
 1 file changed, 1 insertion(+)
 create mode 100644 README

Now I edit the README, but want to merge those changes into the existing commit. So I use my cim command:

[ell@lp shiny]$ vim README 
[ell@lp shiny]$ git cim
[master f4be9e7] Add README
 1 file changed, 3 insertions(+)
 create mode 100644 README

This adds my changes to the existing commit, keeping the commit message I've already laboriously typed in.

Now I show a short log of what's happened so far. I prefer this log format as it shows just the changes files, a short form of the git commit idedntifier, the author, and the comment:

[ell@lp shiny]$ git sl
f4be9e7 Add README
Elliot Smith, 2 seconds ago (2013-07-18 10:35:12 +0100)
A       README

Now I add a new file, package.json, then make an edit to README:

[ell@lp shiny]$ vim package.json
[ell@lp shiny]$ vim README 
[ell@lp shiny]$ git st
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   README
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	package.json
no changes added to commit (use "git add" and/or "git commit -a")

I want to include my edit to README with the existing commit, then add a new commit for package.json. So I first stage the changes to README and use my fix command to merge them into the existing HEAD:

[ell@lp shiny]$ git add README
[ell@lp shiny]$ git fix
[master 71de8f9] Add README
 1 file changed, 5 insertions(+)
 create mode 100644 README
[ell@lp shiny]$ git sl
71de8f9 Add README
Elliot Smith, 32 seconds ago (2013-07-18 10:35:12 +0100)
A       README

Then I stage the changes to package.json and make a new commit for them:

[ell@lp shiny]$ git add package.json
[ell@lp shiny]$ git ci -m "Add package JSON"
[master b717cc8] Add package JSON
 1 file changed, 1 insertion(+)
 create mode 100644 package.json
[ell@lp shiny]$ git sl
b717cc8 Add package JSON
Elliot Smith, 2 seconds ago (2013-07-18 10:35:52 +0100)
A       package.json

71de8f9 Add README
Elliot Smith, 42 seconds ago (2013-07-18 10:35:12 +0100)
A       README
[ell@lp shiny]$ 

Notice that I almost automatically do git sl after each cim/fix/commit command, as it's so quick and easy to read the output.

Anyway, I might write some more if I feel so inclined another day. I make extensive use of interactive rebasing, for example, which is never really explained anywhere in simple terms, but which is massively useful (especially if you're making branches to turn into pull requests for other projects).

git can be a bit daunting at first, but after you've used it for a while I don't think you'd use any other version control system ever again, unless forced to.

Comments

Speaking

SpeechSchool.TV is the world's #1 provider of online speech training. We're based on campus at a major university and in central London. Whether you want to Learn public Speaking online with a Standard English accent or become a seasoned public speaker, our courses can transform your confidence.

I'm greatful for everything

I'm greatful for everything you guys have done for me
Cheers


Mr. Bernardo Alba

Will these GIT commands work

Will these GIT commands work on any presentation? I have a plan on making one but I'm just a starter. surveillance system

Can you make a code for

Can you make a code for showing long logs? city gate Singapore

Thanks for the codes man.

Thanks for the codes man. Been looking for this. Thanks SECOM Singapore Pte Ltd

I wondered MB5-705 dumps

I wondered MB5-705 dumps whether I could extend this idea to web servers: could a web server present a page; and on that page, a link which would start MB4-873 dumps another web server and load a page from it; the latter page being embedded in the first page, and also presenting a link which would start another web server then load a page from it MB4-535 dumps

Git development began after

Git development began after many developers of the Linux kernel gave up access to BitKeeper, a proprietary SCM system that had previously been used to maintain the project.Git was originally designed as a low-level version control system engine on top of which others could write front ends, such as Cogito or StGIT.Thanks.
Regards,
apple.com

I've taken quite a few notes

I've taken quite a few notes on the way, to inform my presentation in two weeks.192.168.1.1

Garcinia cambogia

For anyone who've unfamiliar about it get all of the puts a stop to Sheriff, they inside fairly recent previous coated penitentiary units lilac as Garcinia cambogia well as built convicts don lilac prison garments. Sheriff Later on is finding red-colored once again. Now, they are tired of the best way Maricopa County is using around $18 million dollars 1 year with run-a-way pets. Along most of these lines, quite keeping with complainning..

Guar protein ffer a wide

Guar protein ffer a wide selection of products for your specific requirements. Established in the year 2008, we are one of the leading Manufacturer, Exporter and Supplier of Compound Guar Protein Meal Feeds and Poultry Feeds.
Guar meal churi manufacturer
Guar meal korma exporter
Food grade korma

Reply

I had heard a lot about git commands and its functionalities and searched all over the internet to find an article on it. I accidentally came across this article and I found it to be helpful. Thanks a lot for this wonderful article.
http://poweredpbx.com/

Great! Thanks for your

Great! Thanks for your documents, its been very helpful. Thanks again for sharing your information. --houses for sale in white rock

Even the prettiest websites

Even the prettiest websites online aren’t the most successful ones. It takes more than a pretty site with bells and whistles to make a website successful. That is where Syracuse Web Pages comes in, located in Syracuse NY we can offer you a great looking site but one that is easy to use and easy to navigate.
Syracuse NY ecommerce services
Syracuse NY Wordpress

I foundthis article as very

I foundthis article as very interesting and informative, thank you so much for sharing this really help a lot on my studies. - Rabbi Binyomin Lisbon

These are really a superb

These are really a superb showcases here I am glad to have a review of it and feel to roll on its steps for my benefit always
imac service center in kolkata

For those of you who have

For those of you who have not known about this pull out all the stops Sheriff, he in the relatively recent past painted penitentiary units pink and made convicts wear pink jail clothing. Sheriff Joe is seeing red once more. This time, he is tired of the way Maricopa County is using over $18 million dollars a year on stray creatures. Along these lines, rather keeping on complainning...market research data collection

I find your blog as a source

I find your blog as a source of very nice information. Pre Walker Shoes

I have seen your blog its

I have seen your blog its awesome your writing skills are awesome. http://howtofixacar.info

Thanks you for another

Thanks you for another essential article. Where else could anyone get that kind of information in such a complete way of writing ? I have a presentation incoming week, and I am on the lookout for such information.
Guar powder price