Git/Github Cheat Sheet
Tags: git, github
Published: 2020-08-16 16:10:31

#####*[Originally posted on Codepen on January 21, 2015](https://codepen.io/write/git-cheat-sheet)*

---
### Offical Doc
[Documentation](https://www.atlassian.com/git/tutorials)

### Git stash
[Handle blocking situations well](https://git-scm.com/docs/git-stash/en#_examples)

### Git Tutorial
[Advanced Git Tutorial (Level 1): Git Rebase](https://www.youtube.com/watch?v=6nolZKpiG_w)

---

#### Show commits oneline, author, commit date, change

```
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
```

`%h` = abbreviated commit hash
`%x09` = tab (character for code 9)
`%an` = author name
`%ad` = author date (format respects --date= option)
`%s` = subject

#### Git stash 

Use git stash to be able to checkout master

```
git stash // put changes aside temporarily
git checkout master
git stash pop // apply the changes back to the master local branch
```

#### Create and view repo from command line
```
gh repo create boostup/`basename "$PWD"`
gh repo view -w boostup/`basename "$PWD"`
```

#### Check for unpushed commits
```
git log --branches --not --remotes
```
or
```
git log @{u}..
```
or 
```
git log origin/master..HEAD 
```

#### Publish on gh-pages
[Follow instructions here](https://github.com/gitname/react-gh-pages)


#### Open repo on Git Hub
```
gh repo view -w boostup/`basename "$PWD"`
```

#### New Repo
```
gh repo create boostup/`basename "$PWD"`
```

#### Stop tracking a file
```
git rm --cached <file>
```

#### Download one file from any github repo
```
curl -o app.css https://raw.githubusercontent.com/angular-university/complete-typescript-course/section-7-6/client/src/assets/app.css
```

#### Showing repo remote URLs
```
git remote -v
```

#### Changing repo remote URLs
```
git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git
```

#### Showing remote tracking
```
git remote show origin
```

#### Setting remote tracking from local branch
```
git push --set-upstream origin <branch name>
```
or
```
git branch --set-upstream-to origin/master
```

#### Unsetting remote tracking by branch name
```
git config --unset branch.<branch name>.remote
git config --unset branch.<branch name>.merge
```

#### Unsetting remote tracking from branch
```
git branch --unset-upstream
```



#### Squasing commits
[Follow instructions here](http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html)

#### Finding commits where a file has been modified
```
git log --author=clark --stat <file path from project root>
```

#### Finding remote merged branches 
```
git branch -r --merged | grep origin | grep -Ev 'master|deamon'
```
Note: (grep -v 'master|deamon' => exlcuding branches with names that contain the words 'master' or 'deamon')

#### To amend the previous commit####
```
git add .
git commit --amend

git log --stat
```

#### To change git password when Windows password changes####
```
git config --global credential.helper wincred
```

#### To remove untracked files / directories do:

```
git clean -fdx
```

__-f__ - force
__-d__ - directories too
__-x__ - remove ignored files too ( don't use this if you don't want to remove ignored files)


#### Show a file on a particular commit

```
git show REVISION:path/to/file
```

#### See a graph of branch hierarchy

```
git log --all --graph --decorate --oneline --simplify-by-decoration
```

#### Throwing out all of the changes not committed or pushed

```
git checkout -f 
or 
git reset --HARD
```

#### Git Undo Last Commit (not yet pushed)

- keeping changes
```
git reset --soft HEAD^
```
- trashing changes
```
git reset --hard HEAD^
```


Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

```
git revert HEAD
```

This will create a new commit that reverses everything introduced by the accidental commit.

#### Create a new branch:

```
git checkout -b nombranche ( = git branch branchname + git checkout branchname )
```

#### Pushing local branch and tracking its remote counterpart:

```
git push -u origin nombranche
```

#### Display all local and remote branches:

```
git branch -a
```

#### Display remote branch commits:

```
git log origin/nombranche
```

#### Merging a branch into the build branch:

```
git checkout build
git pull origin build
git merge branchname
```

#### Deleting a local branch:

```
git branch -d nombranche
```

#### Deleting a remote branch:

```
git push origin :nombranche
ou 
git push origin --delete nombranche
```

#### Removing staged and working :

```
git reset --hard directory changes
```

#### Removing untracked files:

```
git clean -f -d 
```

#### Get list of files with conflicts:

``` git diff --name-only --diff-filter=U ``` or ``` git ls-files -u ```

#### Display commits by user:

```
git log --author=authorname
```

#### Diffing

```
git diff f98c5f277e28160ee5076e8ac4fabe7a60e8ca6f  --name-only
```

#### Creating a new tag

```
git tag tagname
```

#### Pushing the tag

```
git push origin branchname --tags
```

#### Deleting a pushed tag

- distant

```
git push origin :refs/tags/tagname
```

- local

```
git tag -d tagname
```

### Troubelshooting
#### Git asks me to commit or stash changes on checkout master, even though there are no changes
```
git checkout -- .
```