List of Top 10 important git commands with examples and explanations

Git is a distributed version control system that is used to manage software development projects. With Git, developers can collaborate on a project and keep track of all the changes made to the code over time.

Commonly used Git commands:

1. git init:

This command is used to create a new Git repository. For example:

git init my-project 

This command will create a new Git repository in a directory called my-project.

2. git clone:

This command is used to clone an existing Git repository. For example:

git clone https://github.com/user/repo.git 

This command will clone the repo.git repository from GitHub.

3. git add:

This command is used to stage changes to your files in Git. For example:

git add myfile.txt 

This command will stage the changes to the myfile.txt file for the next commit.

4. git commit:

This command is used to record changes to your files in Git. For example:

git commit -m "Added a new line to myfile.txt" 

This command will create a new commit that includes the changes to the myfile.txt file, along with a commit message that describes the changes.

5. git push:

This command is used to push your commits to a remote Git repository. For example:

git push origin main 

This command will push your commits to the main branch of the origin remote repository.

6. git pull:

This command is used to fetch and merge changes from a remote Git repository. For example:

git pull origin main 

This command will fetch and merge the changes from the main branch of the origin remote repository into your local repository.

7. git branch:

This command is used to list, create, or delete branches in Git. For example:

git branch 

This command will list all branches in your Git repository.

8. git checkout:

This command is used to switch between branches or restore files to a previous state. For example:

git checkout feature-branch 

This command will switch your working directory to the feature-branch branch.

9. git merge:

This command is used to merge changes from one branch into another. For example:

git merge feature-branch 

This command will merge the changes from the feature-branch branch into the current branch.

10. git status:

This command is used to show the status of your files in Git. For example:

git status

This command will show the status of your files, including which files have been modified, which files have been staged, and which files are untracked.

Some advanced-level Git commands:

11. git stash:

This command is used to temporarily save changes that are not yet ready to be committed. For example:

git stash 

This command will save your changes to the stash, allowing you to switch branches or perform other tasks without committing your changes.

12. git rebase:

This command is used to reapply changes from one branch to another. For example:

git rebase master 

This command will reapply the changes from the master branch onto the current branch.

13. git cherry-pick:

This command is used to apply a single commit from one branch to another. For example:

git cherry-pick abc123 

This command will apply the commit with the SHA-1 hash abc123 to the current branch.

14. git reset:

This command is used to reset your repository to a previous state. For example:

git reset HEAD~1 

This command will reset your repository to the state of the previous commit, keeping your changes in the working directory.

16. git reflog:

This command is used to show a log of all Git commands that have been executed in your repository. For example:

git reflog 

This command will show a log of all Git commands that have been executed in your repository, along with the corresponding SHA-1 hash of each commit.

17. git tag:

This command is used to create, list, or delete tags in Git. For example:

git tag v1.0.0 

This command will create a new tag called v1.0.0 at the current commit.

18. git bisect:

This command is used to perform a binary search to find the commit that introduced a bug. For example:

git bisect start git bisect bad HEAD git bisect good v1.0.0 

This command will start the bisect process, mark the current commit as bad, and mark the v1.0.0 tag as good. Git will then check out a middle commit and ask you whether it is good or bad, and continue the process until it has found the commit that introduced the bug.

These advanced Git commands can be very powerful, but they also require a deeper understanding of Git and its underlying mechanics. It is recommended to practice using these commands on a test repository before using them on a production project.

Comments