JIYIK CN >

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

Undo commits before pushing to remote repository in Git

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

This article explains how to reset commits in Git that have not yet been pushed to a remote repository. Git is a version control system that we use to track changes made to files in a project directory.

In Git, commits are used to track changes made to files. Sometimes, we might commit changes to a file locally, and then we might feel that those changes are no longer relevant.

We now want to reset this commit instead of pushing it to the remote repository. We can use git resetthe command to complete the task of resetting a commit that has not yet been pushed to the remote.


Undo commits before pushing to remote repository in Git

We use Git in a collaborative development environment to track changes made to files. Git uses commits to accomplish the task of tracking changes to files.

When we are happy with the changes we made to the files, we commit the changes to the project's Git repository. In the basic Git workflow, once we have tested and finished with the changes, we add the changes to the staging area. To do this, we use git addthe command.

After adding the changes to the staging area, we now proceed to commit the changes to our Git repository. We use the command git committo commit the changes.

Each commit is used to record a snapshot of the repository state along with a name, timestamp, and message. When we git commitcommit changes using the command, these changes are committed to the local Git repository.

This local repository is associated with the remote Git repository. We then proceed to push the commits from the local Git repository to the remote repository. We use git pushthe command to achieve this.

Sometimes, before pushing commits to the remote repository, we may feel that the current commits in the local repository are no longer needed and we want to discard them.

Therefore, to discard the commits in the local repository, we can use git resetthe command. Suppose we have the following commit history in our Git repository.

$ git log --oneline
453dcfc (HEAD -> master) minor change
bea3aac (origin/master, origin/HEAD) some change
b14f387 Other change
...

In the commit history, a commit 453dcfcis in the local Git repository but not yet in the remote repository. The previous commit is also in the remote repository.

Therefore, to discard commits that exist in the local repository but have not been pushed to the remote repository, we can execute git resetthe command as shown below.

$ git reset --soft HEAD~1

The above git resetcommand HEADresets the current . The command with --softthe option git resetdoes not touch the staging area and the working tree.

It saves all our changed files as 要提交的更改, just git statusas said, as shown below.

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

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

We can now re-examine the commit history and observe that commit 453dcfcno longer exists.

$ git log --oneline
bea3aac (origin/master, origin/HEAD) some change
b14f387 Other change
...

We have shown how to reset commits in Git that have not yet been pushed to a remote 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