20 Git Commands Every Developer Should Know

20 Git Command I Use All The Time — Git CheatSheet

Nihal Parmar
4 min readApr 3, 2022

In this article, I just want to lay down a quick cheat sheet. . Recently, for various System Design projects, I have been collaborating with different technologies. There are a few commands that have become a staple for me.

So let’s get started:

1. git init

This command is used to initialize a project as a git repository.

2. git remote add origin <link-to-github-remote-repo>

Example:

git remote add origin https://github.com/nihalparmar/react/sample-repo.git

This command is used to add or connect to a remote repository.

3. git remote

This command is used to view connected remote repositories.

4. git status

This command is used to view the status of files in your local repository. Are files tracked? untracked? modified?

5. git add <file name>

Example:

git add index.htmlgit add index.html style.css style.scss

This command is used to stage modified or untracked files.

git add -A

This command is used to stage ALL unstaged files.

6. git reset

This command is used to unstage files.

7. git commit

This command is used to commit staged files.

git commit -m “<commit message here”>

Example:

git commit -m "added navigation bar"

This command is used to commit staged files AND provide a commit message for the commit history.

8. git push -u origin <branch-name>

Example:

git push -u origin master

This command is used to push committed files to the remote repository(aka GitHub) in the specified branch. Use this command for the very first time you push files to the remote repository. It will establish where you are pushing these files to. The next time(s) you push files, you can use git push

git push

This command is used to push committed files to the remote repository. You can only use this command to push files to the remote repository AFTER having pushed files using the previous command.

9. git fetch

This command is used to fetch the most updated version of your local repository. It checks for new files, new branches, deletions, etc.

10. git pull

This command is used to take that information you just fetched and pull it into your local repository. This updates your local repository to the most updated version of your remote repository.

11. git rm -r — cached <fileName>

Example:

git rm -r — cached config.js

This command is used to remove a file from the remote repository(GitHub) without deleting it in your local repository.

12. git branch

This command is used to preview the branch you are currently on

git branch -a

This command is used to preview all the branches in your local and remote repository.

git branch -r

This command is used to preview all the branches in your local repository (aka branches you have already visited).

git branch <branch-name>

This command is used to create a new branch in your local repository.

13. git checkout — track origin/<branch-name>

Example:

git checkout --track origin/develop

This command is used to switch branches. This is specifically for when you are visiting a branch (created in GitHub/remote repository) for the very first time.

14. git checkout <branch-name>

Example:

git checkout master
git checkout develop

This command is used to switch to branches you have already visited before.

15. git merge <branch name>

This command is used to merge two branches. To do this, enter the branch that you want to inherit the changes. And the branch name you would use along with this command is the branch that will provide the changes.

Example: master branch will inherit code from develop branch

git merge develop

16. git merge — abort

This command is used to abort a merge.

If there are no conflict errors, merges will always be successful. Ergo, this abort can only be used in situations where a merge failed.

How will you know this can be used?

For starters, your terminal will say merge failed. It may also tell you to fix the merge conflicts.

Here is another sign:

Screenshot of Git Bash

Look at the very end of the first line. In parentheses, it says (master). This is because we are in the master branch. If you are in the develop branch, it would say (develop).

If your merge fails, it will say (master|merging) or something like that. Maybe it says merge or maybe it’s a forward slash or maybe you’re in another branch. Regardless, you get the idea.

This indicates your merge failed.

git merge --abort would just abort the merge entirely.

17. git merge -X theirs <branch name>

Example:

git merge -X theirs develop

This command is used to merge two branches. And if there are merge conflicts, this command will just assume that you’d prefer the changes made in the mentioned branch (rather than the current one).

18. git reset — hard HEAD

This command will erase all the changes you’ve made in your local repository and update it to the latest version that was committed to GitHub.

19. git clean -f

This command is used to delete untracked files in your local repository

20. git clean -d

This command is used to delete untracked directories in your local repository. You can also combine it into git clean -fd to do both.

Thank you for reading! If you have some commands that you use all the time, please share them!

Check out my Github Profile: https://github.com/itznihal

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Nihal Parmar
Nihal Parmar

Written by Nihal Parmar

Software Engineer @Crest Data System|| 6⭐ @HackerRank || Tech-Enthusiast || GSSoC’21 || Full Stack Developer

No responses yet

Write a response