How to Undo a Pushed Git Commit - Reset & Revert a Git Commit After Push

  Рет қаралды 5,119

Cameron McKenzie

Cameron McKenzie

Күн бұрын

Need to undo a pushed Git commit from GitHub, GitLab, Bitbucket or CodeCommit? Well, there are two ways to revert a pushed commit in git.
You can revert a commit pushed to GitHub or GitLab, but that doesn't delete the pushed commit. It just resets the state of your Git repo.
Alternatively you could do a Git reset to undo a commit pushed to GitHub or GitLab or CodeCommit. That deletes the pushed Git commit for good, but it also messes up the Git commit history, so it's dangerous.
Should you use git reset or revert to undo a pushed Git commit?
The Git revert command is the safest way to undo a Git push. The Git reset command completely deletes the pushed commit, so that one is best if you need everything gone, but it does mess up the commit history.
Take a look at these two approaches on how to undo or revert a pushed commit in git and find out which is best for you!
To undo a pushed Git commit, you can follow these general steps. Keep in mind that this process will alter the commit history, so use caution and make sure it's appropriate for your situation.
1. Locate the commit you want to undo:
- Use `git log` to find the commit hash of the commit you want to undo.
2. Create a new commit that undoes the changes:
- Use the following command, replacing `-commit-hash-` with the hash of the commit you want to undo:
git revert commit-hash
- This will create a new commit that undoes the changes introduced by the specified commit.
3. Push the new commit to GitHub:
- After creating the revert commit, push it to the remote repository on GitHub.
git push origin master
- Replace `master` with the branch name if you are working on a different branch.
4. Verify the changes on GitHub:
- Go to your GitHub repository and check if the new revert commit is pushed.
By creating a revert commit, you keep a record of the fact that a change was undone. This is often a safer approach than using commands like `git reset` or `git push --force`, which can rewrite history and cause problems if others have already pulled the changes.
Using `git reset` and force-pushing to undo a pushed commit is generally discouraged because it rewrites the commit history. When you force-push, you replace the existing history with a new one, and this can lead to several issues:
1. Collaboration Issues:
- If others have already pulled the changes you force-pushed, they will have the old commit history. When they try to push their changes, it can result in conflicts, and they may need to force-push as well. This creates a messy and confusing collaboration environment.
2. Loss of History:
- Rewriting history removes the existing commits from the branch, making it difficult to track changes over time. This can be especially problematic if you're working on a shared branch with multiple contributors.
3. Potential Data Loss:
- If the commit you want to undo contains important changes or information, force-pushing without caution can lead to unintentional data loss.
4. Build and Deployment Issues:
- Continuous integration (CI) systems and deployment pipelines often rely on commit history to trigger builds or deployments. Rewriting history can disrupt these processes.
5. Security Concerns:
- If you force-push changes to a shared branch, it can make it harder to audit and track changes, potentially introducing security concerns.
Instead of force-pushing, the recommended approach is to use `git revert` to create a new commit that undoes the changes introduced by the problematic commit. This way, the history remains intact, and collaborators can easily understand what happened.
In summary, force-pushing should be used cautiously and primarily in cases where you're confident that it won't create issues for collaborators. For public or shared branches, using `git revert` is generally a safer and more collaborative way to undo changes.
If you need to completely remove the commit from the history (which is generally not recommended if the commit has been shared with others), you might consider using `git reset` and force-pushing, but this should be done with caution and only in situations where you're sure it won't cause issues for collaborators.
If you need to reset or undo pushed Git commits on GitHub, GitLab or Bitbucket, heed this advice.

