How delete commit from git

How delete commit from git

How to Delete Commits from a Branch in Git

This is a useful guide that will help you to delete commits from a branch in Git, accurately. To delete commits in Git, follow the steps below.

Deleting the latest commits

Generally, the git reset command is used for deleting the latest commits in Git.

Deleting the most recent commit

To delete the most recent commit, run the command below:

Deleting multiple latest commits

To delete the N-th latest commits, you should use HEAD

As an alternative to this method, you can select the appropriate commit by its hash instead of HEAD

Deleting a Commit in the Middle of the History

Undoing the Changes of the «Middle» Commit

There exists a safe way of deleting a commit in the middle of the history: using the git revert command. It adds a new commit, which undoes everything the middle commit has changed.

Deleting the «Middle» Commit from the History.

Take into account that git revert doesn’t delete the middle commit. If you want to delete it from the history, then run git rebase in interactive mode:

Then, an editor opens that shows up the commits following the one you have selected. It will offer you to input the command for each commit. All you need to do is typing «drop» at the beginning of each commit you want to delete.

How delete commit from git. Смотреть фото How delete commit from git. Смотреть картинку How delete commit from git. Картинка про How delete commit from git. Фото How delete commit from git

Pushing changes

Here is an alternative and safer way to push combined commits:

Finding the deleted commit

Finally, in case you want to find the deleted commit, you can do it with the help of the git reflog command:

How git reset Works

The git reset command is a powerful tool for undoing changes.

How git reflog Works

This command is generally used for recording updates made to the branches. With the help of the git reflog command, you can return to the commits even to those that have not been referenced by any branch of tag. Reflog contains information about the preceding state of branches and allows returning to that state if needed.

How git rebase Works

CrookedNumber / gist:8964442

Removing the last commit

2 to remove the last two commits. You can increase the number to remove even more commits.

If you want to «uncommit» the commits, but keep the changes around for reworking, remove the «—hard»: git reset HEAD^ which will evict the commits from the branch and from the index, but leave the working tree around.

If you want to save the commits on a new branch name, then run git branch newbranchname before doing the git reset.

amrhgh commented Jun 22, 2020 •

Use these 2 following commands:

RevitArkitek commented Jun 30, 2020

If you go to View History in Visual Studio, you can click the commit you want to go back to, then right-click to Reset (mixed and hard)

dricislam commented Aug 4, 2020

To force to remove the last commit from git,
use these 2 following commands:

rammyblog commented Aug 22, 2020

To force to remove the last commit from git,
use these 2 following commands:

Thanks for saving my life

AanchalCh commented Aug 29, 2020

To force to remove the last commit from git,
use these 2 following commands:

VanDerRomio commented Oct 6, 2020

That worked for me, thank you very much!

MZeeDev commented Nov 23, 2020

Use these 2 following commands:

MUWASEC commented Dec 6, 2020

thx, this work for me

ghost commented Dec 7, 2020

Thanks, this works for me

esumit commented Jan 28, 2021

Whasnat commented Mar 16, 2021

To force to remove the last commit from git,
use these 2 following commands:

Worked perfectly. Thanks

madhuparmar commented Apr 23, 2021

To force to remove the last commit from git,
use these 2 following commands:

erfanpaslar commented Jun 5, 2021

To force to remove the last commit from git,
use these 2 following commands:

That worked.
Thanks.

sjc-bui commented Jun 8, 2021

Use these 2 following commands:

Work for me.
Thanks.

codavidev commented Jun 24, 2021

To force to remove the last commit from git,
use these 2 following commands:

Thankx it wotked for me too

panamdesign commented Jul 14, 2021

Hey guys! I have been through the same problem and here is what happened:

Once I had no more pending commits, the modifications came back on the VS studio tabs. I was able to repeat the commit process with the last modifications.

As long as the pending stuff is not gone, you cannot override it until you reset the «memory» and have it analyse the new file content.

Remove specific commit

I was working with a friend on a project, and he edited a bunch of files that shouldn’t have been edited. Somehow I merged his work into mine, either when I pulled it, or when I tried to just pick the specific files out that I wanted. I’ve been looking and playing for a long time, trying to figure out how to remove the commits that contain the edits to those files, it seems to be a toss up between revert and rebase, and there are no straightforward examples, and the docs assume I know more than I do.

So here is a simplified version of the question:

Given the following scenario, how do I remove commit 2?

