My most used git commands

Published on Friday, 8 April 2022

In this article, I share git commands I find myself using most often in my day-to-day job as a Software Developer. I ran a survey among my Dev friends to find out their most used git commands, and the list is pretty similar except for 1 or 2 git command differences.

First I will list out my most used git commands, then a small list of the git commands I use quite a bit but not as regularly. For each of the commands, I will give a brief example of how it can be used. Lastly, I added a list of git commands mentioned by Developer friends as their most used commands.

My Most used git command list with examples

  • git status
  • git add
  • git commit
  • git push
  • git pull
  • git checkout
  • git merge

Git Status

I use git status command to check how many files have been changed and have not been committed.

1git status
2

Here, I have 1 file that has not been added to git and 1 file whose content was recently changed.

git status

Git Add

I use this command to add newly created files or recent file changes to a staging area where they wait to be committed.

There are 2 ways to add all files to the staging area:

a) Git add —all

Since I started working on a monorepo, I use git add --all to add all files I have made changes to no matter the project directory.

b) Git add .

I use git add . to add all files I have updated in the project directory.

If you use vscode, the saved changes will move to staged Changes

vscode

Another way to check if files have been committed is by typing git status in the terminal, the files now highlighted in the green show it has been staged but not committed.

git-add-all

The third part of the git add command is where we specify the files we want to add. So “--all” and “.” means all files. We could also specify which files want to add by doing:

1git add dir/file1.js dir/file2.js
2

git-add

Git Commit

This command saves your recent changes in the project’s version control history locally. This command can be used in a short format.

git commit -m

This option lets you commit your work with a short / one line message

1git commit -m 'update article with pictures'
2

git-commit-m

git commit

This is the same as the command above, but it opens up a vim editor where you can write longer, better-structured commit messages that support multiple lines.

git-commit

If you want to use the git commit command, learn how to use a to write a message, save and quit the vim editor. Or you can change your git editor of choice to Nano.

Git Push

I use the git push command to send locally committed changes to the GitHub remote branch. This ensures that the branch on the remote repository contains all the updates made on the local branch.

1git push
2

git-push

For a newly created local branch, this command is used to set up a remote branch with the same name as the local branch.

1git push -u origin <branch_name> 
2

or the default by git

1git push --set-upstream <remote> <branch_name> 
2

Git Pull

I use the git pull command to keep my git local repositories content up-to-date with what is currently in the remote repository.

1git pull
2

Git checkout

I use this to switch branches.

1git checkout <branch name> 
2

git-checkout

Git checkout -b

This creates a new branch from an existing branch and automatically switches to the new branch.

1git checkout -b <branch name> 
2

git-checkout-b

Git Merge

I use this command for updating commits present in the parent branch(dev or main ) with my working branch. For example, A colleague worked on a feature branch named add-navigation, which has been merged into the main branch.

If I want my feature branch to have the same updates as the main branch, I use git merge to transfer commits present in the main branch to the feature branch that I am working on.

How to merge a feature branch into the main branch

make sure you checkout to the main branch and run the git merge command.

1git checkout main
2git merge <branch name> 
3

git-merge-main

How to merge the main branch into a feature branch

git merge can be used to merge branches in the opposite direction too. When I finish working on a feature branch, I use git merge to add the work done on my feature branch to the main branch.

Make sure the local main is up to date with the remote main branch.

1git pull
2

checkout to the feature branch and run the git merge command.

1git checkout <feature-branch> 
2git merge <branch name> 
3

git-merge-feature

Often used Git commands List with examples

These commands I frequently use but not as much as my most used commands.

  • git log --oneline
  • git branch -d
  • git reset
  • git rebase

git log --oneline

This displays your git commit history as a one-line short message, making scanning through previous commit messages easy.

1git log --oneline
2

git-log-online

git branch -d

I use this command to delete local branches I no longer need.

1git branch -d
2

git-delete

git reset head

I use this command when I want to undo a commit to a certain commit in a file. You can use git --oneline to get the short version of the commit hash you want the code to reset to.

1git reset head <commit hash> 
2

Notable mentions from Developer friends of their most used git commands

These are some of the commands I found from Dev friends that are different from my most often used commands.

  • git rm -cached flag
  • git stash
  • git cherry pick
  • git rebase

git rm --cached flag

git rm --cached command remove files from your local git repository. The --cached flag deletes the file from your git repository, it becomes an untracked file in your project folder. Note you have to commit the changes.

1git rm <file Relative path> --cached
2

git-rm-cached

git stash

Say you are working on a feature branch and you need to work on something else but don’t want to commit the changes just yet. You use a git stash command to save the changes temporarily without having to commit them. This lets you switch between branches and back to your feature branch, easily retrieve the stashed changes and continue working on it.

1git stash --include-untracked or git stash -u
2

git-stash

git cherry pick

You use this command when you want to get a single commit from another branch into your working branch. Think of it like this, instead of using git merge  to add all the commits from another branch into your working branch just to get one particular commit from that branch, you use git cherry pick to merge that particular commit into your branch.

1git cherrypick 0c2e231
2

git rebase

git rebase solves the same problem as git merge but does it differently. Instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.

More Information on the difference between merge and rebase.

Conclusion

It was fun learning about what git commands fellow developers mostly use in their day-to-day jobs. Apparently, we share almost the same list, with a sprinkle of a few command differences.

Do share your most used git command list with me on medium

Recommended Reads

Articles written by Kelechi Ogbonna. All rights reserved

Built with NextJs Typescript and TailwindCSS