Skip to main content

Git Tips

This is an unorganized collection of tips and reminders about frequently used or handy git commands.

undoing

You did a git add <file> but have not yet done git commit <file>:

git checkout -- <file>

You did a git add <file> and a git commit <file>` to a file:

git reset HEAD <file>   # unstages file
git checkout -- <file>  # restores previous version
git status              # should show nothing to commit

branching

Create a new branch:

git branch <newbranchname>

Switch to an existing branch:

git checkout <branchname>

Rename the current branch to 'foo':

git branch -m foo

Rename the branch 'foo' to 'bar':

git branch -m foo bar

checkout a specific version of a file or directory

Checkout a specific file from a specific version:

git checkout mybranchname -- src/myapp/myfile.txt
git checkout 23e4af33 -- src/myapp/myfile.txt
git checkout 23e4af33 -- src/myapp

show specific version of file

Get the contents of a file from a specific commit:

git show somebranch:path/from/the/git/root/file.txt

information about changes between commits

List the names of all files that changed between two commits:

git diff --name-only 23ea4f5a 3b22fc41

List the names of all files within a directory that have changed:

git diff --name-only 23ea4f5a 3b22fc41 -- dirname

Detailed diff of all changes made to a file between two commits:

git diff 23ea4f5a 3b22fc41 -- dir1/dir2/file.txt

Detailed diff of all changes to all files between two commits:

git diff 23ea4f5a 3b22fc41

information about differences between local and remote

git status says that my branch is ahead of 'origin/master' by 2 commits. What are the specifics?

# diffs of the files
$ git diff master origin/master

# names of files
$ git diff --name-only master origin/master

history

List all commits, printing short version of hash:

git log --oneline --decorate

List all commits, printing long version of hash:

git log --pretty=oneline

List all commits made between two tags:

git log —oneline v1.9.2..v1.9.3

List all commits and show their graphical relation to each other:

git log --graph

status

Print a concise status:

git status -s

tags

List tags:

git tag

Create a lightweight tag. Assume you have just committed changes and pushed to origin. First, create the tag, then push the tag to origin. The tag must be specifically pushed; it will not be pushed otherwise.

git tag -a v1.9.7 -m 'useful tag description'
git push origin v1.9.7

updating a production box with new code

Assume the server is at version v1.9.2 and we want to go to 1.9.3. Here is one way to do it.

Confirm the server is actually at v1.9.2. If it isn't, don't proceed.

$ git status           # confirm current version
$ git tag -l           # v1.9.3 should not appear

The repository on the server is out of date with respect to origin. Therefore, we must update the server's repository, but we don't want to blindly apply the updated code. Just update the repository, nothing else.

$ git fetch origin                 # update repository on server
$ git tag -l                       # make sure v1.9.3 appears
$ git diff v1.9.2 v1.9.3           # review changes
$ git log —oneline v1.9.2..v1.9.3  # see all commits

This example assumes the Bishop store server. We are applying changes made to the FDGG api. We know we only want to apply changes from the fdgg module.

$ cd ~/current/ecom/store
# confirm again what will be installed
$ git diff v1.9.3 - - fdgg
# see that api.py is the only file that changed
# get the v1.9.3 version of api.py
$ git checkout v1.9.3 - - fdgg/api.py

# if there had been multiple files that changed in fdgg,
# could have done it this way instead:
$ cd ../..
$ git checkout v1.9.3 - - fdgg

This is a little funky in that when all the above is done, git status will still show the system as being at v1.9.2. Need to consider a better method.

Random Stuff

# checkout specific version of file / dir
git checkout mybranchname -- src/myapp/myfile.txt
git checkout 23e4af33 -- src/myapp/myfile.txt
git checkout 23e4af33 -- src/myapp

# show specific version of file
git show somebranch:from/the/git/root/file.txt

# all files changed between commits
git diff --name-only 23ea4f5a 3b22fc41

# all files changed in a directory between commits
git diff --name-only 23ea4f5a 3b22fc41 -- dirname

# all changes in a file between commits
git diff 23ea4f5a 3b22fc41 -- dir1/dir2/file.txt

# all changes between commits
git diff 23ea4f5a 3b22fc41
# files that were change between commits
git diff 23ea4f5a 3b22fc41 --name-only

# history with tags
git log --oneline --decorate   # prints short version of hash
git log --pretty=oneline       # prints long version of hash


# concise status
git status -s

# list tags
git tag

# create a tag
# you have just committed and pushed to origin
# create a lightweight tag
git tag -a v1.9.7 -m 'useful tag description'
# push the tag to remote
git push origin v1.9.7


# server is running v1.9.2, want to upgrade to 1.9.3
#
# 0) On server, make sure you are in current release (1.9.2)
#
$ git status                # confirm current version
$ git tag -l                # v1.9.3 should not

# 1) On server, update local repository to match that of origin
#    This only updates the contents of your repository.  It does not merge or
#    otherwise mess with any changes.  If we didn’t do this, the server would
#    not have 1.9.3 locally.
#
$ git fetch origin
$ git tag -l                # make sure v1.9.3 appears
$ git diff v1.9.2 v1.9.3    # confirm what will be installed
$ git log —oneline v1.9.2..v1.9.3  # see all commits

# 2) For the subset of files in question (in this actual case, we want to pickup
#    bug fixes made to FDGG)
$ cd ~/current/ecom/store
$ git diff v1.9.3 - - fdgg              # confirm again what will be installed
$ git checkout v1.9.3 - - fdgg/api.py   # only file that changed
$ cd ../..
$ git checkout v1.9.3 - - fdgg          # could do this instead if multiple files