The expected result is

Here is an example of how I have been trying to revert

How delete commit from git. Смотреть фото How delete commit from git. Смотреть картинку How delete commit from git. Картинка про How delete commit from git. Фото How delete commit from git

15 Answers 15

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

There are four ways of doing so:

Clean way, reverting but keep in log the revert:

Harsh way, remove altogether only the last commit:

Rebase (show the log of the last 5 commits and delete the lines you don’t want, or reorder, or squash multiple commits in one, or do anything else you want, this is a very versatile tool):

And if a mistake is made:

Quick rebase: remove only a specific commit using its id:

Alternatives: you could also try:

Yet another alternative:

As a last resort, if you need full freedom of history editing (eg, because git don’t allow you to edit what you want to), you can use this very fast open source application: reposurgeon.

How delete commit from git. Смотреть фото How delete commit from git. Смотреть картинку How delete commit from git. Картинка про How delete commit from git. Фото How delete commit from git

2″. Now you have several options. In the vim you can see some commented lines: one of them tells you that you simply can delete a line (which should be the commit you want to get rid of) and this commit will be removed together with it’s log in your history.

5 worked for me. Then I just removed the commit(s) that I didn’t need and I was able to solve the problem in less than 30 seconds. Thank you.

Here is an easy solution:

(Note: x is the number of commits)

upon executing notepad file will open. Enter drop besides your commit.
If you don’t know Vim, just click on each word pick that you want to edit and then hit the «I» key (for insert mode). Once you’re done typing hit the «esc» key to exit insert mode.

How delete commit from git. Смотреть фото How delete commit from git. Смотреть картинку How delete commit from git. Картинка про How delete commit from git. Фото How delete commit from git

and that’s it, you are done. Just sync the git dashboard and the changes will be pushed to remote.

How delete commit from git. Смотреть фото How delete commit from git. Смотреть картинку How delete commit from git. Картинка про How delete commit from git. Фото How delete commit from git

4 includes the last 4 commits.

The algorithm that Git uses when calculating diffs to be reverted requires that

The definition of «adjacent» is based on the default number of lines from a context diff, which is 3. So if ‘myfile’ was constructed like this:

Then it all works as expected.

The second answer was very interesting. There is a feature that has not yet been officially released (though it is available in Git v1.7.2-rc2) called Revert Strategy. You can invoke git like this:

git revert —strategy resolve

and it should do a better job of figuring out what you meant. I do not know what the list of available strategies is, nor do I know the definition of any strategy.

Approach 1

First get the commit hash(ex:1406cd61) that you need to revert. simple fix will be below command,

if you have commit more changes related to 1406cd61 files after 1406cd61 commit, Above simple command will not work. Then you have to do the below steps, which is cherry picking.

Approach 2

Step 1: Find the commit before the commit you want to remove git log

Step 2: Checkout that commit git checkout

Step 4: Now you need to add the commit after the removed commit git cherry-pick

Step 5: Now repeat Step 4 for all other commits you want to keep.

Step 6: Once all commits have been added to your new branch and have been committed. Check that everything is in the correct state and working as intended. Double check everything has been committed: git status

Step 7: Switch to your broken branch git checkout

Step 9: Merge your fixed branch into this branch git merge

You can do the process without creating a new branch by replacing Step 2 & 3 with Step 8 then not carry out Step 7 & 9.

How delete commit from git. Смотреть фото How delete commit from git. Смотреть картинку How delete commit from git. Картинка про How delete commit from git. Фото How delete commit from git

Your choice is between

You should choose (1) if the erroneous change has been picked up by anybody else and (2) if the error is limited to a private un-pushed branch.

Git revert is an automated tool to do (1), it creates a new commit undoing some previous commit. You’ll see the error and removal in the project history but people who pull from your repository won’t run into problems when they update. It’s not working in an automated manner in your example so you need to edit ‘myfile’ (to remove line 2), do git add myfile and git commit to deal with the conflict. You will then end up with four commits in your history, with commit 4 reverting commit 2.

At this point your text editor will open the interactive rebase view. For example

How delete commit from git. Смотреть фото How delete commit from git. Смотреть картинку How delete commit from git. Картинка про How delete commit from git. Фото How delete commit from git

If the rebase wasn’t successful, delete the temporary branch and try another strategy. Otherwise continue with the following instructions.

