This is an overview of some useful Git commands.
Set up Git
Username
1
| git config --global user.name username
|
Email
1
| git config --global user.email email
|
Vim Editor
1
| git config --global core.editor vim
|
Notepad Editor
1
| git config --global core.editor notepad
|
Atom Editor
1
| git config --global core.editor "atom --wait"
|
Activate rename detection
1
| git config diff.renames true
|
Repository
Create a repository
Write data into a Object Database
1
| git hash-object -w hello.txt
|
Get content of object
1
| git cat-file -p 56d90gfdz
|
Get moved files
1
| git log --summary -M90% | grep -e "^ rename"
|
Get copied files
1
| git log --summary -C90% | grep -e "^ copy"
|
Get history of code lines
1
| git blame -M -C -C -C foo.txt
|
Commit
Choose specific files to commit
1
| git add foo.txt bar.txt
|
Choose a directory and everything under it to commit
Choose the current directory and everything under it to commit
Commit
1
| git commit --message "Commit Message"
|
Commit all changed files
Status
Diff
Workspace vs. Stage
Diff for file
Stage vs. Repository
Difference between two files
Change to the predecessor
Count of changes
1
| git diff --stat 1c96fgt 72ser18d
|
Continuous text
Reset
Reset everything
Reset specific files/directories
1
| git reset HEAD foo.txt src/test/
|
Stashing
Stash changed files
1
| git stash --include-untracked
|
Retrieve last saved changes
Get older stashed changes
1
| git stash pop stash@{1}
|
Stash list
Investigate Commits
Show overview
Show file content
1
| git show 32rftsgz:src/main.kt
|
Show root directory
Show directory “src”
Show directory “src” with subdirectories
1
| git ls-tree -r 32rftsgz -- src
|
History
Log output
Last 3 commits
One line per commit
Statistics
Short statistic
1
| git log --shortstat --oneline
|
Graph
1
| git log --graph --oneline
|
Branches
List of branches
Create branch from current commit
Create branch from any commit
1
| git branch new-branch 672dgzuj
|
Create branch from existing branch
1
| git branch new-branch existing-branch
|
Switch to a branch
Create and switch branch
1
| git checkout -b new-branch
|
Reset to older commit
1
| git reset --hard rs29bnz1
|
Delete not active branch
1
| git branch -d branch-to-delete
|
Restore branch when commit hash is known
1
| git branch deleted-branch ek183lp8
|
Get commit hashes
Merge
Merge feature branch into current branch
Start mergetool
Abort merge
Working With Repositories
List of remote tracking branches
1
| git branch --list --remote --verbose
|
Fetch: Get branches from other repository
1
| git fetch origin alpha master
|
Check integrity of repositories