Basic Git commands every developer should know

Basic Git commands every developer should know

20 basic git commands you should know if you beginner

Table of contents

No heading

No headings in the article.

Hey everyone, as developers we have to work with git most of the time. And as a beginner, this can be a bit intimidating to work with git using terminal/commands. This is perfectly alright. There are around 160 commands in git, learning all these commands is a nightmare for developers out there. I am going to talk about the 10-15 basic git commands you will be going to use in your day-to-day workflows.

Following are the Git commands you need in your daily work:

1. git config

The commands set the author name and email address respectively to be used with your commits.

Usage:

  • To set a name that is identifiable for credit when reviewing version history.
git config --global user.name "username"
  • To set an email address that will be associated with each history marker.
git config --global user.email "email"

2. git init

This is the first command used to initialize the existing directory as a git repository.

Usage:

git init

3. git add

This command adds files to the staging area.

Usage:

This command can be used in three ways:

  • To add a specific file
git add [file_name]
  • To add a specific directory
git add [directory_name]
  • To add all unstaged files
git add .

4. git commit

This command is used to save the changes in the local repository. Each commit has a unique hashcode for you to refer to. It allows you to write a message with each commit which is considered the best practice when working with git.

Usage:

git commit -m "your commit message goes here"

5. git status

This command is used to check the current state of your repository.

Usage:

git status

6. git branch

This command is used in performing tasks related to branches like list, create, and delete.

Usage:

  • To get the list of all branches of the repository.
git branch -a
  • To create a new branch.
git branch [branch name]
  • to delete a branch.
git branch -d [branch name]

7. git checkout

This command is used to go to switch between the branches.

Usage:

  • Switch to an existing branch.
Git checkout [branch name]
  • Switch to a new branch.
Git checkout -b [branch name]

8. git merge

This command is used to integrate or combine one branch into another.

Usage:

git merge [branch]

9. git remote

This command is used to connect a local repository to the remote one.

Usage:

git remote <command> <remote_repository_name> <remote_repository_URL>

# or if you don't want to set a name:
git remote <command>  <remote_repository_URL>

Note - In place of command you can use the following:

  • add
  • remove
  • rename
  • set-head
  • set-url
  • set-url --add or --delete
  • set branches
  • get URL
  • prune
  • To list all the URLs attached to your project.
git remote -v

10. git clone

This command is used to create a local copy of an existing remote repository.

Usage:

git clone [URL]

11. git pull

This command is used to get the latest change made to a remote repository.

Usage:

git pull branch_name

12. git push

This command pushes the changes made locally to the remote repository.

Usage:

git push origin [branch-name]

13. git stash

Store the changes made in the local repo but did not commit. This command is used when you don’t want to commit the changes made but want to store them locally.

Usage:

  • To Save modified and staged changes.
git stash -u
  • To List StackOrder of stashed file changes.
git stash list
  • To bring back your temporarily stored files.
git stash pop
  • To delete temporary stored, modified, and tracked files.
git stash clear

14. git log

This command is used to show the commit history of a repository in chronological order.

Usage:

git log

15. git rm

This command deletes the file from your working directory and stages the deletion.

Usage:

git rm [file]

16. git rebase

This command is used to apply any commits of the current branch ahead of the specified one. It is used if you need to rewrite the history of a project.

Usage:

git rebase [branch-name]

17. git revert

This command is used to revert some existing commits.

Usage:

git revert <insert bad commit hash here>

18. git reset

This command is about updating your branch and moving the tip in order to add or remove commits from the branch.

Usage:

  • This command is used to delete the commits.
git reset [hashcodeId]

Note- hash code Id can be seen from the git log

  • Used to Unstaged a file while retaining the changes in the working directory.
git reset [file]

19. git restore

This command is about restoring files in the working tree from either the index or another commit. This command does not update your branch. The command can also be used to restore files in the index from another commit.

Usage:

git restore -SW

20. git diff

This command is a widely used tool to track changes.

Usage:

  • This command is used to see the difference in what is changed, but not staged.
git diff
  • to see the difference in what is staged, but not committed.
git diff --staged
  • to see the difference between the 2 branches.
git diff Branch A...Branch B

Thank you for reading. I hope this blog had helped you in understanding the basic git commands. You can bookmark this blog for your reference in case required in the future.

If you have any questions or feedback let me know in the comment section down below. And we connect on Twitteror LinkedIn for more updates on my journey in tech.