If you’re pushing your topic branch to a remote, you may need to force push since the commit history has changed. If others are working on the same branch, give them a heads up.

Here is a bash script that you can paste in to create a test repository in the /tmp folder:

At this point, we have a file.txt with these contents:

At this point, HEAD is at the 5th commit, HEAD

4 would be the 1st commit (so HEAD

5 results with «fatal: Needed a single revision; invalid upstream HEAD

5″.) A text editor (see screenshot in @Dennis’ answer) will open with these contents:

So we get all commits since (but not including) our requested HEAD

4. Delete the line pick 448c212 3rd git commit and save the file; you’ll get this response from git rebase :

At this point open myrepo_git/ folder/file.txt in a text editor; you’ll see it has been modified:

Basically, git sees that when HEAD got to 2nd commit, there was content of aaaa + bbbb ; and then it has a patch of added cccc + dddd which it doesn’t know how to append to the existing content.

And if previously, we decided to keep the line cccc (the contents of the 3rd git commit that we removed), we would have had:

Well, this was the kind of reading I hoped I’d have found, to start grokking how git rebase works in terms of deleting commits/revisions; so hope it might help others too.

How can I delete a commit to git [duplicate]

There are a few commits on a remote branch that i would like to get rid of. For example if the history looks like:

I would like to delete C and D to give me:

where B is now HEAD?

2 (to delete the last 2 commits).

2 Answers 2

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

I am going to lay out all the different ways to delete commits for you and when you should use them. Before you do any of these I highly recommend you copy your branch to another branch just to be safe.

If you have already pushed, as in your case, you can skip to #3, but if you haven’t pushed you can look at 1 and 2.

1) I have not pushed yet or am working alone and the commit(s) I want to remove are the most recent commits:

and I want to delete C and D.

which results in: A—B

2) I have not pushed yet or am working alone and the commits(s) I want to remove are in the middle of my branch:

and I want do delete C.

which opens up the extremely useful interactive rebase screen:

As git prompts you «# If you remove a line here THAT COMMIT WILL BE LOST.» that is what we are going to do.

I save it in vi with :wq, which results in:

3) I have already pushed and I am working with other people:

If I have: A—B—C—D and I want to delete C and D.

Note that you may have to resolve conflicts here and then commit. Then do

Note that you may have to resolve conflicts here and then commit.

This results in:

This is safe to push as you are really just adding a commit that reverses all of C’s and D’s changes, essentially deleting it.

REMOVING COMMITS FROM GIT HISTORY

You are a software developer who work with a project that is private. You have already committed an SSH private key, a password file or a config file with sensitive data to your repository. After a while, you decide to turn your project to open source. Because Git tracks all changes to all files as you edit them, your project is being reachable by anyone who steal your sensitive data to gain money. It is a very unpleasant situation for you. Fortunately, There are many ways to overcome this condition. We examine the easiest one of them.

The solution is interactive rebase to remove a commit from git history. To modify or remove a commit that is farther back in history, you must move to more complex tools. Git doesn’t have a modify-history tool, but you can use the rebase tool to rebase a series of commits into the HEAD. With the interactive tool, you can remove a commit that you want.

# First, review the history.

8af1cf5 Fixed bug

965fa8d Added something

fa24s7b Initial commit

Commit 8af1cf5 is the last commit and we want to remove it from history by using rebase.

That command will open your default text editor with your two latest commits:

pick 965fa8d Added something

# Rebase 105fd3d.. 8af1cf5 onto 105fd3d

# p, pick = use commit

# r, reword = use commit, but edit the commit message

# e, edit = use commit, but stop for amending

# d, drop = remove commit

# s, squash = use commit, but meld into previous commit

# f, fixup = like “squash”, but discard this commit’s log message

# x, exec = run command (the rest of the line) using shell

# If you remove a line here THAT COMMIT WILL BE LOST.

# However, if you remove everything, the rebase will be aborted.

The most recent commit is the one at the bottom. By deleting line of the commit or writing drop instead of pick, you can remove the commit from history.

# If you want to review the history

965fa8d Added something

fa24s7b Initial commit

Thus, we deleted a commit locally. To remove it from remote repository, we should push our changes to remote.

The + sign before the name of the branch you are pushing, this tells git to force the push.

We analyzed how to remove a commit from git history in this article. However, this may not an effective solution for your problem in some sitiuations. If you want to learn how to remove a file instead of a commit from git history, you can look at the article that is linked below.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *