Configuration commands


git config // to set options

git config --edit // edit repository local config file

git config --global --edit // edit global config file


Repository local config file is at <project dir>\.git\config

Global config file is at %userprofile%\.gitconfig


git init <path> // path is optional, will use current working directory if blank


git status // show status


Log commands


git log

git log --oneline // easier to read

git log --decorate // to view more detailed info

git log -n 5 // show last 5 commits

git log <branch> // display log of all commits (in branch)

git log --all // all commits in all branches

git log --oneline --decorate --all // useful

git log --oneline --decorate --all --graph // add a "graphical" display of branches

git log --all --oneline --grep="string" // display searching on commit message


git reflog // view reference logs


Commit commands


git add * // git add <file> (usually use git add .)


git commit // will prompt for message by a text editor (vim)

git commit -m "message" // quicker, when the full description is not required

git commit --amend // to change commit message

git add . ; git commit --amend // to add later changes to previous commit (and avoid creating a new commit)


git reset --hard HEAD // undoes changes to tracked files, git reset --hard does the same thing?

git clean -f // removes untracked files


git checkout <commit> // to return to an older commit

git checkout <branch> // to return to latest commit (in branch)

git checkout <other_branch> <file> // copy files to current branch from <other_branch>


Diff Commands


git diff // show differences in changed files

git diff --cached // show differences in

git diff --diff-filter=M

git diff --name-only // only show file names of changed files

git diff --name-status // show file names and status of changed files


git difftool // can be used with external comparison tool (WinMerge)

Requires that changes be made to %userprofile%/.gitconfig.  For WinMerge instead of vim add / change:

    tool = winmerge

[mergetool "winmerge"]
    name = WinMerge
    trustExitCode = true
    cmd = "/c/Program\\ Files\\ \\(x86\\)/WinMerge/WinMergeU.exe" -u -e -dl \"Local\" -dr \"Remote\" $LOCAL $REMOTE $MERGED

    tool = winmerge

[difftool "winmerge"]
    name = WinMerge
    trustExitCode = true
    cmd = "/c/Program\\ Files\\ \\(x86\\)/WinMerge/WinMergeU.exe" -u -e $LOCAL $REMOTE


git difftool <other_commit> // compare current commit to other commit.  Brings up all files one by one.

git difftool <commitA> <commitB> <filename> // compare specific file

git difftool <other_commit> <filename> // compare specific file with current commit version

(e.g. git difftool 954b434 ./projname/filename.cpp)

git difftool <commitA>:<filenameA>  <commitB>:<filenameB>


Branch Commands


git branch <name> // create a new branch

git branch // display branches

git checkout <branch> // switch to branch

git checkout -b <new-branch-name> // create new branch to keep new commits while in detached HEAD

Returning to an old commit for testing, etc:

git checkout -b <new-test-branch-name> <checkout> // go back to older commit and start a new branch there

... do work ...

git checkout master

git branch -d <new-test-branch-branch> // delete test branch


Aliasing commands


edit %userprofile%/.bash_profile

alias gs="git status"
alias gl="git log"
alias glo="git log --oneline --decorate --all"
alias glod="git log --all --oneline --decorate --date=format:%Y-%m-%d\ %H:%M:%S --pretty=format:\"%C(yellow)%h%C(auto)%d% %C(white) %cd%Creset %s\""
alias ga="git add ."
alias gc="git commit"
alias gcm="git commit -m"
alias gca="git commit --amend"
alias go="git checkout"

reload changes with source ~/.bash_profile


vim Notes


Writing a git commit message:

type 'i' go into INSERT mode, then write the following

<commit summary 50 chars>

<blank line>

<detailed description of commit>

hit 'ESC', then ':wq' to write and quit.