| by Arround The Web | No comments

Can I Delete a Git Commit but Keep the Changes?

Developers prefer the Git versioning control system for large team projects. All members work on the local repository and then share with each other through GitHub hosting service. They make changes on the local machine and commit them to the repository but sometimes they want to keep the changes rather than commit. In such a situation, the “$ git reset HEAD^” command can be helpful.

This study discusses “can users remove a Git commit but keep the changes” with an example.

Can I Remove a Git Commit but Keep the Changes?

Yes, you can remove a Git commit but keep the added changes. For this purpose, navigate to the Git local repository and create a file in the local repository. Then, track the newly added file into the staging area and update the repository by committing changes. Next, check the repository log history and update the newly created file. Add the changes to the repository, commit changes and delete the previously added commit using the “$ git reset HEAD^” command.

Let’s check out the implementation of the above-listed procedure!

Step 1: Move to Particular Local Repository

Navigate to the desired Git repository by executing the “cd” command:

$ cd "C:\Users\nazma\Git\test_6"

Step 2: Create New File in Local Repository

Run the “touch” command and create a new file in the local repository:

$ touch file1.txt

Step 3: Add File to Staging Area

Next, add the newly created file into the staging area using the following command:

$ git add file1.txt

Step 4: Commit Changes

Now, update the local repository by executing the “git commit” command with the “-m” option and add the desired commit message:

$ git commit -m "1 file added"

Step 5: Check Git Log History

Run the “git log .” command to check the Git reference log history:

$ git log .

Step 6: Update File

Next, open the newly added file with the default text editor:

$ start file1.txt

The specified file will be open in the text editor, add some text, and press the “CTRL + S” keys to save it:

Step 7: Track Updated File

Now, execute the “git add” command with the updated file name and track it to the staging area:

$ git add file1.txt

Step 8: Commit Changes

Commit the added changes to the repository using the below-given command:

$ git commit -m "file1.txt updated"

Step 9: Check Git Reference Log History

Run the “git log .” command to check the Git reference log history:

$ git log .

Step 10: Delete Git Commit

Now, delete the Git commit using the “git reset” command with the “HEAD^” pointer:

$ git reset HEAD^

Step 11: View Git Reference Log History

Again, run the “git log .” command to check the Git reference log history:

$ git log .

As you see in the below-provided output, the most recent commit is deleted from the reference log history:

Step 12: Verify Updated File

Now, run the “start” command with the previously updated file name to verify the keep changes:

$ start file1.txt

According to the below-listed output, the added changes are saved in the file. However, the related commit against these changes is deleted:

We have explained the procedure to delete a Git commit but keep the changes.

Conclusion

Yes, we can remove a Git commit but keep the added changes. To do so, move to the Git particular repository and create a file. Next, add it to the staging area and commit changes. Check the Git reference log history and then update the file. Track the file, commit changes, and delete the previously added commit by executing the “$ git reset HEAD^” command. Lastly, open the updated file and verify the added changes. This study demonstrated the method to delete a Git commit but keep the changes with an example.

Share Button

Source: linuxhint.com

Leave a Reply