Git Basic Commands

Git Basic Commands

In this cheat sheet, we will learn basic Git commands that we need to start with Git.

What is Git?

Git is a free and open source distributed version control system.

Git is an open-source tool that helps us in code management we can track control of the code from a Team or Individual, it's a great collaboration tool used for Large or Small projects.

Version Control System: It is software that helps keep track of the changes that are made to a code, which can be useful where projects have many developers and includes many features.

Are Git and Github are same?

The simple answer is NO! It's the same as saying car and carpenter are the same or Java and JavaScript are the same, No both are different things. Git is a version control system that lets you manage and keep track of your source code history while GitHub is a cloud-based hosting service that lets you manage Git repositories.

Basic Git Commands:

git init

git init  // create a repository

git init creates a new local Git repository or transforms the current directory into a Git repository.

Alternatively, you can create a repository within a new directory by specifying the project name:

git init [project name] // create a repository within a new directory

After creating a local Git repository now you can add the files that require to be committed by using the next command.

git add

git add: Add file contents to the index

This command will add a change file in a git repo. We can add all files using git add . or you can add single files using git add filename

git add . 
git add filename

git status

Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git.

git status

git commit

Record changes to the repository. Create a new commit containing the current contents of the index and the given log message describing the changes. We usually use git commit -m "Commit Message"

git commit -m "Initial Commit"

git branch

This Command is used for Listing, creating, or deleting the branches.

git branch -M main

Here -M is used for Creating New Branch and Branch Name is name.

git remote add

Manage the set of repositories ("remotes") whose branches you track. We usually use this command on GitHub as below command:

git remote add origin "Repository Link"

git push

This Command Update remote refs along with associated objects. In simple words, Our Committed files are push to the GitHub or similar sites. We can use the below command for pushing files.

git push -u origin main

main is Branch Name that we have given while creating the branch.

Β