Skip to content

Instantly share code, notes, and snippets.

@snbeynon
Created July 31, 2018 12:47
Show Gist options
  • Save snbeynon/e31e763816ee55992ce5513a73f66091 to your computer and use it in GitHub Desktop.
Save snbeynon/e31e763816ee55992ce5513a73f66091 to your computer and use it in GitHub Desktop.
Parameter placeholders denoted as `<`…`>`
Command | Explanation
------- | -----------
`git clone <repos-url> <dir>` | create `<dir>` if non-existent, and clone a git repos from `<repos-url>` there.
`git clone --depth <#> --branch <remote-branch> <repos-url> <dir>` | " … " clone only the last `<#>` commits of branch `<remote-branch>`. (a.k.a. *shallow copy*).
`git checkout -` | checkout previously checked out branch, i.e. similar to `cd -`
`git checkout -b <newBranch>` | create a new branch named `<newBranch>`, and switch to it.
`git checkout -b <newBranch> <trackingBranch>` | " … " and setup tracking branch.
`git branch -u <trackingBranch> (<branch>)` | set the (e.g. `upstream`) tracking branch of `<branch>` (or current branch, if none given), e.g. `git branch -u upstream/foo` OR `git branch -u upstream/foo localFoo`
`git branch -m <newName>` | rename current branch to `<newName>`.
`git branch -m <oldName> <newName>` | rename branch `<oldName>` to `<newName>`.
`git branch -a` | shows which branch is currently being worked on `(*)`, and all known remote branches (when last fetched).
`git branch -vv` | show which local branch is tracking which remote branch.
`git branch/tag --contains <hash-id>` | show which local branch or tag contains the commit with `<hash-id>`.
`git status` | show which branch is currently being worked on, and if there a staged commits or untracked files etc.
`git branch -d <branch>` | delete a `<branch>`. Use capital `-D` to force deletion, without being warned if the branch isn't merged yet.
`git push origin :<branch>` | delete a remote `<branch>` (pushing a blank thing to `<branch>`).
`git branch --merged`| show which branches have been merged into the current branch.
`git branch --no-merged` | " … " not been merged into the current branch.
`git checkout -- .` | discard changes in working directory (revert).
`git checkout -- *<string>*` | " … " that match the `<string>` and the wildcards.
`git checkout -p` | discard selected chunks of changes (revert)
Updates | Explanation
------- | -----------
`git fetch <remote-repos>` | update the local view of the `<remote-repos>` , but don't merge anything (stored in `.git/FETCH_HEAD`) Note: doesn't matter on which branch your are.
`git fetch --tags/t <remote-repos>` | fetches all the references to tags from `<remote-repos>`.
`git fetch <remote-repos> --prune --dry-run` | syncs the the locally listed remote branches with the actual remote branches, and removes local references to remote branches that no longer exist. Add `--dryrun` to check what would be removed.
`git pull` | git fetch, followed by a merge on the current branch.
`git add .` | add all changed files to the staged commit.
`git add --all` | add all changed files to the staged commit, including files that have been *moved*, *deleted* or *renamed*.
`git add -p <file>` | add specific lines to staged commit, by editing (`e`) the chunks of interest (optionally, of `<file>`); remove `-` and `+`s.
`git ls-files` | show files tracked by git.
`git commit` | opens `$EDITOR` to create commit message; after closing the message, the commit is done.
`git commit -m "<message>"` | commit with `<message>`.
`git commit -a -m "<message>"` | add and commit all with `<message>`.
`git commit --amend` | commit, if any, to the last commit, replacing it with the amend’s commit message.
`git push origin <tag>` | push `<tag>` to remote repository.
`git push origin --tags` | push all tags to remote origin.
`git log --oneline --decorate --all --graph` | show the commit log in a simplified way (better overview).
`git log <branch>.. --oneline --no-merges` | show the commit log of the current branch (?) since the last rebase with `<branch>`, e.g. `git log master..`
`git log <remote>..` | show the commits that you (local `HEAD`) are ahead of the <remote> (as it was last fetched). The "outgoing changes". (i.e. short for `git log <remote>..HEAD`)
`git log ..<remote>` | show the commits from `<remote>` that are ahead of the current local `HEAD`. The "incoming changes".
`git reflog` | show all git operations `HEAD@{1}` `HEAD@{2}` `HEAD@{3}` etc.
`git reflog --date=iso` | " … " incl. date time.
Fix things | Explanation
------- | -----------
`git reset --hard origin/master` | revert any local changes and overwrite with `origin/master`.
`git reset --hard HEAD` | revert and overwrite with `HEAD` revision ( ! ) does not remove newly created files that haven’t been added to git.
`git reset --hard (HEAD@{#} / HEAD~$)` | revert to reflog `#`, or `$`commits before `HEAD`.
`git reset --hard <revision-hash>` | revert to the specific revision (get hash via `git log`).
`git rm --cached <file>` | untrack a file, e.g. after creating a `.gitignore` (`git status` will show deleted `<file>` » do a `git commit` for this change.)
`git stash` | stash all current changes (staged and unstaged), leaving working directory clean.
`git stash save "<message>"` | stash all current changes (staged and unstaged) with stash message `<message>`, leaving working directory clean.
`git stash list` | show all stashes.
`git stash show stash@{<#>}` | show which files were modified in the most recent stash, or stash number `<#>`.
`git stash show stash@{<#>} -p` | show diff (patched form) of changes in the particular stash.
`git stash apply` | apply the most recent stash without staging (does not remove stash).
`git stash apply stash@{<#>}` | apply stash number `<#>` without staging.
`git stash apply --index` | apply the most recent stash, and stage those files that were staged when we stashed them.
`git stash pop` | apply the most recent stash, and afterwards remove it.
`git stash drop stash@{<#>}` | remove stash #0 or (if given) number `<#>`.
`git stash clear` | remove all stashes.
Command | Explanation
------- | -----------
`git diff --color-words <branch> HEAD` | show diff (compare) of the `HEAD` with `<branch>`, and color blocks that changed.
`git diff <branch1> <branch2> -- <file>` | show diff of `<file>` between `<branch1>` and `<branch2>`.
`git diff <branch2>.. -- <file>` | show diff of `<file>` outgoing changes of current branch (..) to `<branch2>`.
`git diff --no-index <path1> <path2>` | show diff between two non-Git paths.
Command | Explanation
------- | -----------
`git merge <branch>` | merge `<branch>` in branch that is currently checked out.
`git merge --ff-only <branch>` | fast-forward merge of `<branch>` , e.g. when `git status` says *"…behind…, and can be fast-forwarded."*
`git merge --squash <branch>` | stage a merge of some `<branch>` as one single staged commit in the branch you have currently checked out.
`git cherry-pick <hash1> <hash2> …` | applies change(s) of commit `<hash1>` and `<hash2>` etc. on current branch, (!) including original committer name.
`git push origin HEAD:<destination-branch>` | push current `HEAD` of currently selected branch to existing or new branch `<destination-branch>` at a remote repos called `origin`.
`git push -u origin` etc. | push and track remote branch.
`git remote -v` | show info about the remote urls (push and pull).
Tags | Explanation
------- | -----------
`git tag` | list all tags.
`git tag -n` | list all tags, including any description or last commit message.
`git tag <name> <branch>` | create a tag of `<branch>` named `<name>` (nice way to archive a branch, because commit history is included).
`git tag <name> <branch> -m "<message>"` | create a tag of `<branch>` named `<name>`, and add `<message>` including a timestamp (date time).
`git show` | show diff of the `HEAD` and the previous commit.
`git show <tagName/hash>` | show commit history, including tag message if any, of the tag (`<tagName>` could be e.g. `archive/myMergedBranch`).
`git show HEAD~<n>` | show commit that was `<n>` commits before the `HEAD`. (`~0` = `HEAD` itself).
`git diff-tree --no-commit-id --name-only -r <hash-id>` | show which files were changed in commit `<hash-id>`.
`git diff-tree --no-commit-id --name-status -r HEAD` | " … " but with modification type prefix, e.g. `M path/to/somefile.txt`
`git tag -d <tag>` | delete tag
`git ls-remote --tags origin (<tag-name>)` | list all remote tags, or only `<tag-name>` if present.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment