Friday, April 19, 2024
ExplainerTech/Web

Essential git commands every developer should know

In this post, I’m focusing on Essential git commands. Git is an important part of daily programming (especially if you’re working with a team) and is widely used in the software industry.

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

Remember: To understand this article, you need to know the basic knowledge of Git.


Essential git commands


Git clone

To copy a git repository from remote source, also sets the remote to original source so that you can pull again.

git clone <https://name-of-the-repository-link>

Generally, if we want to download a project from Github, all we need to do is click on the green button (clone or download), copy the URL in the box and paste it after the git clone command that I’ve shown right above. This will make a copy of the project to your local workspace so you can start working with it.

Git init

To initialise a git repository for a new or existing project.

git init in the root of your project directory.

Git branch

Branches are highly important in the git world. By using branches, several developers are able to work in parallel on the same project simultaneously. We can use the git branch command for creating, listing and deleting branches.

Creating a new branch:

git branch <branch-name

This command will create a branch locally. To push the new branch into the remote repository, you need to use the following command:

git push -u <remote> <branch-name>

Viewing branches:

git branch or git branch --list

Deleting a branch:

git branch -d <branch-name>

Git status

To check the status of files you’ve changed in your working directory, i.e, what all has changed since your last commit.

git status

Git add

adds changes to stage/index in your working directory.

To add a single file:

git add <file>

To add everything at once:

git add -A

Note: The git add command doesn’t change the repository and the changes are not saved until we use git commit.

Git checkout

Switch to different branches

git checkout <name-of-your-branch>

There are some steps you need to follow for successfully switching between branches:

  • The changes in your current branch must be committed or stashed before you switch
  • The branch you want to check out should exist in your local

There is also a shortcut command that allows you to create and switch to a branch at the same time:

git checkout -b <name-of-your-branch>

This command creates a new branch in your local (-b stands for branch) and checks the branch out to new right after it has been created.

Git commit

commits your changes and sets it to new commit object for your remote.

git commit -m "commit message"

Git push/Git pull

Push or Pull your changes to remote. If you have added and committed your changes and you want to push them. Or if your remote has updated and you want those latest changes.

git push <remote> <branch-name>

However, if your branch is newly created, then you also need to upload the branch with the following command:

git push --set-upstream <remote> <name-of-your-branch>

or

git push -u origin <branch_name>

Git pull

git pull <remote>

Git merge

Merge two branches you were working on.

When you’ve completed development in your branch and everything works fine, the final step is merging the branch with the parent branch (dev or master).

For example, when you want to merge your feature branch into the dev branch:

First you should switch to the dev branch:

git checkout dev

Before merging, you should update your local dev branch:

git fetch

Finally, you can merge your feature branch into dev:

git merge <branch-name>

Note: Make sure your dev branch has the latest version before you merge your branches, otherwise you may face conflicts or other unwanted problems.

Git reset

You know when you commit changes that are not complete, this sets your index to the latest commit that you want to work on with.

 git reset <:mode:> <:COMMIT:>

Git remote

To check what remote/source you have or add a new remote.

git remote to check and list. And git remote add <:remote_url:>

Git stash

Save changes that you don’t want to commit immediately.

 git stash in your working directory. 
 git stash apply if you want to bring your saved changes back.


Thanks for reading. If you like this post “Essential git commands” probably you might like my next ones, so please support me by subscribing my blog.

You may like also: Rock Pi Boards | Rock Pi 4 | Rock Pi X | Rock Pi N10

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *