Skip to main content

git cheat sheet

Staging area#

Unstage the change#

git reset HEAD <source>

Restore file from repository#

git checkout HEAD <source>

Remove file#

git rm <source>

Move file#

git mv <source> <destination> 

Add changes in the working directory to the staging area#

git add .

The staging area lets you construct your next commit in a logical, structure fashion.

Moving files around and deleting them from the filesystem, without notifying Git, will cause you grief.

.gitignore#

Ignore all files that match this pattern#

*.<pattern-name>

Ignore all files that aren't in the top-level directory#

*/*.<pattern-name>

Despite any higher-level rules, don't ignore any files#

!/*.<pattern-name>

Looking at the global .gitignore#

git config --global core.excludesfile

Create global .gitignore#

touch ~/.gitignore_global

Set global .gitignore#

git config --global core.excludesfile ~/.gitignore_global

Sample .gitignore files#

https://github.com/github/gitignore

.gitignore lets you configure Git so that it ignores specific files or files that match a certain pattern.

! negates a matching rule.

Log & history#

Limiting results#

git log -<number>

Compact view of the repository history#

git log --oneline

Graphical view of the repository#

git log --graph

Compact graphical view of the repository#

git log --graph --oneline

Viewing non-ancestral history#

git log --oneline --graph --all

Summary of the commits#

git shortlog

Searching git history#

Names#

Name that consists of one part;

git log --author=kaanf --oneline

Name that consists of two or more parts;

git log --author="Kaan Fırat" --oneline

Words#

git log --grep=<word> --oneline

Single file#

git log --oneline <source>

Directory#

git log --oneline <directory>

This shows you all the changes that happened in that directory, but it’s not clear which files were changed.

Detailed information#

git log --oneline --stat <directory>

Commits that deal with the term#

git log-S"<term>"

Commits that deal with the term - (detailed)#

git log-S"<term>" -p

Commits on other branches#

git log --all -S"<term>"

Branching#

Create new branch#

git branch <branch-name>

Checking current branch#

git branch

Switching to another branch#

git checkout <branch-name>

Delete branch#

git branch -d <branch-name>

Merging#

Fast forward merge commit#

git merge <branch-name> --no-ff

Commits that are not in main branch#

git log <branch-name> --not <main-branch>

Remote repository#

Change workstation branch (master -> main)#

git branch -M maingit push -u origin main

Basics#

Set up a Git repository#

git init

Add a remote repository to local#

git remote add origin <remote-url>