Useful Git Commands
Tags: git, tutorial, programming
Published: 2022-05-25 20:06:46

Git is a fantastic technology that not only provides historical tracking of source code throughout development, but also enables multiple developers to work on the same project without stepping on each others feet. 

I wanted to create a sort of *cheat sheet* for git commands that I find myself using often. I will continue to update this post as I discover other handy git commands.
__________
# Starter Git Commands:
#### 1. Create and checkout a new branch
```shell
git checkout -b <branch>
```

#### 2. Discard changes to a file.
```shell
git checkout -- <file>
```

#### 3. Rename a local branch
```shell
git branch -m <oldName> <newName>
```

#### 4. Merge a specified branch into the currently checked out branch.

```shell
git merge <branch>
```

![GIT MERGE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38p9t5vbj0wb7oux40eu.gif)
__________
# More Git Commands:

#### 1. Stage all changes
```shell
git add -A 
```
stage all new, modified or removed files to the working tree. Note this is different to `git add .` which only stages new and existing files.
<br>

#### 2. Review all changes
```shell
git diff HEAD
```
The same as `git diff` but reviews all changes rather than just those unstaged.
<br>

#### 3. Remove a file from the repo but keep it in your project directory
```shell
git rm --cached <filename>
```
Very useful when you have files you don't want tracked that you forgot to specify in `.gitignore` that have previously committed. After adding them to .gitignore run this command to ensure they are removed from the repo without deleting them from your local project directory.
<br>


#### 4. Reset all local files to the last commit
```shell
git reset --hard
```
###### _**DANGER** - exercise caution when using this command as it has the potential to throw away hours of work._

I often find I have uncommitted changes in a branch I've been messing around in (whitespace, print statements) git doesn't let you checkout another branch with uncommitted changes so this is a quick way to reset your project directory to the HEAD.
<br>

#### 5. Undo the last commit
```shell
git reset --hard HEAD^
```
###### _**DANGER** - Can also cause unintentional loss of work._

We've all made commits we immediately regret... Use this command to destroy the changes from the last commit.
<br>

#### 6. Modify the previous commit
```shell
git commit --amend -m <newMsg>
```
Adding the `-m` option allows you modify the previous commit message. You can also stage new changes to the commit and the previous commit will inherit them.
<br>

#### 7. Remove all branches that no longer exist on remote
```shell
git branch --merged | egrep -v "(^\*|master|main|<branchToSave>)" | xargs git branch -d
```
###### _**DANGER** - this will delete any newly created branches that have not yet been pushed to the remote repo. be sure to specify special cases as in `<branchToSave>`._
This can be used to clean up your local repository if you have lots of old branches that have since been merged. If you want to learn how this works see [this great stack overflow post](https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged?page=1&tab=scoredesc#tab-top).
<br>


#### 8. Cache git credentials for a set amount of time
```shell
git config credential.helper 'cache --timeout 28800' # 8hrs
```
This saves you entering your git user/password every time you want to push/pull
The `--global` flag can be added to do this across all repositories.
<br>