Linux Server setup

Git Cheat Sheet

Eine Liste von gängigen Git-Befehlen.


help

__$ git help -a         # list all
__$ git help commit     # help for a specific command: commit

config

__$ git config --list                               # show config of system, global and local configs
__$ git config --list --show-origin                 # show configs and their origins
__$ git config --system --list
__$ git config --global --list
__$ git config --local --list
__$ git config --global user.name "New Name"
__$ git config --global --edit                      # modify config in editor
__$ git config --global http.sslVerify false        # allows non ssl
__$ git config http.sslVerify false                 # allows non ssl for current repository
__$ git config --global init.defaultBranch main     # set default branchname to main

init

__$ git init
__$ git clone [URL]

Observe

status

__$ git status

show

__$ git show

log

__$ git log
__$ git log --oneline
__$ git log --oneline -n3 # last 3 lines
__$ git log --oneline --graph
__$ git log -p [file/directory]

reflog

__$ git reflog

diff

__$ git diff                                # compare working directory with stage
__$ git diff HEAD                           # compare working directory with stage
__$ git diff --cached                       # compare stage to last commit
__$ git diff commit1 commit2                # compare commit1 with commit2
__$ git diff origin/main                    # compare current branch with remote branch
__$ git diff branchABC origin/branchABC     # compare branch 'branchABC' with remote branch 'branchABC'
__$ git diff --name-status                  # lists just the filenames of modified files
__$ git diff --name-status [remote-branch] [local-branch]

Branches

branch

__$ git branch         # list all
__$ git branch -a      # list all local and remote
__$ git branch -av     # list all local and remote - verbose
__$ git branch -vv     # list all local branches along with their upstream branches
__$ git branch -r      # after fetch: list all branches
__$ git branch [new-branch]
__$ git branch -d [delete-branch]
__$ git remote update origin --prune # update remote branch list
__$ git push origin --delete [delete-remote-branch]

Set default upstream branch

__$ git branch --set-upstream-to=origin/main

checkout

__$ git checkout [branch]        # switch to branch
__$ git checkout [branch] -f     # switch to branch, get rid of detached head
__$ git checkout -b [checkout-and-create-new-branch]     # switch to branch, create if it does not exists
__$ git checkout -- filename     # revert changes of this file, get back from HEAD

Get back from detached head

__$ git checkout [branch] -f
__$ git pull origin [branch]

Rename branch (not default branch)

__$ git branch -m new-name                 # rename current branch
__$ git branch -m old-name new-name        # rename any branch
__$ git push origin :old-name new-name     # delete old-name branch on remote
__$ git push origin -u new-name            # reset upstream branch for new-name local branch

Rename default branch

__$ git checkout master
__$ git branch -m main
__$ git push -u origin main

# set new default branch to main via GUI
__$ git push origin --delete master

# Teammates todo
__$ git checkout master
__$ git branch -m master main
__$ git fetch
__$ git branch --unset-upstream     # reset upstream connection
__$ git branch -u origin/main       # set new upstream connection

Add, Commit, Tag

__$ git add [file/folder]     # add specific file/folder
__$ git add .                 # add all changes
__$ git commit -m "commit message"
__$ git commit -a -m "add files and commit"     # adding changes but ignores newly created files/folder
__$ git tag                              # list all tags
__$ git tag -a v1.2 -m "tag massage"     # create tag
__$ git tag -a v1.2 fe3kdwe              # tag an existign commit

reset

__$ git reset [file]
__$ git reset --soft      # revert local repository
__$ git reset --mixed     # revert stage and local repository
__$ git reset --hard      # revert working directory, stage and local repository
__$ git reset --hard [commit]
__$ git reset --hard [commit] && git clean -f     # undo local changes, get back to last commit, do not log this reset
__$ git reset --hard origin/main                  # revert from remote
__$ git reset --hard origin/main HEAD^            # reset to commit before HEAD
__$ git reset --hard HEAD~1                       # reset to a previous commit

rebase

__$ git rebase --onto [commit]

stash

__$ git stash
__$ git stash list
__$ git stash show
__$ git stash apply     # apply and leave in stash list
__$ git stash pop       # apply and remove from stash list
__$ git stash clear     # clear stash list
__$ git stash drop stash@{NUMBER}  # remove stash by listnumber

restore

__$ git restore [path/file]                   # discard changes on working tree
__$ git restore -s main~1 [path/file]         # one commit back with --source
__$ git restore -s main [path/file]           # restore back to HEAD
__$ git restore -s [commit] [path/file]       # restore from commit
__$ git restore -s [branch] [path/file]       # restore from branch
__$ git restore -s [branch]~2 [path/file]     # restore from branch, two commit back

Sync

fetch

__$ git fetch     # get remote HEAD, update local remote tree
__$ git fetch origin
__$ git fetch origin [branch]
__$ git fetch --all

pull

__$ git pull     # get remote HEAD, update local remote tree, merge into working directory
__$ git pull --rebase

push

__$ git push                        # post local HEAD to remote
__$ git push -u origin [branch]     # attach branch as remote-tracking-branch

Reset local repository to remote HEAD

__$ git fetch origin main
__$ git reset --hard origin/main 

remote

__$ git remote show                 # some info about remote
__$ git remote add origin [URL]     # set remote url

Reset remote password (credentials)

__$ git credential reject
url=[REPO_URL]

# Enter twice

__$ git fetch
# or
__$ git push origin [branch]

# retype username and password