- [[#Setup]]
- [[#Setup Init]]
- [[#Stage Snapshot]]
- [[#Branch Merge]]
- [[#Inspect Compare]]
- [[#Tracking Path Changes]]
- [[#Ignoring Patterns]]
- [[#Share Update]]
- [[#Rewrite History]]
### Setup
Configuring user information used across all local repositories
___
set a name that is identifiable for credit when review version history:
```shell
git config --global user.name “[firstname lastname]”
```
set an email address that will be associated with each history marker:
```shell
git config --global user.email “[valid-email]”
```
set automatic command line coloring for Git for easy reviewing:
```shell
git config --global color.ui auto
```
### Setup & Init
Configuring user information, initializing and cloning repositories
___
initialize an existing directory as a Git repository:
```shell
git init
```
retrieve an entire repository from a hosted location via URL:
```shell
git clone [url]
```
### Stage & Snapshot
Working with snapshots and the Git staging area
___
show modified files in working directory, staged for your next commit:
```shell
git status
```
add a file as it looks now to your next commit (stage):
```shell
git add [file]
```
add all files except:
```shell
git add --all . :!'path/somefile.js'
```
unstage a file while retaining the changes in working directory:
```shell
git reset [file]
```
diff of what is changed but not staged:
```shell
git diff
```
diff of what is staged but not yet committed:
```shell
git diff --staged
```
commit your staged content as a new commit snapshot:
```shell
git commit -m “[descriptive message]”
```
### Branch & Merge
Isolating work in branches, changing context, and integrating changes
___
list your branches. a * will appear next to the currently active branch:
```shell
git branch
```
create a new branch at the current commit:
```shell
git branch [branch-name]
```
switch to another branch and check it out into your working directory:
```shell
git checkout
```
merge the specified branch’s history into the current one:
```shell
git merge [branch]
```
show all commits in the current branch’s history:
```shell
git log
```
### Inspect & Compare
Examining logs, diffs and object information
___
show the commit history for the currently active branch:
```shell
git log
```
show the commits on branch A that are not on branch B:
```shell
git log branchB..branchA
```
show the commits that changed file, even across renames:
```shell
git log --follow [file]
```
show the diff of what is in branchA that is not in branchB:
```shell
git diff branchB...branchA
```
show any object in Git in human-readable format:
```shell
git show [SHA]
```
### Tracking Path Changes
Versioning file removes and path changes
___
delete the file from project and stage the removal for commit
```shell
git rm [file]
```
change an existing file path and stage the move
```shell
git mv [existing-path] [new-path]
```
show all commit logs with indication of any paths that moved
```shell
git log --stat -M
```
### Ignoring Patterns
Preventing unintentional staging or commiting of files
___
Save a file with desired patterns as `.gitignore` with either direct string matches or wildcard globs:
```shell
logs/
*.notes
pattern*/
```
system wide ignore pattern for all local repositories:
```shell
git config --global core.excludesfile [file]
```
### Share & Update
Retrieving updates from another repository and updating local repos
___
add a git URL as an alias
```shell
git remote add [alias] [url]
```
fetch down all the branches from that Git remote
```shell
git fetch [alias]
```
merge a remote branch into your current branch to bring it up to date
```shell
git merge [alias]/[branch]
```
Transmit local branch commits to the remote repository branch
```shell
git push [alias] [branch]
```
fetch and merge any commits from the tracking remote branch
```shell
git pull
```
### Rewrite History
Rewriting branches, updating commits and clearing history
___
apply any commits of current branch ahead of specified one:
```shell
git rebase [branch]
```
clear staging area, rewrite working tree from specified commit:
```shell
git reset --hard [commit]
```
### Temporary Commits
Temporarily store modified, tracked files in order to change branches
___
Save modified and staged changes:
```shell
git stash
```
list stack-order of stashed file changes:
```shell
git stash list
``````
write working from top of stash stack:
```shell
git stash pop
```
discard the changes from top of stash stack
```shell
git stash drop
```
___
**Tags**: #git #cheatsheet