Useful git commands for Developers

This article describes some git-related commands and best practices that are very useful in your daily life as a developer. Previously I discussed Spring Boot best practices. I assume you have the basic idea of git and version control systems. This article will cover:

  • Git data transport
  • Git repository naming conventions
  • Add your local project to GitHub
  • Work with the remote origin
  • Clone a project
  • Be up to date?
  • Checkout a branch
  • Delete a branch
  • How to hide your changes?
  • How to undo staged changes?
  • How to modify the commit message?
  • Display metadata
  • How to check whether your branch is merged or not?

Git data transport

Git is known as a distributed version control system that is not centralized like SVN. There are five important things to know when using git. They are stashes, workspaces, indexes, local repositories, and remote repositories.


  • When you are stashing files, adding files, committing, pushing, pulling, fetching…etc, data will flow through the above layers.

Git repository naming conventions

  • As a practice, we adhere to using the very popular kebab-case when naming git repositories. Please check this out.

Add your local project to GitHub

  • Go to the project location through the terminal or cmd. 
  • Type, git init 
  • Now you can check the status using git status command
  • Now you can view untracked files. 
  • Then you can add the required files one by one with the following command:
  • Add files one by one 
                      git add <fileName> 
  • Add all files 
                 git add . 

  • Then you can check file status by git status
  • Now you are ready to commit changes into the local repository. 
  • Type the below command to commit changes. For example 
         git commit -m “commit message”
  • Now you can check the status of your repository.
  • Now let’s push changes to the remote repository.

Create a new repository on GitHub.

Now you can add the remote origin.

  • Now you can push the changes.

This is so simple and straightforward that I won’t explain it further.

Work with the remote origin

Get the remote origin details

         git remote show origin

Remove remote origin

         git remote remove origin

Clone a project

Just clone a repository.

         git clone <URL>

Clone a repository into a specific directory.

         git clone <url> <folderLocation>

Clone a branch into a specific directory.

         git clone <branch> <url> <folderLocation>

Be up to date?

  • All developers may face merge conflicts, and sometimes they become worse. 
  • Most of them are working on a feature branch and at the end of developing the feature, they are going to merge it. 
  • This may lead to many merge conflicts. How can we avoid this?
  • As a best practice, we recommend updating your branch daily at the beginning of the day.

Use git fetch

  • Git fetch will update your local repository, but it doesn’t merge with your working branch.
  • Every day you can fetch updates including new branches.

Use git pull

  • Git pull automatically updates your local repository and merges any pulled commits into your working branch.
  • It will do both git fetch and git merge.

Checkout a branch

  • Before you checkout a branch, you need to pull or fetch to update the local repository, as I mentioned above.

Checkout a branch

         git checkout <branchName>

Create a branch locally and checkout

          git checkout -b <newBranchName>

Delete a branch

Delete a local branch

         git branch -d <branchName>

Delete a remote branch

         git push -d <branchName>

How to hide your changes?

  • Git stash will save changes in your workspace or hide them.
  • Let’s say you’re working on a feature branch and have already made some changes. 
  • But at the same time, some files have changed or a serious error has occurred in the remote repository that needs to be fixed, so the workspace needs to be cleaned up and we need to get a pull.

If you try to git pull, you may get an error.

  • Git stash will be the solution. It uses a stack to save data( the latest change is always on the top).

Add tracked files to stash

  • push is recommended over save
  • You can use git stash save also, but it is deprecated.
         git stash push

  • Add tracked and untracked files to stash and perform git clean
         git stash push -u

  • Add tracked, untracked, and ignored files to stash and perform git clean
         git stash push -a

View all stashed data

         git stash list

View all stashed data with changes

                       git stash list -p

View specific stashed data

Once you list down your stash list, you can find the index of stashed data.

         git stash show -p <stashIndex>

Delete all stashes

         git stash clear

Delete a specific stash

         git stash drop <stashIndex>

Apply a stash into the workspace

         git stash apply <stashIndex>

Apply the topmost stash and delete it

         git stash pop <stashIndex>

Apply stash into a separate branch

         git stash branch <branchName> <stashIndex>

How to undo staged changes?

  • Just imagine that you mistakenly added some files that are not required to commit, or you mistakenly added all files using git add . or else you find a bug in added files to the index (staging area). 
  • If you want to clear the index, there are several ways to do it.

git rm

  • This will delete files from the staging area.
  • No changes will be made in the workspace.
         git rm <fileName> — — cached

git reset 

  • This will update your branch and change the commit history.
  • This will restore the index. So you need to be careful when using this.
         git reset

git restore

  • This won’t update your branch and commit history.
  • It will remove files from the staging area (index).
         git restore — — staged <fileName>

  • If you want to discard changes you made, simply skip the — stage flag
         git restore <fileName>

  • If you want to discard all of the changes
         git restore — — staged .

There are other methods as well. Another important point is that if you use Git, there are many attributes you can use with your Git commands. Check the documentation for more details on them.

How to modify the commit message?

  • Just think, you need to edit the commit message you set when you commit changes into the local repository.
          git commit — amend -m <message>

Display metadata

  • This will be helpful for retrieving metadata and modified data.
          git show

How to check whether your branch is merged or not?

Check merged branches

           git branch — merged

Check not merged branches

           git branch — — no-merge

This is a long, story-like article. But I wanted to share my thoughts in one article rather than in a series of articles. I hope you learn and enjoy it. Feel free to share your thoughts.

Useful git commands for Developers Useful git commands for Developers Reviewed by Ravi Yasas on 2:06 AM Rating: 5

No comments:

Powered by Blogger.