Skip to content

Instantly share code, notes, and snippets.

@sagerio
Last active May 17, 2024 18:28
Show Gist options
  • Save sagerio/81c91b3cd95984d77956b7ed70d20ad2 to your computer and use it in GitHub Desktop.
Save sagerio/81c91b3cd95984d77956b7ed70d20ad2 to your computer and use it in GitHub Desktop.
Git commands

FORCE REMOTE BRANCH TO LOCAL BRANCH

$ git reset --hard origin/master

UPDATE GIT (WINDOWS)

$ git update-git-for-windows

CONFIG

$ git config --list --show-origin

LINE ENDING SETTINGS

on Windows: “Checkout Windows-style, commit Unix-style”
Git will convert LF to CRLF when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects, this is the recommended setting on Windows
$ git config --global core.autocrlf true

on Unix: “Checkout as-is, commit Unix-style”
Git will not perform any conversion when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects this is the recommended setting on Unix
$ git config --global core.autocrlf input

CLEAN

$ git clean [-n: dry run] [-d: directories] [-f: really run]

CLONE

$ git init
➜  .gitignore
$ git clone <https://gitlab.com/sager.io/TinyPGP.git>
|
$ git remote add origin <https://gitlab.com/sager.io/TinyPGP.git>
$ git remote add github git@github.com:sagerio/Parse.NetCore.git
$ git push -u origin <my_branch> [--all]
$ git add --all
$ git commit -m "initial commit;"
-- oder --
$ git commit -am "commit message"
$ git push -u origin master
$ git remote set-url <origin> <https://gitlab.com/sager.io/TinyPGP.git>
$ git remote rm <origin>

FETCH

$ git branch -a | --all
$ git checkout -t origin/<branchname>

SET UPSTREAM

$ git branch -u github/master

STATUS

$ git status -u|--untracked-files  # alle files, nicht nur Ordner
$ git status --ignore  # zeige ignorierte Dateien

PUSH TO NON-BARE REPOSITORY

$ git init server && cd server
$ touch a && git add . && git commit -m 0
$ git config --local receive.denyCurrentBranch updateInstead
$ cd .. && git clone server local && cd local
$ touch b && git add . && git commit -m 1
$ git push origin master:master
$ cd ../server && ls
➜ a b

CHANGING LAST COMMIT MESSAGE

$ git commit --amend [--no-edit] -m “<new_message>”

CHANGING OLDER COMMIT MESSAGES

$ git rebase -i HEAD~<commit_count>
$ “pick” ➜ “reword”
$ git push --force

REVERT ALL CHANGES

$ git checkout -- .
$ git reset --hard

UNDO CHANGES TO FILE

$ git restore <filename>

UNSTAGE FILE

$ git restore --staged <filename>

VIEWING UNPUSHED COMMITS

$ git log origin/master..HEAD

SUBTREE

"ghpages": "ng build --prod --base-href "https://sagerobert.github.io/\"",
"gsub": "$ git subtree push --prefix dist origin master"

CHANGE GIT

$ cd $HOME/Code/repo-directory
$ git remote rename origin bitbucket
$ git remote add origin https://gitlab.com/sager.io/TinyPGP.git
$ git push origin master
$ git remote rm bitbucket

MERGE

$ git checkout a (you will switch to branch a)
$ git merge b (this will merge all changes from branch b into a)
$ git commit -a (this will commit your changes)

CHERRY-PICK

$ git cherry-pick <sha>

REBASE

(in den aufnehmenden branch wechseln)
$ git checkout master
$ git rebase <abgebender branch>
--oder--
$ git rebase <sourcebranch> <targetbranch>

TAGS

TAG LÖSCHEN

$ git tag -d <tag-name>
$ git push origin :refs/tags/<tag-name>

TAG ANLEGEN

$ git tag -a <tag-name> -m "<beschreibung>" (kommentierter tag)
-- oder --
$ git tag <tag-name> (einfacher tag)

NACHTRÄGLICH TAGGEN

$ git tag -a <tag-name> -m '<beschreibung>' <sha>

TAG ANZEIGEN

$ git tag
$ git show <tag-name>

TAGS VERÖFFENTLICHEN

$ git push origin <tag-name>
-- oder alle tags --
$ git push origin --tags

REMOTES

REMOTE INFO

$ git remote -vv

PUSH TO MULTIPLE REMOTES

$ git remote add ALL <url1>
$ git remote set-url --add --push ALL <url1>
$ git remote set-url --add --push ALL <url2>
$ git push ALL <branchname>

