JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Removing unpushed commits in Git

Author:JIYIK Last Updated:2025/04/01 Views:

This article will teach you how to delete unpushed commits in Git.

Git is used in collaborative development environments to track changes made to files in a project directory. Git uses commits to track changes to local and remote Git repositories.

From time to time, one may wish to remove unpushed commits from a local Git repository.

Use git resetthe command to remove unpushed commits. We will illustrate this with an example.


Use git resetthe command to delete unpushed commits in Git

Whenever we want to commit changes to our project directory, we can commit the changes using the git addand git commitcommands.

When you use git committhe command, commits are created in the local Git repository. We can then use git pushthe command to push the commits in the local Git repository to the remote Git repository.

Sometimes, we may realize that we do not want to push the commit to the remote repository, but only commit to the local repository. In this case, we can use git resetthe command to cancel the commit or delete the last commit in the local Git repository.

git resetIs a command used to undo local changes to the state of a Git repository.

Assume we have made changes to our working directory. We can git statusview the status of the changes using the command.

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
        modified:   mynotes.txt

We can see that mynotes.txtthe file has been modified and is eligible for commit.

To commit our modifications, we need to first git addadd the changes to the staging index of our local Git repository using the command. We must run the command as follows git add.

$ git add .
$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   mynotes.txt

So when using git statusthe command we can see that the changes have now been staged.

We can commit the changes to our local Git repository. We have to use git committhe command to create a commit for the staged changes.

We run git committhe command as follows.

$ git commit -m "updated mynotes"
[main e1b08a5] updated mynotes
 1 file changed, 1 insertion(+)

Now, let's run git statusthe command again to check the status.

$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

We can now see that the local branch in our local repository is origin/mainone commit ahead of the remote Git repository branch.

We can use git pushthe command to push the commits to the remote Git repository. But instead of doing that, we use git resetthe command to delete the unpushed commits as shown below.

$ git reset --soft HEAD~1

The command with --softthe -p option git resetremoves unpushed commits from the local Git repository, but keeps local changes. HEAD~1Specifying git reset-p to remove only the last commit removes the last commit.

We will now run git statusto check the status of the repository as shown below.

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   mynotes.txt

So we can see that the unpushed commits are no longer there. However, the modifications are still in progress.

Using --hardthe -p option instead of --softthe -p command option and git resetthe -p command will remove HEAD~1the most recent commit specified by -p and complete your local changes.

We can --hardrun git resetthe command with the -p option.

$ git reset --hard HEAD~1

This will also remove untracked files or directories and tracked modifications. So use it with caution otherwise we may lose all the work we have done.

Therefore, we learned to delete unpushed commits in our local Git repository.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Moving commits to another branch in Git

Publish Date:2025/04/01 Views:200 Category:Git

Git is a very useful and powerful tool in the modern software world. Many types of files and codes can be stored through branches and commits in Git. Branches are a different concept depending on the version control system you use. Many dev

Git push using SSH keys

Publish Date:2025/04/01 Views:94 Category:Git

SSH stands for Secure Shell. It is the key that provides us with the credentials to access the SSH network protocol. It provides access to remote servers between engines on an unsecured open network. It is used for transferring data, files,

Delete commits but keep changes in Git

Publish Date:2025/04/01 Views:179 Category:Git

This article outlines the steps necessary to undo a Git commit while preserving the changes introduced by the same commit. We'll cover two commands we can use that have the same effect. Without further ado, let’s jump right in. Remove com

Different ways to commit untracked files in Git

Publish Date:2025/04/01 Views:198 Category:Git

This article discusses the different methods we can use to commit untracked files in Git. If you introduce new files in your project in Git, these files will fall under the category of untracked files. With respect to the Git version contro

Git add all but one file to commit

Publish Date:2025/04/01 Views:73 Category:Git

This article explains how to add all files to commit while excluding selected files. This comes in handy when you have many files to include in a commit and must leave out one file. Instead of adding files one at a time, you can follow thes

Git exits the commit message editor

Publish Date:2025/04/01 Views:91 Category:Git

This article outlines the steps to exit the commit message editor in Git. When you merge or make a commit in Git, the console prompts you to provide a commit message that briefly describes the new commit. Git opens your default text editor,

git add, git commit and git push are combined into one command

Publish Date:2025/04/01 Views:142 Category:Git

This article discussed two methods that you can use to add, commit, and push files to a remote repository with a single command. When making small changes to a single file, you still need to follow the three-stage process of publishing chan

Git Add and Git Commit merged into one command

Publish Date:2025/04/01 Views:184 Category:Git

This article discusses combining the git add and git commit commands into one command line. Combining the two commands into one can save you time. When combining these two commands, you have to keep in mind what you are committing to. Let's

Git list commits

Publish Date:2025/04/01 Views:66 Category:Git

Git is the most common, free and open source distributed version control system. It has repositories that play an important role in the Git world. As a result of this feature of Git, repositories hold a huge significance in the life of deve

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial