Skip to content

Instantly share code, notes, and snippets.

@dannylee8
Created March 27, 2020 05:33
Show Gist options
  • Save dannylee8/907a438ce07fefa776c4d2e9d0f906c7 to your computer and use it in GitHub Desktop.
Save dannylee8/907a438ce07fefa776c4d2e9d0f906c7 to your computer and use it in GitHub Desktop.
git-cheatsheet
## Initial configuration
$ git config --global user.name "<your name>"
Sets the user name which will be credited with commit transactions.
$ git config --global user.email "<email addie>"
Sets the email address which will be attached to the commit transactions.
$ git config --global color-ui auto
Turns on the line coloring option which makes Git output more appealing to the eye.
## Other Git Config Commands
$ git config --global alias.example <git-command>
Creates a shortcut command for Git. (e.g. "git config --global alias.aa add --all" will add the alias to your .gitconfig file and allow you to use "git aa" to execute the command.
$ git config --system core.editor <editor>
Sets the default text editor for users on the machine.
$ git config --global --edit
Opens your global configuration file (.gitconfig) in your default text editor.
## Git Basics
```
$ git init
or
$ git init <directory>
```
Creates an empty Git repository in the current directory (no arguments) or the specified directory.
```
$ git clone <url>
```
## Staging and Snapshots
Downloads and clones a repository from a hosted location at <url> either via HTTP or SSH.
```
$ git status
```
Shows which files are staged, unstaged, and untracked.
```
$ git show [commit]
```
Outputs metadata and content changes of the specified commit
```
$ git add <file>
```
Adds a file in current state, to your next commit (stage)
```
$ git commit -m "clever message"
```
Commits your staged content as a new commit snapshot and records file snapshot into version history
```
$ git reset <filename>
```
Unstage a file while retaining the changes made to it
```
$ git log
```
Displays the commit (version) history between your index and working directory.
```
$ git log --follow [file]
```
Lists version history for a file, including renames
```
$ git log branchB..branchA
```
show the commits on branchA that are not on branchB
```
$ git diff
```
Shows files that have changed, but are not staged
```
$ git diff HEAD
```
Show difference between working directory and last commit
```
$ git diff --staged
```
Shows the difference between what is staged but not yet committed
```
$ git diff --cached
```
Shows the difference between what is staged changes and last commit
```
$ git diff [first-branch] [second-branch]
```
Shows the difference between two branches
## Branching
Branching allows you to isolate work, by "checking out" a branch you can make changes that do not effect other branches. Later you can merge them together.
```
$ git branch <branch-name>
```
Creates a new branch
```
$ git checkout <branch-name>
```
Switches to the specified branch and updates the working directory
```
$ git checkout -b <branch-name>
```
Create and check out a new branch named "branch-name"
```
$ git merge <branch>
```
Combines the specified branch history into the current branch. This is usually done via Pull Requests.
```
$ git branch -d <branch-name>
```
Deletes the specified branch.
```
$ git log
```
Shows all commits in the current branch's history
## Synchronize changes
Sync changes between local repository and the remote repo.
```
$ git fetch
```
Downloads all history from the remote tracking branches
```
$ git fetch <remote> <branch>
```
Fetch specified remotes copy of branch.
```
$ git merge
```
Combines remote tracking branch into current local branch
```
$ git push <remote>/<branch>
```
Uploads all local branch commits to specified remote repository (or default without argument) and create a branch if it doesnt exist.
```
$ git pull <remote>
```
Fetch the specified remote's copy (or default without argument) and merge it with local copy. A combo of git fetch and merge.
```
$ git remote add <alias> <url>
```
Create a new connection to a remote repository. After adding a remote, you can use "alias" as a shortcut
## Reverting/Rewriting Git History
> erase mistakes and craft replacement history
```
$ git reset [commit]
```
Undoes all commits after [commit], preserving changes locally. With no argument resets to most recent commit.
```
$ git reset [file]
```
Removes file from staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
```
$ git reset --hard
```
Reset staging area and working directory to match most recent commit and overwrites all changes in the working directory
```
$ git reset --hard [commit]
```
Discards all history and changes back to the specified commit
```
$ git rebase [branch]
```
Apply any commits of current branch ahead of specified one
```
$ git rebase <base>
```
Rebase the current branch onto "base", this can be a commit ID, branch name, a tag, or a relative reference to HEAD.
```
$ git rebase -i <base>
```
Interactively rebase current branch onto "base". Launches editor to enter commands for how each commit will be transferred to new base.
```
$ git revert <commit>
```
Create new commit that undoes all of the changes made in commit, then apply it to the current branch
```
$ git reflog
```
Show a log of changes to the local repository's HEAD. Add --relative-date flag to show date info or --all to show all refs
***If you need to change commits that exist on the remote, proceed with caution.***
## Temporary Commits
```
$ git stash
```
Save modified and staged changes
```
$ git stash list
```
list stack-order of stashed file changes
```
$ git stash pop
```
write working from top of stash stack
```
$ git stash list
```
discard the changes from top of stash stack
## Git Log
```
$ git log -<limit>
```
Limit number of commits by "limit". "git log -5 will limit to 5 commits"
```
$ git log --oneline
```
Condense each commit to a single line
```
$ git log -p
```
Display the full diff of each commit
```
$ git log --stat
```
Include which files were altered and the relative number of lines that were added or deleted from each of them
```
$ git log --author="<pattern>"
```
Search for commits by a particular author
```
$ git log --grep="<pattern>"
```
Search for commits with commit message that matches "pattern"
```
$ git log <since>..<until>
```
Shows commits that occur between since and until. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference
```
$ git log -- <file>
```
Only display commits that have the specified file
## Glossary
- **git**: an open source, distributed version-control system
- **GitHub**: a platform for hosting and collaborating on Git repositories
- **commit**: a Git object, a snapshot of your entire repository compressed into a SHA
- **branch**: a lightweight movable pointer to a commit**clone**: a local version of a repository, including all commits and branches
- **remote**: a common repository on GitHub that all team member use to exchange their changes
- **fork**: a copy of a repository on GitHub owned by a different user
- **pull request**: a place to compare and discuss the differences introduced on a branch with reviews, comments, integrated tests, and more
- **HEAD**: representing your current directory, the HEAD pointer can be moved to a different branches, tags, or commits when using 'git checkout'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment