Change a Git Commit in the Past with Amend and Rebase Interactive
We'll change the last commit's message and also do an interactive rebase on an older commit.
Hit the subscribe button to receive more videos like this!
REFERENCE LINKS
---------------------------------------------------
► github.com/nickjj/build-a-saa...
► github.com/nickjj/dotfiles
COURSES
---------------------------------------------------
Courses I've created that focus on web dev and deployment topics.
► nickjanetakis.com/courses/
THE TOOLS I USE / GEAR
---------------------------------------------------
► nickjanetakis.com/blog/the-to...
FOLLOW ME ELSEWHERE
---------------------------------------------------
► Twitter: / nickjanetakis
► GitHub: github.com/nickjj
TIMESTAMPS
---------------------------------------------------
0:00 -- What we're going to cover in this video
0:20 -- Use cases for wanting to change a commit
1:03 -- The project we'll be amending commits in
2:04 -- Amending the last commit message
3:15 -- Amending a file in the last commit without editing the message
5:44 -- Adding or removing multiple files and amending the last commit
6:20 -- Preparing to do an interactive rebase
7:25 -- Figuring out which commit to amend using HEAD~N or the direct SHA
9:15 -- Starting an interactive rebase with git rebase --interactive [SHA]^
10:18 -- Making our changes as part of the interactive rebase
11:39 -- Adding our changes and amending the commit during the rebase
12:33 -- Pulling the trigger and finalizing the rebase
13:21 -- Aborting a rebase in case you decide not to apply it
14:44 -- Using git stash to rebase a commit without mixing in our un-staged changes
17:35 -- Recap and do you have any questions?
Пікірлер: 78
I’m really liking the content and the pace of the presentation. No fluff just straight to the point.
The content and presentation is superb, I saved a lot of time during my code patch stacks. I recommend this to all my colleagues to reduce their patch stacks time .
@NickJanetakis
4 ай бұрын
Thanks a lot for watching and recommending the video.
Thanks to you I've finally learned rebase.
Perfect and clear explanation! Thanks a lot!
Nice presentation. To the point, quick and only meaningful content. Well done Nick.
@NickJanetakis
Жыл бұрын
Thanks Deepak!
Awesome way to explain such handy and useful things. Thanks, man.
@NickJanetakis
3 жыл бұрын
No problem!
You are a legend sir.
this is the best video for rebase on the internet lol, I hope you go viral somehow :)
@NickJanetakis
5 ай бұрын
Thanks a lot, me too haha.
Thank you Nick.. just changed an already pushed commit message
Really good content. Thanks for your effort!
Thank you. It solved my issues. Great work. 💯❤
Thanks a lot for this, very helpful.
You are awesome! I needed to modify some past commits where my api keys were exposed and this helped a lot.
@NickJanetakis
Жыл бұрын
Thanks, happy to hear it helped!
i usually change playback speed to 1.25 but this is already 1.5. very helpful!
@NickJanetakis
2 жыл бұрын
No problem, thanks for watching!
Great summary my man. Speed of light pacing, I like it. ;)
@NickJanetakis
4 ай бұрын
Thanks a lot for watching! Do you also watch videos at 2x speed?
Very helpful, thank you!
You are amazing dude!
@NickJanetakis
Жыл бұрын
Thanks!
Very helpful.. thank you so much 🙏
Excellent Video !!
This is such an excellent feature you have explained with this video. So useful ! Saved from doing Dravidi Pranayam!
@NickJanetakis
Жыл бұрын
No problem, thanks for watching!
You saved my day, thanks a lot
You teaches very well
@NickJanetakis
2 жыл бұрын
Thanks a lot.
Thank you very much. :)
Thank You a lot.
Thanks a lot!
Amazing video! I had to edit a comment from an old commit for work because I left a curse word 🤦
cool! nice content!
@NickJanetakis
3 жыл бұрын
Thanks!
i havent seen you actually showing how to change the date in git --amend as i have been doing the same thing but after making changes in git --amend and saving them by typing in command line :wq, it was not saving them dk why
Thanks! A really good video!!! I have a question, how can I edit the first commit? (tried the tricks you mentioned but got an "invalid upstream" in this case)
@NickJanetakis
2 жыл бұрын
Invalid upstream would be unrelated to editing commits. It's hard to say what happened, you might have been on the wrong branch before starting to edit / rebase your commits. Editing the first commit is at 2:04, if you mean first as in the latest commit. If you mean first as in the first commit of the project, then 6:20 covers this workflow which would be the same for anything but the latest commit.
@il5548
2 жыл бұрын
@@NickJanetakis I tested again with a new project file, with only the master branch, and in total two commits. While I am in the latest commit, I used the command "git rebase --interactive HEAD~2" to try changing the commit of the first commit of the project, and eventually getting that warning again🥺 Sir, any idea on how to fix that problem? 🥺
@NickJanetakis
2 жыл бұрын
@@il5548 Not without any extra details sorry. What happens if you use HEAD~1?
@il5548
2 жыл бұрын
@@NickJanetakis HEAD~1 will have no problem, but the windows popped up will not show that commit I want to change (only the latest commit shown in my case)
@il5548
2 жыл бұрын
@@NickJanetakis it is like the same note that you mentioned in the video at 8:51
Love this video, very helpful. How would I remove a file that I added in a previous commit? For example, I changed maybe 5 files, added them with commit --amend. But now I want to remove one file from the commit altogether. How would I do that?
@NickJanetakis
2 жыл бұрын
It sounds like you would still do an interactive rebase like this video except instead of adding the file you'd remove the file using git, it would be the same way to remove a file with git with git rm command and other variants (maybe adding --cached to keep the file on disk, etc.).
@ShitsAndGiggles124
2 жыл бұрын
@@NickJanetakis thanks!
@2:25 hahahahaahaha
13:02 James Madison and the first U. S. congress in 1791:
@NickJanetakis
2 жыл бұрын
Haha nice reference.
Thanks for this tutorial, it helped me a lot ! I accidently deleted some code by another contributor and noticed it first when I wanted to rebase onto master after several commits. I approached it like this: - rebase to last commit where code still exists - copy paste the deleted code to another place - stash - continue with rebase to the next commit where I've overwritten the other contributor's code - pop stash - resolve merge conflict and continue with rebase Is this a good approach or is there a more intuitive way? Just curious :) Thanks a lot !
@NickJanetakis
Жыл бұрын
Hi, there might be an easier way with cherry picking parts of a commit into another commit but that's not something I know about in detail, but there's stackoverflow.com/questions/1526044/partly-cherry-picking-a-commit-with-git. I think I would have solved it your way if I had the same problem, or if it were pushed to a remote I'd probably just copy / paste it from the remote assuming it's only a couple of functions.
@kipsta1993
Жыл бұрын
@@NickJanetakis Thanks for the input. I heard about cherry pick but didn't come to my mind as an option. I always thought when you use copy and paste in git you are doing something wrong.^^
@NickJanetakis
Жыл бұрын
@@kipsta1993 I wouldn't worry too much about it, I've lost track of how many times I've deleted a local repo only to clone it from a remote again because I couldn't back out of some craziness I did.
how do i get out of the editor after changing the commit message?
@NickJanetakis
3 жыл бұрын
If you're using Vim you would type :q to quit, but if you wanted to write the message and quit you would do :wq.
Helpful video! But how to change the first commit? When "git rebase --interactive ^ " git says "fatal: invalid upstream ^"
@NickJanetakis
2 жыл бұрын
That's a good question, I never had to do that before. Turns out you can run: git rebase --interactive --root , to get the "root" commit (first commit).
How to rebase unstaged changes?
@NickJanetakis
Жыл бұрын
There wouldn't be anything to rebase in that case since they're not commit. kzread.info/dash/bejne/kaCs1rGSoqbQZbg.html goes over how you can unstage both tracked and tracked files if that's what you're asking.
It seems someone has made changes to my commit and most probably it's using amend. If you could help me with finding the id of the commit before amend, or revert back to my old commit before amend, would be very helpful
@NickJanetakis
2 жыл бұрын
I don't know, sorry. Maybe running a git reflog could give you some info tho. It tends to have a lot of information.
@dhirenpunjabi9167
2 жыл бұрын
@@NickJanetakis But I won't be able to see someone else's amend if I do git reflog in my system. Will have to reflog in the suspected system to check. Could you correct me if I'm wrong ?
@dhirenpunjabi9167
2 жыл бұрын
A video on this i.e. finding the suspect, would be super helpful though. If it were possible.
@NickJanetakis
2 жыл бұрын
@@dhirenpunjabi9167 Your reflog should have your original commit sha before it was amended?
11:07
You are very fast, as one who not speake native english this is hard to understand, however thank you
@NickJanetakis
Жыл бұрын
You can try setting the playback speed slower. Also sorry, unfortunately talking does require saying words!
9:55 "exiting without saving will bring you back" i will call this as aborting the current command for now so, my question is, how to abort the `git commit --amend` ? say @ 3:00 i deleted all content, and exit with :q! , a new sha is still generated. which means the command was not aborted, rather it proceeded to completion.
@NickJanetakis
2 жыл бұрын
Hi, good question. It looks like if you try to save an empty git commit message and save it with :w then git will fail to write and abort it.
@yash1152
2 жыл бұрын
@@NickJanetakis thanks for reply. yeah, i tried and it worked.
Too fast for me
18 minutes 💀💀
@NickJanetakis
Жыл бұрын
9 minutes if you 2x speed it, or potentially 30 seconds if you jump around the chapters to the specific parts you're interested in.