Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Last active January 12, 2021 17:09
Show Gist options
  • Save tangoabcdelta/eabd64d581dd8906912be389e2ded950 to your computer and use it in GitHub Desktop.
Save tangoabcdelta/eabd64d581dd8906912be389e2ded950 to your computer and use it in GitHub Desktop.
some other commands in git
Checkout and track (1)
git fetch <remote-origin-name>

git checkout demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'
(1a)
git fetch upstream
git checkout bug-fix-release-13-12-2016
Branch bug-fix-release-13-12-2016 set up to track remote branch bug-fix-release-13-12-2016 from upstream.
Switched to a new branch 'bug-fix-release-13-12-2016'
Checkout and track (2)
git checkout -b <desired-branch-name> origin/<remote-branch-name>
git checkout -b release upstream/release
Checkout and track (3)
git checkout origin/master -- path/to/file
(3a)
git checkout -b bug-fix-release-13-12-2016 upstream/bug-fix-release-13-12-2016
Make an empty commit in git:

git commit --allow-empty

Rebase current branch wrt upstream/development
git fetch upstream development

git rebase upstream/development

Assume the following history exists and the current branch is "topic":

      A---B---C topic

     /

D---E---F---G master

From this point, the result of either of the following commands:

git rebase master

git rebase master topic

would be:

              A'--B'--C' topic

             /

D---E---F---G master
Retrieve the commit log for a specific line in a file
  • git blame shows you only the last commit for a particular line
  • to get a similar log of the commits that touched a particular line
git blame -L150,+11 -- git-web--browse.sh

git log --pretty=short -u -L 155,155:git-web--browse.sh

alias

If you use this functionality frequently, you might find a git alias useful. To do that, put in your ~/.gitconfig:

[alias]
    # Follow evolution of certain lines in a file
    # arg1=file, arg2=first line, arg3=last line or blank for just the first line
    follow = "!sh -c 'git log --topo-order -u -L $2,${3:-$2}:"$1"'" -

And now you can just do:

git follow git-web--browse.sh 155
To view the the list of commits that occurred in the file

using gitk is the easiest

# preferred way
# because it's a plumbing command;
# meant to be programmatic:
gitk /path/to/file/filename.extension
index.html
javascript/application.js
javascript/ie6.js

# another way
# less preferred for scripts
# because it's a porcelain command;
# meant to be user-facing
$ git show --pretty="" --name-only bd61ad98    
index.html
javascript/application.js
javascript/ie6.js
  • The --no-commit-id suppresses the commit ID output.
  • The --pretty argument specifies an empty format string to avoid the cruft at the beginning.
  • The --name-only argument shows only the file names that were affected.
  • Use --name-status instead, if you want to see what happened to each file (Deleted, Modified, Added)
  • The -r argument is to recurse into sub-trees

Harder ways:

git diff-tree --no-commit-id --name-only -r bd61ad98
If you want to get list of changed files:
git diff-tree --no-commit-id --name-only -r <commit-ish>
If you want to get list of all files in a commit, you can use
git ls-tree --name-only -r <commit-ish>
To view the change history of a method/function
git log -L :myfunction:path/to/myfile.c
use the combination of --stat and --oneline with the show command:
git show --stat --oneline HEAD
git show --stat --oneline b24f5fb
git show --stat --oneline HEAD^^..HEAD

# if you do not like/want the addition/removal stats
# then, you can replace --stat with --name-only

git show --name-only --oneline HEAD
git show --name-only --oneline b24f5fb
git show --name-only --oneline HEAD^^..HEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment