How to remove commit from git
How to remove commit from git
Removing Git Commits From Master
(Safely)
Have you ever been in a situation where a breaking change escaped to the master branch? Maybe now it’s causing some issues on your website and you have to get it down now before more potential customers experience the error. Especially because the fix is going to take more than five minutes. How in the world do you that? Thankfully, git makes it really easy to roll back commits safely without wrecking anything (too much). Let’s walk through it, step by step.
Removing a Single Commit
For this article, I’ve created a repo to test against which you can find here. Not much there besides empty files, but you can fork it if you’d like to play with reverting commits on your own.
Looking at my commit history on github, you can see 3 commits, one where I add files a.txt and b.txt, another two others adding files c.txt and d.txt.
Let’s say that file c.txt needs to be removed before it can be pushed out to the production servers. The git command you need is called revert (full documentation). It’s usage is pretty straightforward:
To revert the commit with c.txt, I’ll copy the full sha on github.
Next, I run the following command in terminal:
Doing this will create a new commit undoing the changes in the commit you are reverting. In this example, a commit deleting file c.txt is being added to the repo. Running this command opens a vim window in terminal which gives you a chance to edit the commit message.
I like to leave the commit message alone. I want to know that commit is specifically undoing changes, not adding new code. This will come in handy later.
In the terminal window, type :wq (which stands for write & quit) to apply the change and go back to your normal terminal interface.
Now, when you push to master, you’ll see a new commit removing file c.txt from the repo. The file is being “removed” because the commit we reverted added the file in the first place. File d.txt is still in the repo and the code can now safely go to production.
Revert The Revert
Now that you’ve redeployed the site without file c.txt and have some breathing room, you can pull your changes back in and fix the bug. You don’t need to redo any work at all in this case. By repeating the same process as above, we can add c.txt back to the master branch and get to bug stomping!
Grab the commit sha as before, this time using the sha from the revert commit, and run the revert command with it.
This time, you’ll get a commit message that looks like the following:
I know the commit message looks kind of stupid, but I like to know what was going on when I go through the master branch history. This paints a clear picture of what happened.
Looking at the commit history on github again, you can see the new commit re-adding the file.
Reverting Multiple Commits
This is where things can get a little tricky. I follow a specific set of rules to help reduce any errors and conflicts.
Consider the following example. New work has created commits that add files e.txt, f.txt, g.txt, and z.txt. This time, we need to remove files e, f, and g but leave z intact.
This time, you don’t get a vim window asking you to verify the commit message. You’re back at the terminal window.
In the real world, you may have a ticketing system requiring you to add the ticket number to each commit. This makes it easy to reference which commits were reverted in your message. For example, your message could be: “Revert POD-456, POD-672, and POD-887”.
Check Your Work!
After doing any kind of reverting or other file manipulation, check your work! Make sure everything runs as you expect it and all your tests pass. The last thing you want to do is check in a broken revert.
Important Rule of Thumb
Force pushing to master can have dire consequences. When you force push to master, you are telling git “I don’t care what is in the remote master branch, what I have is correct.” While this sounds good in theory, you can erase other team member’s work by doing this. If you haven’t pulled down the latest version from master and someone else has made a few commits, those commits are gone when you force push. They may need to redo their work or hopefully, their code is still stored in a branch waiting to be resurrected.
If you do need to force push to master, make sure everyone on the team knows what you have to do and why. Pull the latest from master and be sure you’ve got everyone’s commits before force pushing. This is one situation in which over-communication is a good thing.
The main caveat to this advice is in branches. Typically, if you’re working in a feature branch, you are the only one working on that branch. Force pushing to a branch is usually acceptable. You’ll want to default to your team’s guidelines and practices though. Play fair!
Reverting and undoing changes can be a scary business. If you ever have doubts, create a branch and try out your changes there before moving forward. Once you’ve done it a few times, it’s not as scary as it seems. Hopefully you don’t have to do this a lot though!
*Update*
TJ Draper on Twitter brings up a very good point, which I completely agree with:
Unfortunately, the current stack I work with uses the master branch for testing and production so potentially broken code does end up in our master branches. One of the most popular workflows for git is called Git Flow which helps mitigate this problem almost entirely. It’s a little more complex but it more often than not ensures what’s in your master branch is 100% working code.
How to delete commit from the GIT?
How to just remove commits from the GIT leave local code untouched? For example I want to delte lol
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
Where commit pointer is the commit hash or HEAD
An editor (probably nano) will show up with a temporary file created by Git ( git-rebase-todo ). Follow the instructions (you are going to change ‘pick f59c3d6 lol’ to ‘fixup f59c3d6 lol’) and save the file (Ctrl-X, y, Enter).
If you want to apply the changes from lol to the newer commit you will have to mark the view added commit with squash and after saving the git-rebase-todo file another one will pop out with commit messages of squashed commits. Edit it and save.
The complexity of changing a commit N commits ago is something like O(N) so better don’t change something from the very beginning of the project or at least go have a coffee then.
Sounds like you’ll want to do an interactive rebase. When you run this command, the editor will open, and you’ll be able to see a list of commits, with their SHAs, the commit message, and the action to take on each commit. By default, they will all be «picked». We have 7 options for what to do with a commit: pick, squash, edit, reword, fixup, exec and delete. To delete you simply just need to remove the line, BUT you will also lose the code changes with it. Instead, we want to squash the commit, merging it into the commit that came before. Then, we can reword the commit message if necessary, and move on. It’ll look something like this
Your file will look something like this:
We need to change the second line to «remove» the lol commit:
A couple of notes
Completely remove commit from git database [duplicate]
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
I need a commit to no longer be in the git database of commits. I need to be able to remove commit abc123. such that git checkout abc123. returns error: pathspec ‘abc123. ‘ did not match any file(s) known to git.
The QA Delete commits from a branch in Git answers this partially, as in how to remove references to a commit from the HEAD, but it doesn’t cover finding all of the branches a commit is present in nor does it cover expiring and purging the commit once it has been made a dangling commit.
How would I achieve this?
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
List all branches that contain the commit:
Remove commit from these branches:
If a changed branch is tracked in any remote, push it there with override:
(Note that, depending on your development process, the commit may appear in untracked remote branches as well.)
Purge the commit, so it can not be restored from your local repo:
Be warned that above commands will remove all dangling objects from Git repo and all the history of branch changes — so you wouldn’t be able to restore (by non-forensic means) anything lost prior to its run.
Tell all collaborators to fetch changed branches and update any work they might have based on them. They should do as follows:
For each branch that is based on a branch you changed above (including the branch itself if they have it locally):
How to remove specific commits from Git? [duplicate]
I want to remove few commits from my remote repository. I have few commits like this in my repo:
I want to remove commit 98y65r4 and 987xcrt from my repo. How to achieve it?
1 Answer 1
There are two alternatives to this: the safe one that leaves you with a dirty git history, or the unsafe one that leaves you with a clean git history. You pick:
Option 1: Revert
You can tell git to «Revert a commit». This means it will introduce a change that reverts each change you made in a commit. You would need to execute it twice (once per commit):
Then you can push the new 2 commits to your remote repo:
This solution is safe because it does not make destructive operations on your remote repo.
Option 2: Interactive rebase
You also can use an interactive rebase for that. The command is:
In it, you are telling git to let you select which commits you want to mix together, reorder or remove.
When you execute the command an editor will open with a text similar to this:
Just go on and delete the lines you don’t want, like this:
Save the file and close the editor.
Now you need to push your changes to your repo, which is done with a «push force», but before you execute the command bear in mind that push force is a destructive operation, it will overwrite the history of your remote repository and can cause merge troubles to other people working on it. If you are ok and want to do the push force, the command is:
How to permanently remove few commits from remote branch
I know that’s rewriting of history which is bad yada yada.
But how to permanently remove few commits from remote branch?
10 Answers 10
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
This SO answer illustrates the danger of such a command, especially if people depends on the remote history for their own local repos.
You need to be prepared to point out people to the RECOVERING FROM UPSTREAM REBASE section of the git rebase man page.
n
(replace n by the number of commits to remove)
So we must not reset to the commit_id that we don’t want.
Then sure, we must push to remote branch:
There are three options shown in this tutorial. In case the link breaks I’ll leave the main steps here.
1 Revert the full commit
2 Delete the last commit
or, if the branch is available locally
where +dd61. is your commit hash and git interprets x^ as the parent of x, and + as a forced non-fastforwared push.
3 Delete the commit from a list
This will open and editor showing a list of all commits. Delete the one you want to get rid off. Finish the rebase and push force to repo.
If you want to delete for example the last 3 commits, run the following command to remove the changes from the file system (working tree) and commit history (index) on your local branch:
Then run the following command (on your local machine) to force the remote branch to rewrite its history:
Congratulations! All DONE!
Some notes:
You can retrieve the desired commit id by running
Then you can replace HEAD
N with like this:
Источники информации:
- http://stackoverflow.com/questions/27043508/how-to-delete-commit-from-the-git
- http://stackoverflow.com/questions/11271263/completely-remove-commit-from-git-database
- http://stackoverflow.com/questions/46724078/how-to-remove-specific-commits-from-git
- http://stackoverflow.com/questions/3293531/how-to-permanently-remove-few-commits-from-remote-branch