DEV Community

Cover image for Git: basic and the most useful commands
Anshu Pathak
Anshu Pathak

Posted on

Git: basic and the most useful commands

Git for beginners

Git is necessary for many companies and a very useful skill to have as a technical person.
Almost it is the very basic need of a developer or any person who is doing cording.

you may realize that knowing git is nearly as important as knowing an actual programming language.
But still many people forget to check out how git works and what commands are responsible for what action.

This is why today, I decided to create a simple blog about the basic git commands which you can take a look at and use whenever you’ve just forgotten what you have to put inside the command line or need to refresh your memory.

In this article, we will decide about the git and how to use it or the most useful command as a developer which you most know.
Alt Text

Let’s start!

A few basic terms which help to understand git commands
repository: keeps all your project's files, including commits and branches.

branch: is a copy of the repository holding the specific version. The main branch in git is master.

commit: may be imagined as a single save of changes to the specific branch.

checkout: is the operation of switching between the current branch and the one specified in the command.

master: is the main branch of the repository.

merge: is an action that adds changes from one branch to another.

fork: is a copy of the repository.

head: is the most recent commit of the repository you work with.

Basic git commands everyone should know
git init | git init [folder]
git init is used to initialize an empty repository from the folder you are currently using this command or using folder path, both ways are correct. It’s used while starting a new project or if you want to initialize the git repo inside the existing project.

git clone [repo URL] [folder]
git clone is used to copy the existing repository to the specified folder on your computer. Git clone can be used only with repo URL as a parameter, then it will copy the repository to the folder from where you used the command. If you want to copy the repository to a different location on your computer, add a folder path as a second parameter.

git add [directory | file]
git add stage all changes in the directory or in the file, and it depends on what you add as a parameter. In most cases, it's followed by git commit and git push commands.

git commit -m "[message]"
git commit -m "initial commit" this command is used to commit all staged changes with the custom message passed as a string. By changing -m parameter to -am it's possible to add and commit changes at once.

git push
git push origin branch_name is the command, which pushes changes to the origin branch.

git status
git status is used to check the status of the modified files and it shows which files are staged, unstaged, and untracked.

git log
git log is used to display the history of the commit in the default format.

git diff
git diff shows all unstaged differences between the index and the current directory. And another option is to use the command with the file name to display differences between the file and in the last commit.

git pull
git pull is used to get changes from the original branch, and it merges the changes into the local branch.

git fetch
git fetch --all This command retrieves the most recent changes from the origin branch but doesn't merge.

Thanks for reading !!

Happy cording

Top comments (0)