We can't remember every single Git command, no matter how hard we try. Remembering these commands is also unnecessary as we can easily search them on Google.
In this blog, we will briefly cover some of the most important Git commands. So, let us jump right into it.
Installing and Configuring Git
Before we begin, you must install Git on your system. You can follow the blogs below to learn more about installing Git on different operating systems:
After you have installed Git, you need to configure it. Here's how you do it:
After configuring Git, you can use different commands to perform many operations. Here are a few of them:
Initialize a repo
The first thing to do while using Git is initializing a repository with the git init command.
You can also initialize a directory to a certain folder on your computer using the following command:
git init <directory>
Example: git init C:\Program Files\Project
The git init command creates a hidden folder named .git/ in your project folder, which tracks changes to the files.
Add to the Staging Area
Git has a staging area, which is a temporary space where you add files you have made changes to before you commit those changes.
To add a file to the staging area, you can type the git add command. There are two versions of this command:
You can commit changes to your local repository once you have entered the files into the staging area.
Here's the Git commit command:
git commit -m <message>
where <message> can be any information about the commit.
Example: git commit -m "New feature added"
These commands are used to track changes, view folder history, and see the files in different stages. Here's how you use them:
Creating a new branch in Git is basically creating a new version of your file. As a developer, you need to be able to work with branches. Here are some commands for it:
Git also allows you to merge different file versions, i.e., branches. Here are some Git merge commands:
Connecting With Remote Repositories
The best part about Git is that we can connect it with a remote repository and share our files with others. Here are a few commands for this purpose:
When you mistakenly perform a commit, you need to be able to revert the changes back. You might also need to remove files from the staging area.
Git allows you to do this with these commands:
Here are a few miscellaneous Git commands that you might need to use in some scenarios:
Frequently Asked Questions
1. What is a Git cheat sheet?
A Git cheat sheet is basically a list of important Git commands with short descriptions that you can use when you can't remember a command.
2. What are the basic Git commands?
Some of the most basic Git commands include:
- git init
- git add
- git commit
- git push
- git pull
- git merge.
3. How many Git commands are there?
It depends on the Git version you are using. But generally, there are 135 to 164 commands. But don't worry, you probably won't be using all of them. Also, most of these commands are sub-commands.
4. How do I run a Git command?
After you have installed Git, you can run it with the Command Prompt in Windows and the terminal in Linux and Mac. Alternatively, you can use Git Bash with Windows.
Subscribe to Programiz PRO Blog!
Be the first to receive the latest tutorial from Programiz by signing up to our email subscription. Also more bonus like inside looks on the latest feature and many more.