Necessary Git Commands to Improve a Developer Productivity

Sohaib Anser
FAUN — Developer Community 🐾
5 min readJan 8, 2022

--

git commands

Git is the most widely used version control system. It keeps track of your changes and updates. Multiple people can collaborate efficiently and productively. To be productive in your team you should have a strong grip on this tool. Next, we will look at the most important commands of git that you require in your day-to-day task.

Git Clone

It will create a clone of the existing repository. It will create a copy of the whole repository, a tag, or a branch depending on your requirements. It supports different URL protocols like SSH, HTTPS, etc.

git clone <repository>

To create a clone from a tag

git clone --branch <tag> <repo>

The shallow clone will clone the repo with the defined number of commits(depth).

git clone -depth=1 <repo>

Git Status

To get the state of the repository and indexing(stagging) area, use git status. It will display the current branch state, changes, track, and untracked files.

git status

Add & Remove files:

To display all the tracked files.

git ls-files

To add the untracked files and update changes not staged for commit, use the git add command.

git add <file>

--cachedwill remove the file from the index area and move that file to the untracked area.

git rm --cached <file>

Git Commit

Git commit is used to capture the current snapshot of the staged project. Git will never alter it until you specifically change it. To pass the message with commit use the -m flag.

git commit -m "commit message"

--amend is used to do changes in the most recent commit. You can change the commit message by using the -m flag with amend like

git commit --amend -m "This is the correct message."

To add a new file in the last commit

# Edit hello.py and main.py
git add hello.py
git commit
# Realize you forgot to add the changes from main.py
git add main.py
git commit --amend --no-edit

the --no-edit will amend the changes without changing the commit message.

Git Pull & Fetch

The git fetch will retrieve the latest metadata from the remote repository to the local one.

git fetch

On the other side, git pull will copy the changes from the remote repository to the local and retrieve the metadata as well.

git pull

Git Log

To show all the commits made to the repository, use the git log. It will return the commit hash, message associated with commit, timestamp, and author of commit, etc.

git log

To get the specified number of commits pass the number and to get the state of commit pass the--statflag.

git log -number_flag --stat
#Example
git log -1 --stat

Git Reset & Restore

The below command will revert the last commit and return the committed files to the index state.

git reset --soft HEAD~1 

To unstaged a file from stagging (index) state use,

git reset HEAD file_from_stagging_area

git restore --staged will be used to un-stage a file from the changes to be committed area

git restore --staged <file>

To discard changes in the working directory use

git restore <file>

Git Cherry-Pick

To bring the changes from a specific commit to another branch use cherry-pick.

git cherry-pick <commit-hash>

Git cherry-pick will pick the specific git commit from any branch and add it to the current working directory. It is useful for hot bug fixes.

git cherry-pick commit_hash

To edit the commit message, use the -edit flag and to add the content without making a new commit pass the --no-commit flag.

Git Stash

git stash temporarily stores the changes to your working directory so you can work on some other branch and come back later on to reapply those changes.

--list will return the list of the stash changes.

git stash --list

To restore the stash changes, use the index of the stash you want to apply.

# Restore the stash at index 0
git stash apply 0

To show the changes in stash

git stash show 0# to show the changed content
git stash show -p 0

To create a new branch from stash

git stah branch new-branch 0

To remove all the stash

git stash clear

To get the latest stash

git stash pop

To partially stash the changes use the -p (or --patch) flag and it will iterate through all the changes and ask you if you want to add those changes in the current stash.

Git Squash

The git squash will take a series of commits and combine it into fewer commits. -i is used for interactive mode.

git rebase -i HEAD~3

It will take the last 3 number of commits and will open up the file like below. Then use the s flag instead of the pick to squash the commits. There are other defined options are available in the file as well. When you close it then a new file will be opened for a commit message.

To use the fixup option with squash, replace the pick with f flag for the intended fixup commits. Remember in this case, the main commit message will be marked as the new commit message.

Git Reflog

The git reflog is used to record all the history. keeps track of commits that are made as well as the commits that are discarded. It provides the rolling buffer for the specified time and you can recover anything like deletion of branches, rebase, and reset, etc.

git reflog

Git Tags

The git tags are the specific points in the git history. It is usually used for release purposes. To create a tag from any branch, first, check out that branch and use the tagging command.

# lightweight tag
git tag <tag_name>
# annotated tag
git tag -a <tag_name> -m "message"

A lightweight tag is just a bookmark and stores the name of the tag and on the other hand, annotated tag stores the detail metadata like tagger name, email, tag message, etc.

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--