Git Cheat sheet

Title:Git Cheat sheet
Author:Douglas O’Leary <dkoleary@olearycomputers.com>
Description:Git tasks and commands
Date created:04/2014
Date updated:04/2014
Disclaimer:Standard: Use the information that follows at your own risk. If you screw up a system, don’t blame it on me...


Overview:

Git is an extremely versatile Version Control Software (VCS). As such, it has lots of options and arguments to those options. This cheat sheet will be designed, generally, around how I am using and will use it. I am a UNIX and security admin, though, so the way I use git will differ from the way serious programmers do.

I’m keeping the list of commands at the top so I can find them quickly when I forget the damn things. There’s a discussion section a little further on which goes into some more detail, particularly related to the log.

In no way should this be considered to be a replacement for reading/learning/researching git so you have an understanding of what it does and how it does it. This is strictly a quick reference to the git commands that I typically use.

Commands:

Command Descripton
git config –global user.name dkoleary Creates a default user
git config –global user.email ${email} Creates a default email
git config –global color.ui true Enables color on displays.
git config –global core.excludesfile ${f} Configures a global ignore file
git config –list displays all configured settings
git init Creates a git repo in the current directory
git add [ *|.|spec ] Adds the specified files to the staging area
git commit [-a | ${f}] [-m’${text}’] Commits the files in the staging area.
git commit –amend Redoes the last commit if you want to update the message.
git rebase -i HEAD~# Enables rewriting/merging/removing multiple commits. Current minus #
git clone ${dir} Creates a clone of the repo under the current directory
git clone git://${dir} Creates a clone of the repo using the git protocol
git clone ${user}@${host}:${dir} Creates a clone using ssh.
git status Displays files that have changed, not tracked, and ones waiting commission
git diff [${f}] Displays differences in all files or in the one specified.
git diff ${rev1}:${file} ${rev2}:${file} Display differences in ${file} between two commits
git rm [–cached] ${f} Removes the file from repo and disk - unless –cached is added.
git mv ${src} ${dest} Renames a file from ${src} to ${dest} in repo and on disk.
git log Displays the commit log. Options described below
git log –follow ${file} Display commits that affect ${file}
git log ${ref} -p -1 Display changes in a specific commit reference
git checkout ${f} Reverts the file to the one in staging or the last committed copy
git checkout ${rev} ${f} Reverts the file to the commit referenced by ${rev}
git pull [${remote}] Updates files that were cloned earlier.
  • git fetch [${remote}]
  • git log ..${remote}/master
  • git merge ${remote}
  • Pulls changes but does not apply them.
  • Displays changes that are not yet applied
  • Merges in the changes. These three steps are the same as git pull
git push ${remote} ${branch} Pushes updates to the remote repo.
git remote [-v] Display remote repos associated w/current directory
git remote show ${remote} Display additional information on remote repo
git branch –set-upstream master origin/master Associates origin/master w/local repos master. Avoids requirement to execute git pull origin master
git branch ${branch} Create a new branch of the current repo
git checkout ${branch} Switch to ${branch}
git checkout -b ${branch} Create and switch to a new branch of the current repo
git checkout master Return to the original branch
git merge ${branch} Merges the master and ${banch} trees
git branch Displays the available branches and which one is current

Discussion:

git log:

The git log command has an ugly array of options. Just straight, it displays a healthy amount of info:

$ git log
commit 9b3f27c27cbdd10774ec515ac893b40713b92c30
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Mon Sep 30 14:09:11 2013 -0500

    reasonably close to final list of commands

commit 5b627623a1f17ebccb6897a766efd47c8d257a86
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Mon Sep 30 13:09:32 2013 -0500

    intermediate commit

commit 74f82bb910f6f9ea2c210affcf7910b04fcf151f
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Mon Sep 30 12:45:28 2013 -0500

    initial deposit

You can also limit the number of commands:

$ git log -1
commit 9b3f27c27cbdd10774ec515ac893b40713b92c30
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Mon Sep 30 14:09:11 2013 -0500

    reasonably close to final list of commands

Or, to see the information for a specific log:

# git log e74cca3 -1
commit e74cca3507beee8547eb8c4ce39fe1fc7f1011de
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Wed Oct 2 09:23:24 2013 -0500

    config.xml: 152 2011-08-16

Display the changes in a specific commit reference (-p arg):

$ git log 3d5d316 -p -1
commit 3d5d316ce3b445c1fefc29eda938875ad5055e95
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Mon Apr 7 17:58:18 2014 -0500

    rhce_notes: short study period on ch 10

diff --git a/rhce/rhce_notes.rst b/rhce/rhce_notes.rst
index 8993096..0356efb 100644
--- a/rhce/rhce_notes.rst
+++ b/rhce/rhce_notes.rst
@@ -419,5 +419,45 @@ other.  Ended up going comletely nuclear and reinstalling v
 KVM went rule happy when libvirtd is restarted.  Too many rejects in the forwar
 the rhel_kvm page for details.

+04/07/14:  A short study day today.  Wanted to get a little further since yeste
+waste.

+*   While playing w/the firewall rules on server1, I'm not able to block outsid
[[snip]]

To display statistics:

$ git log -1 --stat
commit 9b3f27c27cbdd10774ec515ac893b40713b92c30
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Mon Sep 30 14:09:11 2013 -0500

    reasonably close to final list of commands

 git_commands |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)

and, the one that I’ll use most often, display one-line logs w/abbreviated hashes:

$ git log --pretty=oneline --abbrev-commit git_commands
9b3f27c reasonably close to final list of commands
5b62762 intermediate commit
74f82bb initial deposit

The nice thing about the abbreviated hashes is that git will lengthen them to ensure uniqueness. (There’s got to be a way to make that the default...)

You can then use those abbreviated hashes to revert to a previous commit:

git checkount 5b62762 git_commands

git log can also show whaat commits affected a specific file:

$ git log --follow DR/${target}
commit aec3941c264c05b29c648e6293e6fbb63fd4d679
Author: root <root@mysource.myco.com>
Date:   Tue Nov 5 18:00:02 2013 -0600

    Periodic update/push

commit 7318080a26b9930e471380062245a5ddd7a801ae
Author: root <root@mysource.myco.com>
Date:   Tue Nov 5 12:00:02 2013 -0600

    Periodic update/push

commit 056144eb367080a86022571f772a8737b1952195
Author: root <root@mysource.myco.com>
Date:   Fri Nov 1 09:55:03 2013 -0500

While not log specific, the way to identify the differences between two git versions for a specific file is:

$ git diff 8d2fcac0acf6fa:${file} c52ffa848c7a0:${file}
diff --git a/8d2fcac0acf6fa:${file} b/c52ffa848c7a0:${file}
index f706856..4097f5d 100644
--- a/8d2fcac0acf6fa:${file}
+++ b/c52ffa848c7a0:${file}
@@ -35,7 +35,7 @@ ExtendedStatus On

 LoadModule weblogic_module      modules/mod_wl_20.so

-NameVirtualHost 10.200.111.43:80
+NameVirtualHost *:80

 Include conf/mod_jk.conf
 Include conf/vhost-myhost.conf

Commit references:

git uses a sha1 hash reference as the means of differentiating various commits. These hashes are used in a variet of commands so knowing how to get them and how to refer to them will probably make your life easier.

As such, and as a review:

$ git log --pretty=oneline --abbrev-commit git_commands
9b3f27c reasonably close to final list of commands
5b62762 intermediate commit
74f82bb initial deposit

Getting similar info for the entire repo (which isn’t much different than that file atm):

$ git reflog
9b3f27c HEAD@{0}: commit: reasonably close to final list of commands
5b62762 HEAD@{1}: commit: intermediate commit
74f82bb HEAD@{2}: commit (initial): initial deposit

To see what happened to git_commands at the first commit:

$ git show 74f82bb git_commands
commit 74f82bb910f6f9ea2c210affcf7910b04fcf151f
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Mon Sep 30 12:45:28 2013 -0500

    initial deposit

diff --git a/git_commands b/git_commands
new file mode 100644
index 0000000..8229d15
--- /dev/null
+++ b/git_commands
@@ -0,0 +1,4 @@
+git config --global user.name dkoleary | Creates a default user
+git config --global user.email dkoleary@olearycomputers.com | Creates a default
+git config --global color.ui true | Enables color on displays.

You can also use the hash syntax: HEAD@{2}. That means the current version minus two commits:

$ git show HEAD@{2} git_commands
commit 74f82bb910f6f9ea2c210affcf7910b04fcf151f
Author: dkoleary <dkoleary@olearycomputers.com>
Date:   Mon Sep 30 12:45:28 2013 -0500

    initial deposit
[[rest of same display snipped]]

And, lastly, check out previous revisions based on those:

$ wc -l git_commands
29 git_commands
$ git checkout HEAD@{2} git_commands
$ wc -l git_commands
4 git_commands
$ git checkout HEAD@{1} git_commands
$ wc -l git_commands
18 git_commands
$ git checkout HEAD git_commands
$ wc -l git_commands
29 git_commands

Summary:

I can pretty much guarantee there’ll be additions to this page. Every time I have to search for the same thing twice, it’ll end up in here...

I’ve got to say, though, that git is a very nice VCS. It seems to be much easier and much more flexible than SVN - at least as I remember it...