Contents

Git Commands

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

1
git init

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

1
git add directory/

Choose the current directory and everything under it to commit

1
git add .

Commit

1
git commit --message "Commit Message"

Commit all changed files

1
git commit --all

Status

1
git status

Diff

Workspace vs. Stage

1
git diff

Diff for file

1
git diff foo.txt

Stage vs. Repository

1
git diff --staged

Difference between two files

1
git diff 1c96fgt main

Change to the predecessor

1
git diff p03sr4f^!

Count of changes

1
git diff --stat 1c96fgt 72ser18d

Continuous text

1
--word-diff

Reset

Reset everything

1
git reset HEAD .

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

1
git stash pop

Get older stashed changes

1
git stash pop stash@{1}

Stash list

1
git stash list

Investigate Commits

Show overview

1
git show 32rftsgz

Show file content

1
git show 32rftsgz:src/main.kt

Show root directory

1
git show 32rftsgz:

Show directory “src”

1
git show 32rftsgz:src

Show directory “src” with subdirectories

1
git ls-tree -r 32rftsgz -- src

History

Log output

1
git log

Last 3 commits

1
git log -n 3

One line per commit

1
git log --oneline

Statistics

1
git log --stat

Short statistic

1
git log --shortstat --oneline

Graph

1
git log --graph --oneline

Branches

List of branches

1
git branch

Create branch from current commit

1
git branch new-branch

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

1
git checkout main

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

1
git reflog

Merge

Merge feature branch into current branch

1
git merge feature

Start mergetool

1
git mergetool

Abort merge

1
git merge --abort

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

1
git fsck