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

The post is absolutely

The post is absolutely fantastic Lots of great information and inspiration both of which we all need Also like to admire the time and effort you put into your site and detailed info you offer. I will bookmark your website. swan system

I really loved reading your

I really loved reading your blog. It was very well authored and easy to understand. happy new year 2016

Thank you for sharing these

Thank you for sharing these very useful codes as they help me further develop my program. I am a beginner for this language and I am really grateful I found this one. I need to learn more and practice more regarding this language. I hope you keep those tips coming. Thank you! allaboutnewlaunch.com

These commands are very

These commands are very helpful in my work so I really want to say thank you. I am still in the beginning stage of GIT and I really want to master this language. I hope you keep up giving helpful tips for use amateurs in GIT. Again, I am very grateful in finding this article.new condo in Singapore

Exotic Car Rental Miami Beach

An exotic car and luxury car rental company. We also provide sport cars, suvs and convertibles as well. our entire car fleet is new and updated. We deliver and pick up as well.

http://luxurycarrentalsmiami.com/

First of all, than you for

First of all, than you for this content. Second, good job with GIT commands sometimes I worry that I won't find reliable content regarding GIT. Fortunately, I found this article. I will be able to resolve my problem. compression sleeve Amazon.com

Thank you for these GIT

Thank you for these GIT commands you have shared. I am an amateur when it comes to GIT and I am really thankful for this article. I worked on a project that involves GIT and I am relieved to find this. I hope you keep this coming for us beginners. mesh screen

Really such a unique

Really such a unique article plus subject is too. When i came and started reading this post then recon of this article that your post is astonishing for me. Buy assignments help online services from here with satisfactions plus in cheap rate.

I am happy to find this

I am happy to find this website it has very informative content which I found good to read. I will visit this website to read moreclothing manufacturer

I found so many interesting

I found so many interesting stuff in your blog especially its discussion. and it has really helped me alot. Do you have any other posts about this topic? Thanks for sharing with us. Dottori Damis Resta Rossi Iommetti

When it comes to deals, you

When it comes to deals, you can avail 12 months free Line rental or SIM only deals in accordance with your needs. Not only this, one can enjoy easy cash back offers as well as other schemes along with the favourite handset. mobile price

Let's start with the process

Let's start with the process by which one can avail the gadget. First of all, you are needed to sign a contract with the provider for the fixed span of time.memory card

This great site is really

This great site is really useful. I must realize other sorts of more knowledge about that article. And so make sure you provide other sorts of details about this specific aspect,Cheers. Pierfrancesco Rossi Iommetti

I am happy to find this

I am happy to find this website it has very informative content which I found good to read. I will visit this website to read more. uk essays | essay valley

superiorpaper

hard drive data recovery

My friend wanted to know how about hard drive data recovery some days ago very urgently. I advised him to visit the website hard drive data recovery a great website as I know. You can find the solution of all kind of information about hard drive data recovery in this website which will help you very much I believe.

I have browsed most of your

I have browsed most of your posts. This post is probably where I got the most useful information for my research gaven til ham

interesting

I've never used git, simply because I haven't had any reason to, but thanks for the syntax. Maybe I'll research and write about it myself, try and frame it like my article on access to blocked sites, I think that will be cool.

Thanks for this article.

Thanks for this article.
http://www.newfashiontalents.com/

Horosoft, Astrology Software

Horosoft, Astrology Software program, based on Indian native Astrology, is known as a flaw-less software programs getting attained worldwide brand, particularly it’s ‘Web Edition’ dispensing facility for any web sites to present online astrological agencies. Their Medical specialist Release certain. 0 caters to the full fledged requirements involving pro astrologers by providing exact astrological computations, softwaresegment

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.

GLOBAL POSITIONING SYSTEM

GLOBAL POSITIONING SYSTEM UNIT models get produce for essential holiday getaway break colleague for autos the same as vehicles, motorbikes and even marine yachts apart from airplane. thanks in an effort to it is certainly handy, proper apart from trouble-free to develop usage for course-plotting industrial, it’s an easy task to quickly process your speediest apart from most secure way for the concentrate on position. gpssegment

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

Mobile

it has been found that freebies are the best method which automatically incite the people to plunge into the purchasing process. club mobile