Skip to main content

Basic Git Commands

What is Git?

Git is a distributed version control system that allows multiple people to work on a project at the same time. It is designed to handle everything from small to very large projects with speed and efficiency. It is easy to learn and has a tiny footprint with lightning fast performance.

In this tutorial, we will be covering some of the basic Git commands that are crucial for everyday programming.

Setting Up Git

Before we can start using Git, we need to set it up on our system. Here's how:

  1. Download and install Git from here.
  2. Open Terminal.
  3. Set your username in Git by typing the following command:
git config --global user.name "Your Name"
  1. Set your email address in Git by typing the following command:
git config --global user.email "[email protected]"

Basic Git Commands

git init

This command is used to start a new repository.

git init

git clone

If you want to get a copy of an existing Git repository, for example, a project you’d like to contribute to, the command you need is git clone. If you’re familiar with other VCS systems such as Subversion, you’ll notice that the command is "clone" and not "checkout".

git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

git add

This command adds a file to the staging area.

git add <file>

git commit

This command records or snapshots the file permanently in the version history with a message describing the change.

git commit -m "Commit message"

git status

This command shows the list of the files that have been changed along with the files that are yet to be added or committed.

git status

git push

This command sends the committed changes of the master branch to your remote repository.


git push origin master

git pull

This command fetches and merges changes on the remote server to your working directory.

git pull

git branch

This command lists all the branches in your repo, and also tells you what branch you’re currently in.

git branch

git checkout

Switches from one branch to another.

git checkout <branch-name>

git merge

Merges the specified branch’s history into the current branch.

git merge <branch>

git remote

This command allows you to connect your local repository to the remote server.

git remote add origin <url>

Conclusion

These are just the basic Git commands that every developer should know. Git has a lot more commands which you can explore by typing git help -a in the terminal.

Remember, practice makes perfect. Keep using these commands, understand what each does, and you'll become proficient with Git in no time. Happy coding!