Cheat Sheet
Shell
Command | Function |
---|---|
[tab] | pressing tab for autocompletion, depending on shell multiple [tab] work as well |
alias g=git | sets g to be an alias for git in your current shell session |
ls | lists directory |
ls -a | lists all files in a directory, also hidden files |
mkdir directory | creates directory |
cd directory | change into directory |
cd .. | change to parent directory |
pwd | print working directory |
echo "input" > file | writes input in file, overwrites file |
echo "input" >> file | appends input to file, does not overwrite file |
touch file | creates empty file |
cat file | prints file to stdout |
less file | prints file to stdout with pagination |
rm file | delete a file |
rmdir directory | deletes directory |
file filename | shows information about file type |
strings filename | extracts strings out of file |
man command | shows the manpage for command |
GIT
- [something] denotes a optional parameter
- A
ref
can be a commitHash or any other ref, e.g. a branch name.
Configuration
All configuration can be changed with either
git config [--global] ...
for your usergit config --local ...
for the current repository
the current configuration can be listed running
git config list
.
Configuration | Function |
---|---|
alias.myalias "command" | creaty myalias for command |
init.defaultBranch main | sets the default branch name to main |
rerere.enable true | enables reuse recorded resolutions |
pull.rebase true | by default rebases local changes on top of remote branch when pulling |
user.email yourmail | sets user email to yourmail |
user.name yourname | sets user name to yourname |
core.editor nano | sets default editor to nano (or whatever editor you prefer) |
Repository creation
Command | Function |
---|---|
git init | creates new repository |
git init --initialBranch test | creates new repository with branch test |
Working in a repository
Command | Function |
---|---|
git add path | stages path |
git add -i | starts interactive staging |
git add -p path | stage hunk by hunk |
git status | prints status |
git status --short | print short status |
git commit | commits the staged files, opens an editor |
git commit -m "message" | commits the staged files with message |
git commit --amend | amends the last commit, opens an editor |
git stash | stashes the current staged files |
git stash list | lists all stashes |
git stash pop | applys and deletes the uppermost stash |
git stash apply | only applys the uppermost stash |
git stash drop | drops the uppermost stash |
git reset ref | performs a mixed reset, resetting index and HEAD |
git reset --soft ref | performs a soft reset, only resetting HEAD |
git reset --hard ref | performs a hard reset,/ resetting index, HEAD and work tree |
git checkout ref | check out ref, can be a commit or a branch, will enter detached HEAD mode |
Show diffs
Command | Function |
---|---|
git diff | shows differences between HEAD and current status |
git diff --staged | shows differences between stage (index) and HEAD |
git diff ref1..ref2 | shows the diffs between ref1 and ref2 |
git diff ref1...ref2 | shows only diffs which are either only on ref1 or on ref2, but not on both |
Working with git objects
Command | Function |
---|---|
git show objectId | shows the object, if it is a commit it adds a diff |
git cat-file -p objectId | prints the contents of a git object |
git cat-file -t objectId | prints the type of a git object |
git fsck --full | checks the repository for errors and print dangling objects |
Debugging with git
Command | Function |
---|---|
git blame file | annotates each line of the file with the commit it was changed |
git blame -C file | like git blame, but tries and identifiy copies |
git blame -M file | like git blame, but tries and identifiy moved lines |
git bisect start | starts git bisect |
git bisect bad [ref] | marks the current commit or ref as bad |
git bisect good [ref] | marks the current commit or ref as good |
git bisect reset | ends bisecting |
Inspect the hsitory
Command | Function |
---|---|
git log | shows the commit history, by default in --fuller format |
git log --oneline | shows the git commit history with only short commitHash and commit message |
git log --oneline --graph -a | shows the commit history of all branches in a graph and one line messages |
git reflog | prints the reflog |
git log -g | prints the reflog like a normal log |
Working with branches
Command | Function |
---|---|
git branch | shows local branches |
git branch -a | shows all branches |
git branch mybranch | creates mybranch |
bit branch -m newname | renames the current branch to newname |
git switch branchname | switches to branchname |
git switch - | switches back to the previous branch |
git switch -c mybranch | creates and switches to mybranch |
git checkout ref | check out ref, can be a commit or a branch |
git checkout -c mybranch | creates and switches to mybranch |
git merge branchname | merges branchname into current branch |
git rebase branchname | rebases current branch onto branchname |
git rebase -i ref | starts an interactive rebase at ref |
git cherry-pick commitHash | copies the contents of the commit into the current branch |
Working with remotes
Command | Function |
---|---|
git clone repoAddress [localname] | clones from repoAddress into localname if provided |
git remote -v | shows the configured remotes |
git pull | pulls changes from the remote |
git fetch | fetches, but does not pull, changes from remote |
git push | pushes changes to the remote |
git push -u origin remotebranch | set up a tracking branch remotebranch on remote origin and pushes changes |
git push --force-with-lease | force pushes changes but only if the tip of the tracking branch is at the exected commit |
git push --force | force pushes changes and overwrites |
Working with git objects
Command | Function |
---|---|
git blame file | annotates each line of the file with the commit it was changed |
git blame -C file | like git blame, but tries and identifiy copies |
git blame -M file | like git blame, but tries and identifiy moved lines |
git bisect start | starts git bisect |
git bisect bad [ref] | marks the current commit or ref as bad |
git bisect good [ref] | marks the current commit or ref as good |
git bisect reset | ends bisecting |
Debugging with git
Command | Function |
---|---|
git show objectId | shows the object, if it is a commit it adds a diff |
git cat-file -p objectId | prints the contents of a git object |
git cat-file -t objectId | prints the type of a git object |
git fsck --full | checks the repository for errors and print dangling objects |