# 30+ Git Commands That I Frequently Use

In this article, I will list out all the git commands that I use very frequently. This is not in any way a complete list, just the commands that I use very often. This is intended to be used as a quick reference to perform an action that you want. 

- [Clone repo](#git-clone)
- [Clone repo into specified directory](#git-clone)
- [Initialize current directory as a git repo](#git-init)
- [Add a file](#git-add)
- [Add all the files in current directory](#git-add)
- [Add all the files including files in .gitignore](#git-add)
- [Create a new commit](#git-commit)
- [Add changes to existing commit](#git-commit)
- [Show status](#git-status)
- [Show status in short format](#git-status)
- [Show status in short format and show branch](#git-status)
- [Show the commit logs](#git-log)
- [Show the diff for unstaged files](#git-diff)
- [Show the diff for staged files](#git-diff)
- [Show all the remotes with URLs](#git-remote)
- [Add a new remote](#git-remote)
- [Change URL for existing remote](#git-remote)
- [Switch to another branch](#git-checkout)
- [Create a new branch and switch to it](#git-checkout)
- [Remove all the unstaged changes in the current directory](#git-checkout)
- [Remove all the unstaged changes for a file](#git-checkout)
- [Push local changes to the remote repo](#git-push)
- [Push and set the remote as upstream](#git-push)
- [Force push local changes to the remote repo](#git-push)
- [Delete the branch in the remote repo](#git-push)
- [Delete the branch locally](#git-branch)
- [Force delete the branch locally](#git-branch)
- [Remove all untracked files and directories](#git-clean)
- [Merge the branch to the current branch](#git-merge)
- [Fetch changes from the remote and merge](#git-pull)
- [Remove all the changes to the tracked files that haven't been committed yet](#git-reset)

### git clone
```sh
# Create 'blogs' folder and clone the 'pbteja1998/blogs' repo into it
git clone https://github.com/pbteja1998/blogs.git

# Create `my-blogs` folder and clone the `pbteja1998/blogs` repo into it
git clone https://github.com/pbteja1998/blogs.git my-blogs
```

### git init
```sh
# Initializes the current directory as a git repo
git init
```

### git add
```sh
# Adds the file contents to the index
# Ready to be committed next time you run `git commit`
# By default, Ignores the files present in `.gitignore`

# Add a single file
git add README.md

# Add all the files in the current directory
git add .

# Also adds the files present in `.gitignore`
git add -f .
```

### git commit
```sh
# Commits/Records the changes to the local repo
git commit -m "some message"

# Does not create a new commit
# Adds the changes to the most recent commit
git commit --amend
```

### git status
```sh
# Shows the status of the working tree
git status

# Shows the output in short format
git status -s

# Shows the branch even in short format
git status -sb
```

![Screenshot 2020-11-13 at 10.59.31 AM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605245389283/23Tok2fCR.png)

### git log
```sh
# Shows the commit logs
git log
```

![Screenshot 2020-11-13 at 10.39.39 AM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605244215553/QCNWhim_s.png)

### git diff
```sh
# Shows the changes between unstaged files and the commits
git diff

# Shows the changes between staged(ready-to-be-committed) files and the commits
git diff --staged
```

![Screenshot 2020-11-13 at 10.41.43 AM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605244442028/73XfwnHzS.png)

### git remote
```sh
# Shows all the remotes configured and their remote URL
git remote -v

# Adds a remote
# git remote add <remote-name> <remote-url>
git remote add upstream https://github.com/something/blogs.git

# Changes the URL of the remote
git remote set-url upstream https://github.com/some-thing/blogs.git
```

![Screenshot 2020-11-13 at 10.55.54 AM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1605245169242/6n7-C9q05.png)

### git checkout
```sh
# Switch to branch 
# git checkout <branch>
git checkout master

# Creates a new branch and switch to that
git checkout -b new-feature

# Removes all the unstaged changes in the current directory
git checkout .

# Removes all the unstaged changes for a file
git checkout -- README.md
```

### git push
```sh
# Pushes the local changes to the remote to keep it up-to-date
git push origin master

# Force push the local changes to the remote
# Usually git will not allow you to push to the remote if the remote has some commits that are not present in the local repo
# This will override that check and lets you force push to the remote
# This may cause the remote to lose some commits. So use it carefully.
git push -f origin master

# Push and set the remote as upstream 
# same as `git push --set-upstream origin feature-branch`
git push -u origin feature-branch

# Deletes the branch in the remote
# same as `git push --delete origin new-feature`
git push -d origin new-feature
```

### git branch
```sh
# Deletes the branch locally
# same as `git branch --delete feature-branch`
git branch -d feature-branch

# Force delete a branch even if it's not merged
# same as `git branch --delete --force feature-branch`
git branch -D feature-branch
```

### git clean
```sh
# Removes all the files and directories that are not yet tracked by git
git clean -fd
```

### git merge
```sh
# Merges the <branch> to the current branch
# git merge <branch>
git merge feature-branch
```

### git pull
```sh
# Fetches the changes from the remote and merge it into local repo
git pull origin master
```

### git reset
```sh
# Removes all the changes to the tracked files that have not yet been committed
git reset --hard
```

## What's Next?
The next article will most probably be a part of [My Review of Kent C. Dodds's EpicReact.Dev](https://hashnode.com/series/my-review-of-kent-c-doddss-epicreactdev-ckfv4gidh08efu9s1408v8tgp). Check out the series page for more info.

#### Until Next Time 👋

If you liked this article, check out
- [Create Your Own Super Simple URL Shortener](https://blog.bhanuteja.dev/create-your-own-super-simple-url-shortener)
- [Why you should start using HSL color format](https://blog.bhanuteja.dev/why-you-should-start-using-hsl-color-format)
- [Hyphenate when you justify text](https://blog.bhanuteja.dev/til-hyphenate-when-you-justify-text)

If you have any comments, please leave them below or you can also @ me on Twitter ([@pbteja1998](https://twitter.com/pbteja1998)), or feel free to follow me.

%%[newsletter]

