Basic Git Commands Every Developer Should Know

Navneet Jasrotia
Bobble Engineering
Published in
6 min readAug 20, 2020

Git plays an essential role in every developer workflow and has become a part in daily development process. It keeps track of all our project changes makes it a useful tool for development.

To use the Git efficiently one should know how the Git works and gets familiar with it.

Git

It is a system that keeps track of all our changes in our code. It is also known as “Version Control” because it keeps a history of all previous versions of our code. In this way we can work in parallel and can share our code with others. There is a local Git repository which is saved locally in our computer and a remote Git Repository which is in server like Gitlab, Github etc.

Branches

We can use git to store and run multiple versions called branches. With the use of branches we can maintain different changes for a project in different branches. In this way we can work in parallel with other persons. By default we commit all our code in main branch known as “master”.

Some common git commands are:

  1. git init

This command is used to setup Git repository in your project. It will create a hidden .git directory that tracks our project changes locally.

2. git config

We have to set username and email if we want to work locally. This information is used when we are doing some commits in a branch to our remote repositories. Remember we have to set user information again for a newly created repository but we can avoid this by using global in command which will set the user information globally.

Globally:

Locally:

3. git add

This command is used to add files in the staging area before a file is available to commit to a repository. The staging area keeps track of all the files before commit. There are multiple ways we can add files or directories into stage.

To add a specific file:

To add all unstaged files

4. git commit

Now we have added our files in our staging area so we are ready to save them locally. We have to commit all those files changes and provide a description for it so that the other person will understand easily what changes we have done in this commit.

5. git status

This command will return the current state of the repository. We can see the files that have been modified, the files present in staging area or being committed and the current branch we are working in.

6. git log

With this command we can see all the commits from the beginning. It shows all the commit history with the name of the author and the date.

7. git branch <branch-name>

With this command we can list, create and delete different branches . To list all branches we use show-branch replacing branch in the command and if we want to delete it use -D option after branch in command to delete.

Add a branch

To delete a branch

8. git checkout <branch-name>

If we want to switch branches we can use this command. If we want to create a branch and switch it then use -b option after checkout.

To switch between branch

To create a new branch and switch it

9. git merge

If we want to merge specific branch into our current branch use this command. It will merge all the changes of that branch into our branch but make sure there are no conflicts present. They will fail all the merging process.

10. git pull

If we are working with other developers and let’s say they have made some changes in a branch and we want the latest changes in our branch so we have to pull the code from the remote Git repository.

11. git push

After we have committed our changes we want to push our code to the remote repository so that it can be available for others and they can take a pull from it. We have to define the origin and the branch we want to push. Origin is the git repository url.

12. git clone

With this command we can copy a remote repository into our local machine. We have to specify the url of the git repository along with this command.

13. git diff

This command will show us the difference between our commits and our current working tree.

14. git stash

With this command git temporarily saves our data safely without commiting. If we want to switch to another branch and doesn’t want to commit our work due to any reason then this command can be useful.

15. git stash (pop & apply )

With these commands user can re-apply previous saved work in the current working file. The main difference betwen pop and apply is that pop command delete the stash from the stack after re-apply

git stash pop:

git stash apply:

16. git reset

This command helps us to undo current changes and goes back to previous changes. With this command we can switch to a different version by resetting it to previous commit. Since each commit comes with a unique id we can reset it by providing the id after this command.

— Soft Flag

If we have used soft flag with this command we can go back to previous commits but our staging area and current working tree will stay the same and if we use commit, it will commit all our changes from the staging area.

— Hard Flag

If we have use hard flag with this command we can go back to previous commit but this time it will delete all our data from the current working tree and staging area.

17. git fetch

With fetch, we can download new data from a remote repository which are not present after you get them. Running this will fetch all the branches and/or tags from the remote repositories along with their objects.

18. git relfog

Reflog are references log which keeps track of all the updated of the branches. We can see all the changes in the branch including the lost commits and we can get them back by using reset command along with the commit id which is visible in reflog.

--

--