Contents

Git Commands

This is an overview of some useful Git commands.

Set up Git

Username

git config --global user.name username

Email

git config --global user.email email

Vim Editor

git config --global core.editor vim

Notepad Editor

git config --global core.editor notepad

Atom Editor

git config --global core.editor "atom --wait"

Activate rename detection

git config diff.renames true

Repository

Create a repository

git init

Write data into a Object Database

git hash-object -w hello.txt

Get content of object

git cat-file -p 56d90gfdz

Get moved files

git log --summary -M90% | grep -e "^ rename"

Get copied files

git log --summary -C90% | grep -e "^ copy"

Get history of code lines

git blame -M -C -C -C foo.txt

Commit

Choose specific files to commit

git add foo.txt bar.txt

Choose a directory and everything under it to commit

git add directory/

Choose the current directory and everything under it to commit

git add .

Commit

git commit --message "Commit Message"

Commit all changed files

git commit --all

Status

git status

Diff

Workspace vs. Stage

git diff

Diff for file

git diff foo.txt

Stage vs. Repository

git diff --staged

Difference between two files

git diff 1c96fgt main

Change to the predecessor

git diff p03sr4f^!

Count of changes

git diff --stat 1c96fgt 72ser18d

Continuous text

--word-diff

Reset

Reset everything

git reset HEAD .

Reset specific files/directories

git reset HEAD foo.txt src/test/

Stashing

Stash changed files

git stash --include-untracked

Retrieve last saved changes

git stash pop

Get older stashed changes

git stash pop stash@{1}

Stash list

git stash list

Investigate Commits

Show overview

git show 32rftsgz

Show file content

git show 32rftsgz:src/main.kt

Show root directory

git show 32rftsgz:

Show directory “src”

git show 32rftsgz:src

Show directory “src” with subdirectories

git ls-tree -r 32rftsgz -- src

History

Log output

git log

Last 3 commits

git log -n 3

One line per commit

git log --oneline

Statistics

git log --stat

Short statistic

git log --shortstat --oneline

Graph

git log --graph --oneline

Branches

List of branches

git branch

Create branch from current commit

git branch new-branch

Create branch from any commit

git branch new-branch 672dgzuj

Create branch from existing branch

git branch new-branch existing-branch

Switch to a branch

git checkout main

Create and switch branch

git checkout -b new-branch

Reset to older commit

git reset --hard rs29bnz1

Delete not active branch

git branch -d branch-to-delete

Restore branch when commit hash is known

git branch deleted-branch ek183lp8 

Get commit hashes

git reflog

Merge

Merge feature branch into current branch

git merge feature

Start mergetool

git mergetool

Abort merge

git merge --abort

Working With Repositories

List of remote tracking branches

git branch --list --remote --verbose

Fetch: Get branches from other repository

git fetch origin alpha master

Check integrity of repositories

git fsck