Git Top Daily Life Usage Commands
In this blog, we’ll explore the top most popular Git commands that developers use on a daily basis.
1. git init
The git init
command initializes a new Git repository in your project directory. It sets up the necessary infrastructure to start tracking changes.
$ git init
2. git clone
To obtain a copy of a remote repository on your local machine, you can use the git clone
command. This command creates a local clone of the repository, including all its branches and history.
$ git clone https://github.com/websparrow-org/spring.git
3. git status
The git status
command provides an overview of the current state, showing any modified, added, or deleted files.
$ git status
4. git add
Before committing changes, you need to add files to the staging area using the git add
command. It prepares them for inclusion in the next commit.
Add a single file:
$ git add file.txt
Add multiple files:
$ git add Test.java pom.xml ...
Add all files:
$ git add .
5. git commit
The git commit
command creates a new commit with the changes in the staging area and a descriptive message explaining the modifications made.
$ git commit -m "Handled the NPE"
6. git pull
To fetch and merge changes from a remote repository into your current branch, you can utilize the git pull
command. It updates your local branch with the latest commits from the remote repository.
$ git pull origin master
7. git push
When you’re ready to share your local commits with the remote repository, the git push
command comes into play. It pushes your changes to the designated remote repository.
$ git push origin master
8. git branch
To view all the branches in your repository, you can execute the git branch
command. It provides a list of branches and highlights the current branch.
$ git branch
9. git checkout
The git checkout
command allows you to switch between branches within the repository.
$ git checkout feature/user-crud
10. git merge
To incorporate changes from one branch into another, the git merge
command is essential. It merges the specified branch into the current branch, integrating the modifications.
$ git merge feature/global-exception-handler
11. git fetch
The git fetch
command retrieves the latest changes from a remote repository without merging them into your local branches. It is useful for reviewing changes before merging.
$ git fetch origin
12. git log
The git log
command displays a detailed log of all commits in your repository. It provides information such as commit hashes, authors, timestamps, and commit messages.
$ git log
13. git diff
To view the differences between your working directory and the staging area, you can use the git diff
command. It highlights the lines that have changed.
$ git diff
14. git remote
When working with remote repositories, the git remote
command is invaluable. It lists the remote repositories associated with your local repository.
$ git remote -v
15. git reset
If you accidentally add files to the staging area, the git reset
command allows you to unstage them, removing them from the staging area.
$ git reset Test.java
References
- Getting Started with Git and Setup Development Environment
- Git Commands – Atlassian
- Git Commands- Git SCM