Git is a powerful and popular version control system that helps developers to manage their code repositories efficiently. With Git, developers can track changes, collaborate on projects, and maintain a history of their codebase. As a beginner, learning Git can be intimidating due to its many commands and features. In this article, we will explore the top 20 Git commands with examples to help you get started with Git.
$ git init
- git add The git add command is used to add files to the staging area. Files in the staging area are not yet committed to the repository but are ready to be committed.
$ git add file1.txt file2.txt
- git commit The git commit command is used to commit changes to the repository. Commits are snapshots of your project at a particular point in time.
$ git commit -m “Commit message”
- git status The git status command is used to show the current status of your repository. This command shows which files are in the staging area, which files are modified, and which files are not tracked by Git.
$ git status
- git log The git log command is used to display a log of all the commits made in the repository. This command shows the commit hash, author, date, and commit message.
$ git log
- git branch The git branch command is used to create, list, and delete branches in your repository. Branches are used to work on different features or versions of your code.
$ git branch branchname
- git checkout The git checkout command is used to switch between branches or to checkout a specific commit in your repository.
$ git checkout branchname
- git merge The git merge command is used to merge changes from one branch to another. This command takes the changes from the source branch and applies them to the target branch.
$ git merge branchname
- git pull The git pull command is used to fetch and merge changes from a remote repository. This command is used to keep your local repository up-to-date with the remote repository.
$ git pull origin master
- git push The git push command is used to push changes from your local repository to a remote repository. This command is used to share your changes with other developers.
$ git push origin master
- git clone The git clone command is used to create a copy of a remote repository on your local machine. This command is used to download a repository for the first time.
$ git clone https://github.com/user/repo.git
- git remote The git remote command is used to manage connections to remote repositories. This command shows the names of remote repositories that your local repository is connected to.
$ git remote add origin https://github.com/user/repo.git
- git fetch The git fetch command is used to fetch changes from a remote repository without merging them. This command updates your local repository with the changes from the remote repository.
$ git fetch origin master
- The git reset command is a powerful command in Git and can be used in several different ways depending on what you want to achieve.
Unstaging Changes with git reset One of the most common uses of git reset is to unstage changes that you have previously staged using the git add command. If you have made some changes to a file and added those changes to the staging area using git add, but then decide that you do not want to commit those changes, you can use the git reset command to unstage them.
Here’s an example. Let’s say you have made some changes to a file called “example.txt” and added those changes to the staging area using git add. To unstage those changes, you would run the following command:
git reset example.txt
- git stash Git stash command allows you to temporarily store changes made to your working directory so that you can switch to another branch without committing the changes. To stash your changes, use the following command:
This will save your changes to a stash, which you can retrieve later. To retrieve your stash, use the following command:
git stash apply
- git reset The git reset command allows you to unstage changes that you have made. This can be useful if you have made a lot of changes and want to unstage some of them before committing. To unstage changes, use the following command:
git reset HEAD
- git log The git log command shows you the commit history of your repository. This can be useful if you want to see who made changes to the repository and when they were made. To view the commit history, use the following command:
- git diff The git diff command shows you the difference between two versions of a file. This can be useful if you want to see what changes were made between two versions of a file. To view the differences between two versions of a file, use the following command:
- git branch The git branch command shows you the list of branches in your repository. This can be useful if you want to switch to a different branch or create a new branch. To view the list of branches, use the following command:
- git merge The git merge command allows you to merge two branches together. This can be useful if you have made changes in one branch that you want to incorporate into another branch. To merge two branches together, use the following command:
Conclusion In this article, we have covered the top 20 Git commands that every developer should know. By mastering these commands, you can improve your productivity and work more efficiently with Git.
Remember that Git is a powerful tool, and there are many other commands that you can use to manage your repositories. Keep learning and exploring Git to get the most out of this powerful version control system.