BRANCHES

BRANCH WECHSELN

$ git switch <branchname>

NEUEN BRANCH ERSTELLEN

$ git switch --create <branch_name>
$ git branch <new_branch_name> <branch_name>
$ git switch [--merge] <new_branch_name>  # incl. uncommiteed changes

CREATE BRANCH FROM REMOTE BRANCH

$ git checkout -b test <name of remote>/test
--- or the shorthand ---
$ git checkout -t[--track] <name of remote>/test

PUSH LOCAL BRANCH TO REMOTE

$ git push [-u|--set-upstream] <remotename> <branchname>

DELETE REMOTE BRANCH

$ git push <remote_name> --delete <branch_name>

SHOW REMOTE DETAILS

$ git remote show <remotename>

DELETE BRANCHES REMOTE THAT ARE NOT EXISTING LOCAL

$ git remote prune [-n|--dry-run] <remotename>

SKIP HOOKS

$ git commit [-n|--no-verify] ...

VIEW SINGLE FILE IN OTHER BRANCH

$ git show <branch>:<file>
$ git show <branch>:<file> > <exported_file>

DELETE BRANCH

$ git branch -d [-D] <branch>
$ git push origin --delete <branch>

GELÖSCHTEN BRANCH WIEDERHERSTELLEN

$ git checkout -b <branch> <sha>

SHOW ALL BRANCHES

$ git branch [-a|--all]

OVERWRITE BRANCH "A" WITH CONTENTS OF BRANCH "B"

$ git checkout b                        # source name
$ git merge [-s ours | -X --theirs ] a  # target name
$ git checkout a                        # target name
$ git merge b                           # source name
[ourstheirs](https://nitaym.github.io/ourstheirs/)

RENAME BRANCH

$ git branch -m <newname>
$ git branch -m <oldname> <newname>

DIFF

VERGLEICHT 2 BRANCHES

$ git diff [--name-only|--name-status] master..development

VERGLEICHT DATEI AUS 2 BRANCHES

$ git diff <branch1> <branch2> -- <file.ext>
$ git diff <branch1>..[.]<branch2> [--compact-summary]

AKTUELLES MIT LETZTEM COMMIT VERGLEICHEN

$ git show
$ git (diff|difftool) (HEAD|@)(^|~) [(HEAD|@)]

DIFF BETWEEN HEAD AND ANY COMMIT

$ git diff <sha> [HEAD]

LOG

$ git log --pretty=oneline
$ git reflog

SHOW

$ git show <commit-sha> --oneline
$ git show <branch>:<file.ext> [ > export.ext ]
$ git show REVISION:/path/to/file
$ git cat-file -p <branch>:<path/to/file>

UNDO COMMIT

$ git commit -m "some big bullshit"
…
$ git reflog
$ git reset --hard HEAD@{5}

IGNORE FUTURE REVISIONS TO A FILE

$ git update-index --skip-worktree <file>
That will ignore changes to that file, both local and upstream, until you decide to allow them again with:
$ git update-index --no-skip-worktree <file>
get a list of files that are marked skipped with:
$ git ls-files -v . | grep ^S

IGNORE ALREADY TRACKED FILES

$ git rm --cached <file>
If editing:
$ git update-index --assume-unchanged <file>
$ git add --all && git commit -m "removed ignored files"

CREATE NEW BRANCH FROM EXISTING BRANCH LOCALLY

$ git checkout -b <new branch name> <source branch name>
- oder -
$ git branch <new branch name> <source branch name>
$ git checkout -b <new branch name>  # with changes

VIEW SINGLE FILE IN OTHER BRANCH

$ git show <branch>:<path/to/file>

STASH

$ git stash list [<options>]
$ git stash show [stash@{n}] -p, --patch: full diff
$ git stash drop [-q|--quiet] [<stash>]
$ git stash apply [stash]
$ git stash clear  # delete **all** stashes
$ git stash save [-u|--include-untracked] [-p|--patch] [-a|--all] [message]
$ git stash pop [stash@{n}]
$ git checkout stash@{n} -- <filename>
$ git stash branch <name> <stash@{n}>  # new branch from stash

RECOVER DELETED STASH

$ git stash drop <stash> ➜ Dropped stash@{1} (<sha>)
$ git stash store -m "test" <sha>

STASH SINGLE FILE

$ git stash push -m "this is a single stash" path/to/single.file

DIFF WITH STASH

$ git stash show -p [stash@{n}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment