Loading, please wait...

A to Z Full Forms and Acronyms

Git Commands You Can not Ignore

Jun 24, 2021 Git, Git Commands, 501 Views
In Git, there are more than 100 commands. This article will explain those git commands which are less known but insanely powerful.

Git Commands You Can not Ignore

Git is popular version control. Without any doubt, Git is a powerful tool and easy to set up Git on any machine. Git has saved reasonably more developers' jobs than any other tool. As long as you commit your work with Git, you'll always be able to roll back to a prior version of your code.

Git's command-line interface, on the other hand, is sometimes challenging to grasp.  

1. Effective use of Git Clean Command  

Let's assume you have a bunch of untracked files. And there is no need for you to see all the time, especially when you run the git status command. 

Here are a few options for dealing with this issue:

(a) git clean -n 

If you run the git clean command with the -n option (it's a dry run option), it will not delete anything. Just print all the files that can be deleted. 

(b) git clean -f 

The -f option with the git clean command will forcefully delete all untracked files.

(c) git clean -x 

And if you run the git clean command with -x flab, it will remove both ignored and non-ignored files.

2. Revert Repo (To a Previous Commit)

In many cases, reverting your code makes sense, especially if you've thoroughly messed up a piece of code. 

The most common use case is when you wish to go back in time and look at a previous version of your codebase before returning to the current version. 

You can use the following Git command.

$ git checkout <commit hash>

You can use only the first eight characters of your commit instead of the entire commit hash.

It will disconnect the HEAD and allow you to play about without checking out any branches. Detaching the head isn't as frightening as it may appear.  

When you are ready, you can use the following git command to checkout the branch.

$ git checkout <branch>

3. Undo Your Commit

Anyone can make mistakes. And thanks to Git as it can revert a few mistakes. Suppose you make a wrong commit, and now you want to revert it.

It's easy, and here are a few steps to do it step by step. 

You can use the git reset command with the soft option.

 

$ git reset --soft HEAD~1

When you use the git soft reset, it will only reset the commit history.

Git will shift your HEAD pointer back to the commit you made before this one when you run the first command, allowing you to move files or make modifications as needed.

4. Move a Commit 

When you work in a team, many developers might share a task depending on its difficulty. High cohesiveness and low coupling are sometimes overlooked or considered impracticable. 

It might cause many new issues during the development process.  

Let me share more details with an example. As it might be possible many times, you've worked on such scenarios.

Let's say you are working on a new feature. Now there is a live issue which you have to fix. And it's already set in the feature branch.

How will you deal with this situation?

Either you can write the same code in the bug fix branch or use Git cherry-pick.

Here is the syntax

$ git cherry-pick <commit hash>

It is a Git command to transfer a commit from one branch to another. 

Git Cherry-Pick

To transfer a commit from one branch to another, use the git command — Image from acompiler.com 

This command applies the chosen commit to the current branch, which is helpful for bug hotfixes. Cherry-picking is useful, but if you do over, it might cause many duplicates commits. 

5. Delete Local & Remote Branch

Most of the time, when you create a new local branch, you will push it to a remote repository branch with the same name. When you've completed all of the necessary modifications for that branch, merge it with your production branch (like the main branch).

After merging that branch, it becomes obsolete, and GitHub, for example, allows you to remove the branch thereafter. You could forget to remove the local branch if you do this. If you do this for a lot of other branches, you'll end up with a lot of out-of-date local branches.

When a branch is no longer needed, it is always preferable to tidy up the repository by removing it. Use the git branch with the -d option to delete the branch from the local repository.

$ git branch -d <your branch name>

Using the same git push command with the --delete option, deleting a branch on the remote repository is identical to making an update on the remote repository.

$ git push <remote> --delete <your branch name>

Summary

This Git post is only the beginning.  Git, like everything else, takes time and effort to master. You need practice and practice to learn Git.  

Without Git, a developer's portfolio is incomplete, like going to fight without any weaponry.

Despite how well-known Git is these days, most of its potential is still unknown to the general public. It's true that with only a few commands like commit, push, and pull, you can "survive."

If you're willing to go a little farther, you'll find some of Git's more powerful capabilities. And they have the potential to make you not just more productive but also a better developer in the long run!

If you're interested in learning more about Git, I recommend checking out this Git commands resource.

A to Z Full Forms and Acronyms

Related Article