Installing and Configuring Git
Before we begin, you must install Git on your system. You can follow the Official Git Blog 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:
Commands |
Configurations |
git --version |
Check Git Installation |
git config --list --show-origin |
View all your Git settings |
git config --global user.name "Your Name" |
Set your Username |
git config --global user.email yourname@example.com |
Set your Email |
git config --global init.defaultBranch main |
Change Default Branch Name |
git config --global core.editor "'path to the code editor app'" |
Change your Code Editor |
Git Basics
After configuring Git, you can use different commands for performing tons of operations. Here are a few of them:
Initialize a repo
The first thing to do while using Git is to initialize 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:
Commands |
Tasks |
git add |
Adds certain files to the staging area. |
git add |
Adds the entire directory to the staging area. |
Commit Changes
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"
Status Commands
These commands are used to track changes, view folder history, and see the files in different stages. Here's how you use them:
Commands |
Tasks |
git log |
Display the entire commit history. |
git status |
Show the status of files in the repository, i.e., if the files are staged or not. |
git diff |
Show the difference between your staging area and the working directory. |
Git Branches
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:
Commands |
Tasks |
git branch |
List the branches in your repository. |
git branch -r |
Check remote branches that Git is tracking. |
git branch branch_name |
Create a new branch. |
git checkout branch_name |
Switch to a new branch. |
git checkout -b branch_name |
Create a new branch and switch to it at once. |
git branch -d branch_name |
Delete a branch. |
Git Merges
Git also allows you to merge different file versions, i.e., branches. Here are some Git merge commands:
Commands |
Tasks |
git merge branch_name |
Merge the branch you are currently working in. |
git merge origin/main |
Merge remote repo with a local repo. |
git merge --abort |
Abort a merge and start over. |
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:
Commands |
Tasks |
git remote add |
Create a new connection to a remote repo using its name and URL. |
git push -u origin branch_name |
Push a new branch to the remote repo. |
git push --all |
Push all of the changes in your local branches to the specified remote repository. |
git push |
Push the changes and commits in the local repository to the main branch of the remote repository. |
git remote -v |
See all the remote repositories for your local repository. |
git push -f |
Force a Push request. |
git fetch |
Fetch a specific branch from the repository. |
git pull |
Get the remote copy of the current branch and merge it to the local repo. |
Reverting Changes
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:
Commands |
Tasks |
git revert |
Create a commit that undoes all the changes made. |
git reset |
Remove files from the staging area. |
git clean -n |
Show the files that will be removed from the working directory. |
git clean -f |
Delete files from the working directory. |
Miscellaneous Commands
Here are a few miscellaneous Git commands that you might need to use in some scenarios:
Commands |
Task |
git tag [name] [commit sha] |
Add a reference tag name to a commit id. |
git tag |
List all tags. |
git clone |
Clone a repo located at onto local machine. |
git rebase |
Transfer completed work between branches. |
Frequently Asked Questions
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.
Some of the most basic Git commands include:
- git init
- git add
- git commit
- git push
- git pull
- git merge.
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.
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.