DEV Community

Cover image for Developer's Cheat Sheet: 10 Game-Changing Git Commands 🚀
Pratham Tyagi
Pratham Tyagi

Posted on

Developer's Cheat Sheet: 10 Game-Changing Git Commands 🚀

Understanding Git and GitHub is crucial for any developer, providing effective version control and code management. Proficiency in these tools sets you apart and enhances your productivity. In this blog post, we will explore a set of Git commands to kickstart your journey into software development.

Git Vocabulary

Before delving into the commands, let's acquaint ourselves with some Git terminologies. This understanding will not only help you grasp Git better but also provide a foundation for your overall comprehension.

A repository, or repo, serves as a storage space where your project's source code and its version history are stored.

Working Directory
The working directory is where you currently make changes to your project, housing the files you are actively working on.

Staging acts as an intermediate area between the repository and your working directory. It's where you add changes before committing them to the main repository.

A commit is a snapshot of proposed changes to the stage, identified by a unique identifier (SHA-1 hash) and accompanied by a commit message.

A branch represents a parallel version of your repository, facilitating work on different features or bug fixes independently.

Merging involves combining proposed changes into the main repository, often utilized to integrate new features into the main project.

Pulling refers to fetching code from any remote repository and merging it into your local repository, i.e., working directory.

Pushing involves sending your local changes to any remote branch of any repository.

Cloning is the process of creating a local copy of the main repository and establishing a connection for efficient pull and push.

Fetching downloads changes from any remote repository to the local repository, without directly merging them into your local repository. It is useful for reviewing changes before merging.

Forking creates a personal copy of someone else's repository on your GitHub account, enabling changes without affecting the original repository.

A conflict arises when two or more branches have changes in the same part of the code, and Git cannot directly merge them.

In Git, HEAD is a pointer/reference always pointing to your latest commit in the current branch. When you make a new commit, HEAD moves to the top of your commit.

Now, let's explore the 10 Git commands one by one.

This GIF marks the beginning of the breakdown of all the pointers mentioned above, tags- git, github, commands, beginner, coding, blockchain, react, javascript, funny, memes. coding meme

1 - Adding and Committing Files Together

Traditionally, in Git, we use the git add * command to stage all modified files for a subsequent commit. Subsequently, we use the git commit -m "commitMessage" command to commit these changes. However, a more streamlined command exists, achieving both tasks in a single step:

git commit -am "commitMessage"

Enter fullscreen mode Exit fullscreen mode

The -am flag allows us to stage these changes and commit them in one efficient operation`

2 - Creating and Switching to a Git Branch

Similar to the previous scenario, another command combines the functionality of two commands. Instead of using separate commands, use git branch branchName to create a branch and git checkout branchName to switch to it. Achieve both tasks in a single step with the following command:


git checkout -b branchName


The -b flag with the git checkout command allows us to create a new branch and immediately switch to it.

3 - Delete a Git Branch

To delete a branch in Git, use the git branch -d or git branch -D command. The -d option is for a safe deletion, only deleting the branch if fully merged into the current branch. The -D option is for forceful deletion, regardless of whether it's fully merged. Here are the commands:

Safe deletion (checks for merge):


git branch -d branchName

Forceful deletion (doesn’t check for merge):


git branch -D branchName


4 - Renaming a Git Branch

To rename a branch, use the git branch -m command followed by the current branch name and the new desired branch name. For example, to rename a branch called oldBranch to newBranch, run:


git branch -m oldBranch newBranch


If you want to rename the current branch where you are working, without specifying the old name, use the following command:


git branch -m newBranchName


Here, you don’t need to specify the old branch name because Git will assume you want to rename the current branch to the new name.

5 - Unstaging a Specific File

Occasionally, you may want to remove a particular file from the staging area, allowing additional modifications before committing. Use:


git reset filename

This will un-stage that file while keeping your changes intact.

6 - Discarding Changes to a Specific File

To completely discard changes made to a specific file and revert it to its last committed state, use:


git checkout -- filename

This command ensures the file returns to its previous state, undoing recent modifications. It’s a helpful way to start fresh on a particular file without affecting the rest of your changes.

7 - Updating Your Last Git Commit

Imagine you’ve just made a commit in your Git repository, but then you realize that you forgot to include a change in that commit, or perhaps you want to fix the commit message itself. You don’t want to create a whole new commit for this small change. Instead, you want to add it to the previous commit. This is where you can use the command:


git commit --amend -m 'message'

This command modifies the most recent commit you made, combining any staged changes with your new comment to create an updated commit.

A thing to remember is that if you have already pushed the commit to a remote repository, you will need to force push the changes using git push --force to update the remote branch. A standard git push operation appends a new commit to your remote repository rather than modifying the last commit.

8 - Stashing Changes

Imagine you’re working on two different branches, A and B. While making changes in branch A, your team asks you to fix a bug in branch B. When you attempt to switch to branch B using git checkout B, Git prevents it, displaying an error:

This is an Git error, displayed after we mess up, oops :(

We can commit our changes as suggested by the error message. But committing is

more like a fixed point in time, not an ongoing work in progress. This is where we can apply the error message’s second suggestion and use the stash feature. Use this command for stashing your changes:


git stash

git stash temporarily saves changes that you're not ready to commit, allowing you to switch branches or work on other tasks without committing incomplete work.

To reapply stashed changes in your branch, use git stash apply or git stash pop. Both commands restore the latest stashed changes. Stash applying simply restores the changes, while popping restores the changes and removes them from the stash. You can read more about stashing over here.

9 - Reverting Git Commits

Imagine you’re working on a Git project, and you discover that a particular commit introduced some undesirable changes. You need to reverse those changes without erasing the commit from history. Use the following command to undo that particular commit:


git revert commitHash

It’s a safe and non-destructive way to correct errors or unwanted alterations in your project.

For instance, let’s say you have a series of commits:

  • Commit A
  • Commit B (undesirable changes introduced here)
  • Commit C
  • Commit D To reverse the effects of Commit B, run:


git revert commitHashOfB


Git will create a new commit, let’s call it Commit E, which negates the changes introduced by Commit B. Commit E becomes the latest commit in your branch, and the project now reflects the state it would have been in if Commit B had never happened.

If you’re wondering how to retrieve a commit hash, it’s straightforward using git reflog. In the screenshot below, the highlighted portions represent the commit hashes that you can easily copy:

Commit Hashes

10 - Resetting Git Commits

Let’s assume you’ve made a commit to your project. However, upon inspection, you realize that you need to adjust or completely undo your last commit. For such a scenario, Git provides these powerful commands:

Soft reset


git reset --soft HEAD^


When you use git reset --soft HEAD^, you are performing a soft reset. This command allows you to backtrack on your last commit while preserving all your changes in the staging area. In simple words, you can easily uncommit while retaining your code changes, using this command. It is handy when you need to revise the last commit, perhaps to add more changes before committing again.

Mixed reset


git reset --mixed HEAD^


This is the default behavior when you use git reset HEAD^ without specifying --soft or --hard. It un-commits the last commit and removes its changes from the staging area. However, it keeps these changes in your working directory. It is helpful when you want to un-commit the last commit and make changes from scratch while keeping the changes in your working directory before re-committing.

Hard reset


git reset --hard HEAD^


Now, let’s talk about git reset --hard HEAD^. It completely erases the last commit along with all the associated changes from your Git history. When you use --hard flag, there's no going back. So use this with extreme caution when you want to discard the last commit and all its changes permanently.

Thank you so much for reading. This is my first blog on I hope this post is helpful, and that you learned some new commands. If you have any further questions, don’t hesitate to reach out. Feel free to share any Git commands you tend to use in your daily routine and find super handy. :)

Connect with me:-


Top comments (0)