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:

[merge]
    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

[diff]
    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)

 

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

 

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.