Пікірлер: 27
@jeetendranavgikar6384
@jeetendranavgikar6384 13 күн бұрын
After watching so many videos for the solution. this video got it right. thank you
@cameronmcnz
@cameronmcnz 13 күн бұрын
Why would you watch someone else's video? Happy to help!
@user-ly9qv8oe9o
@user-ly9qv8oe9o 10 күн бұрын
Don't think there will be any other video which will explain it much better than you sir. Thank you
@shawnbunch4872
@shawnbunch4872 Ай бұрын
Clear, concise, and straight to the point. 🐐
@cameronmcnz
@cameronmcnz Ай бұрын
I do try my best to cover what's important. Nobody seems to want to hear my opinions on middle-east politics in these Git videos, it seems.
@samhkwest
@samhkwest Ай бұрын
Thanks for your demonstration. I've just used this tricks to fix a code merges issue for my work.
@cameronmcnz
@cameronmcnz Ай бұрын
Happy to have helped. Nothing worse than thinking you Git pushed something you shouldn't have and you're worried you've broken the build!
@Spyros.Charalambous
@Spyros.Charalambous Ай бұрын
It's the most simplified video ever! thank you so much for your time!!!
@cameronmcnz
@cameronmcnz Ай бұрын
Thanks for the kind words! And given the two different options and the implications of each, I'd say it's anything but simple. If you think it's simple, that's a reflection of the fact that you're smarter than the average bear. 👍
@ABDUL-REHMAN345
@ABDUL-REHMAN345 Ай бұрын
Thankyou so much sir ! i'm watching a lot of video's but i don't understand....finally seeing your video and i will understand♥♥
@cameronmcnz
@cameronmcnz Ай бұрын
Thanks for watching! Honestly, once you get pulls, pushes and commits all under your belt, there's really not much more to being a true Git expert!
@pravinprince3221
@pravinprince3221 2 ай бұрын
Thank you sir it is one of the best video in the Git series, it is so helpful to many of us sir, thanks again sir
@cameronmcnz
@cameronmcnz 2 ай бұрын
First comment! Thanks for the support. Every comment and like really helps out. It boosts the algo and helps get the videos in front of more people.
@vodka7924
@vodka7924 Ай бұрын
You are a life saver sir, Thank you!!!
@cameronmcnz
@cameronmcnz Ай бұрын
Happy to help!
@abujayed8999
@abujayed8999 Ай бұрын
tnx, life saving.
@cameronmcnz
@cameronmcnz Ай бұрын
Meh, being a great dev means knowing how to find answers to your problems. You found it. You saved yourself. All I did was record a video.
@user-il7oz8jr7x
@user-il7oz8jr7x Ай бұрын
thanks. your video really helped me
@mickey008ful
@mickey008ful 11 күн бұрын
Thank you a lot!
@justin9494
@justin9494 Ай бұрын
thank you so much!
@cameronmcnz
@cameronmcnz Ай бұрын
Happy to help! Which approach did you take? Revert the pushed commit or reset it?
@justin9494
@justin9494 Ай бұрын
@@cameronmcnz kind of a bummer because I skipped the intro and didn't know there was a git revert after I've done git reset. I wanted to grab the code that didnt break my app but well, my fault for skipping. Thanks!
@cameronmcnz
@cameronmcnz Ай бұрын
@@justin9494 locally you can still find the orphaned commit created by the reset with the reflog command. Ten reset back to that and then do a revert. A lot of with though if what you’ve got works. Maybe I should make the intros shorter?
@cesarmta87
@cesarmta87 Ай бұрын
Thank you!!!
@pclovers219
@pclovers219 Ай бұрын
Sir when i pass the command git revert (commit id ) of any previous 3 or 4 commit it give me a merge conflict and in it there is a (current code ) but after ======== in the file there is nothing, no incoming change what can i do to go back to my previous state? Hope so you understand my problem 😂
@sagar.sreepuram1960
@sagar.sreepuram1960 2 ай бұрын
How to undo commit if we committ before push and get an error
@cameronmcnz
@cameronmcnz 2 ай бұрын
You can run into so many different scenarios. What's the error? I've probably run into it.
Git rebase basics
15:01
Team Programmer
Рет қаралды 11 М.
Git revert vs reset: What's the difference?
8:15
Cameron McKenzie
Рет қаралды 3,4 М.
Normal vs Smokers !! 😱😱😱
00:12
Tibo InShape
Рет қаралды 112 МЛН
Be kind🤝
00:22
ISSEI / いっせい
Рет қаралды 12 МЛН
Miracle Doctor Saves Blind Girl ❤️
00:59
Alan Chikin Chow
Рет қаралды 22 МЛН
How to Undo Mistakes With Git Using the Command Line
55:21
freeCodeCamp.org
Рет қаралды 81 М.
How to EDIT, UNDO or CHANGE the last Git commit message with AMEND
8:45
How to commit better with Git
11:41
Kie Codes
Рет қаралды 16 М.
So You Think You Know Git - FOSDEM 2024
47:00
GitButler
Рет қаралды 945 М.
13 Advanced (but useful) Git Techniques and Shortcuts
8:07
Fireship
Рет қаралды 883 М.
How to Generate GitHub SSH Keys
19:15
Cameron McKenzie
Рет қаралды 6 М.
Git MERGE vs REBASE: The Definitive Guide
9:39
The Modern Coder
Рет қаралды 56 М.
How To Squash Your Git History Like A Pro
13:09
Colt Steele
Рет қаралды 51 М.
Why Java Uses static & final for Constants
8:44
Cameron McKenzie
Рет қаралды 650
Normal vs Smokers !! 😱😱😱
00:12
Tibo InShape
Рет қаралды 112 МЛН