Skip to main content
Git

Basic Git Commands - Every Developer Should Know

Lovekesh Kumar
Written by Lovekesh Kumar
Article on 3 min read

Get started with DigitalOcean and host your projects for free. Get $100 credits now!

Git is an open-source version control system that works locally to help developers work together on software projects that matter. Linus Torvalds, the developer of the Linux kernel, created Git in 2005 to help control the Linux kernel's development.

Today we are gonna discuss the common but most important Git commands for any developer.

Some common Q&As

What is a version control system?
A version control system is a system that helps you keep track of changes you've made to files in your project. Using this system we can revert to the previous version of our project if something went wrong or we want to do so.

What are the types of version control system?

  • Distributed Version Control System
  • Centralized Version Control System

Are Git and GitHub are same?
No, Git is a version control system that helps us keep track of changes in the files among our various projects whereas GitHub is a cloud platform on which we can manage our code and project repositories which are powered by Git or we can say that we manage Git repositories on GitHub.

Now we have a basic idea about Git. So we can look up its commands now!

Git requires some configurations when it gets set up on a system. Basic configurations include username and email. Whenever a user commits some changes in a project then it makes a record of changes and saves the reference of the user.

Check your Git configuration

By running this command you can check the user's Git configuration for that particular computer system

git config -l

Configure your Git Username

You can configure your username from the command given below

git config --global user.name "thelovekesh"

Configure your Git User Email

Set up your email using this command which will be used in the commits later:)

git config --global user.email "[email protected]"

Initializing a Git Repo

Starting up Git within a project and getting it connected.

git init
Initializes (or starts) your current working directory (folder) as a Git‌‌repository (repo).
git clone https://www.github.com/username/repo-name
Copies an existing Git repo hosted remotely.
git remote or git remote -v
Shows your current Git directory’s remote repo. Use the -v flag for‌‌more info.
git remote add upstream https://www.github.com/username/repo-name
Adds the Git upstream to a URL.

Staging in a Git Repo

Creating files staged after modifying a file and marking it ready to go in the next commit.

git status
Checks the status of your Git repo, including files added that are not staged.
git add . git or add my_script.js
Stages modified files. If you make changes that you want included in the next commit, you can run add again. Use git add . for all files to be staged, or specify specific files by name.
git reset my_script.js
Removes a file from staging while retaining changes within your working directory.

Committing in a Git Repo

Recording changes made to the repo.

git commit -m "Commit message"
Commits staged files with a meaningful commit message so that you and others can track commits.
git commit -am "Commit message"
Condenses all tracked files by committing them in one step.
git commit --amend -m "An updated commit message"
Modifies your commit message.

Branching in a Git Repo

git branch
Lists all current branches. An asterisk ( ) will appear next to your currently active branch.
git branch new-branch
Creates a new branch. You will remain on your currently active branch until you switch to the new one.
git checkout another-branch
Switches to any existing branch and checks it out into your current working directory.
git checkout -b new-branch
Consolidates the creation and checkout of a new branch.
git branch -d branch-name
Deletes a branch.

Collaborating and Sharing in a Git Repo

Downloading changes from another repository or sharing changes with the larger codebase.

git push origin main
Pushes or sends your local branch commits to the remote repo.
Some repos use master instead of main in their commands
git pull
Fetches and merges any commits from the tracking remote branch.
git merge upstream/main
Merges the fetched commits.

Showing Changes in a Git Repo

See changes between commits, branches, and more.

git diff --staged
Compares modified files that are in the staging area.
git diff a-branch..b-branch
Displays the diff of what is in a-branch but is not in b-branch .
git diff 61ce3e6..e221d9c
Uses commit id to show the diff between two specific commits.

Sources

Something Missing?

If something is missing in this post or if you found some part confusing, then you can:

We love hearing from you!

Give Feedback

Thank you for the feedback! (Join Codebulbs Writers Club)

Sorry to hear that. Please tell us how we can improve. (Suggest an Improvement)

Codebulbs Writers Club

If you are passionate about web, open-source, or javascript and want to share, join Codebulbs writers club!

Join Program

Advertisement