DEV Community

Cover image for 30+ Git Commands That I Frequently Use
Bhanu Teja Pachipulusu
Bhanu Teja Pachipulusu

Posted on • Updated on • Originally published at blog.bhanuteja.dev

30+ Git Commands That I Frequently Use

Hello World πŸ‘‹

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.

git clone

# 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
Enter fullscreen mode Exit fullscreen mode

git init

# Initializes the current directory as a git repo
git init
Enter fullscreen mode Exit fullscreen mode

git add

# 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 .
Enter fullscreen mode Exit fullscreen mode

git commit

# 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
Enter fullscreen mode Exit fullscreen mode

git status

# 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
Enter fullscreen mode Exit fullscreen mode

Screenshot 2020-11-13 at 10.59.31 AM.png

git log

# Shows the commit logs
git log
Enter fullscreen mode Exit fullscreen mode

Screenshot 2020-11-13 at 10.39.39 AM.png

git diff

# 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
Enter fullscreen mode Exit fullscreen mode

Screenshot 2020-11-13 at 10.41.43 AM.png

git remote

# 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
Enter fullscreen mode Exit fullscreen mode

Screenshot 2020-11-13 at 10.55.54 AM.png

git checkout

# 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
Enter fullscreen mode Exit fullscreen mode

git push

# 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 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
Enter fullscreen mode Exit fullscreen mode

git branch

# 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
Enter fullscreen mode Exit fullscreen mode

git clean

# Removes all the files and directories that are not yet tracked by git
git clean -fd
Enter fullscreen mode Exit fullscreen mode

git merge

# Merges the <branch> to the current branch
# git merge <branch>
git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

git pull

# Fetches the changes from the remote and merge it into local repo
git pull origin master
Enter fullscreen mode Exit fullscreen mode

git reset

# Removes all the changes to the tracked files that have not yet been committed
git reset --hard
Enter fullscreen mode Exit fullscreen mode

What's Next?

The next article will most probably be a part of My Review of Kent C. Dodds's EpicReact.Dev. Checkout the series page for more info.

Until Next Time πŸ‘‹

If you liked this article, check out

If you have any comments, please leave them below or you can also @ me on Twitter (@pbteja1998), or feel free to follow me.

Top comments (9)

Collapse
 
lulasvob profile image
LulaSvob • Edited

Always use force push with lease except when not possible:

git push --force-with-lease 
Enter fullscreen mode Exit fullscreen mode

And one more that you should consider starting to use:

git rebase 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gonzofish profile image
Matt Fehskens • Edited

And if it’s not possible without β€”force-with-lease, there’s probably a good reason and you should really think about what’s happening before forcing.

And rebase (especially interactive) is crucial to my everyday!

Collapse
 
vishnuharidas profile image
Vishnu Haridas • Edited

Undo the last commit:

git reset HEAD~
Enter fullscreen mode Exit fullscreen mode

This is one of the most upvoted question in StackOverflow.

I... use... this... a lot!

Collapse
 
leo8545 profile image
Sharjeel Ahmad

i didnt know about this one: git commit --amend

Thank you for sharing bro.

Collapse
 
bgrand_ch profile image
Benjamin Grand

Thanks!

git commit --amend πŸ™ŒπŸ™ŒπŸ™Œ

Collapse
 
mcgurkadam profile image
Adam McGurk

Yeah.....git clean is gonna change my life.

Collapse
 
ahmedghazi profile image
Ahmed

What terminal app do you use?

Collapse
 
bgrand_ch profile image
Benjamin Grand
Collapse
 
b_hantsi profile image
Bala Hantsi πŸ‡³πŸ‡¬

Awesome! The terminal looks cool