Git Tutorial: How to Undo Git Commits, Rename a Git Branch and Checkout a Previous Commit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Undoing git mistakes can be difficult if you don't know how. This video steps through examples of how you can undo all types of actions in git as well as explaining git branches and merges. Full outline and timestamps below!

🕓 Outline with timestamps:

(0:04) Recap Quick Start

(1:20) git restore [filename] (undo changes)

(2:18) git restore --staged [filename] (undo staged files)

(3:09) git log (show log with details)

(3:45) git log --oneline (shows simplified log)

(4:35) git commit -v --amend (change last commit's message)

(6:35) git branch [newBranchName] (creates new branch)

(7:50) git branch --list (lists all branches)

(8:05) git checkout [branchName] (switch to this branch)

(8:10) working with more than one branch

(12:25) git branch -m [oldBranchName] [newBranchName] (renames branch)

(14:10) git checkout -b [newBranchName] (creates new branch and switches to it)

(15:00) git branch -D [branchName] (deletes branch permanently)

(16:00) git merge [branchName] (merges named branch into branch you are on)

(17:10) Understanding and handling git merge conflicts

(22:30) git checkout [commitNumber] (go back to a specific commit number)

(27:00) git revert [commitNumber] (revert code to a specific commit number by creating new commit)

(29:30) git reset [commitNumber] (reset code to a specific commit number and unstage the code that came after that commit)

(31:45) git reset [commitNumber] --hard (reset code to a specific commit and permanently delete the code that came after the commit)

Git Tutorial: How to Undo Git Commits, Rename a Git Branch and Checkout a Previous Commit: https://youtu.be/EGooeHH3hDY

👍︎︎ 6 👤︎︎ u/DaveOnEleven 📅︎︎ Aug 29 2020 🗫︎ replies
Captions
hello and welcome today we are fixing get mistakes and that is often what happens after a quick start previously i issued a quick start guide and will recap very quickly because it has some hints to fixing mistakes along the way that we didn't cover before of course the first thing you must do i already did with this repository is use git init to initialize git in your project after that you usually check the status and it says we are up to date with origin master and that is what we have at github and we linked those in the quick start guide it says nothing to commit working tree is clean so let's remove a paragraph and save the file and see what git says to us now when we come back and check the status one more time and now we have a modified file you see the m by the index and the file tree and in git bash our terminal window you see modified index.html but there are some hints above this as well now we haven't added anything we haven't committed anything it says change is not staged for commit and you stage a file or a change for commit when you add it and so it says use git add file to update what will be committed you can also use git restore file to discard changes in the working directory so if we made a mistake if we decide something we changed in this file is not what we want to uh have had happen and we just want to discard those changes we can do git restore index.html and our third paragraph has returned up here in our indexed html file now once again i'll remove that so we can go on and test something else come back here and we'll check the status and there's our modified file now i want to go ahead and add the file and now i have staged it for commit so let's check the status again and now it says changes to be committed and you can use get restore dash dash staged to unstage the file and so now if we added a file that we did not previously want to add for some reason we've staged it for a commit using the add command and we do not want it there and we do not want to commit it we can use git restore staged index.html and now it once again if we check the status that file will need to be added it is no longer staged for commit and so that gives us a couple of hints as we go through the process okay we've used git status to check our status just like we did above here several times on our branch there's also something we can do to check our commits and our history and that is get log let me make the terminal window the full size of the screen here git log and here is a full history of our commits notice each commit has a very long id and we've got more details with each commit as well here you can see remove paragraph added third paragraph each commit message along the way but if we don't want something that's quite so large we can pare that down by using the dash dash one line flag now let's look at our message and so this is what i usually do i use git log dash dash one line and now you can see a smaller id for each commit and you can see it says we're on the master right here and here is the removed paragraph commit message and below that added third paragraph and you can see i pulled that in from origin master at github we haven't discussed pull yet and here were the previous commits we had also made and so it gives us that full history so let's say we just made a commit and we want to change that message we left out a note that we should have added or we left a typo whatever it is and i typed remove paragraph and you can see just by the standard formatting here all the other commits start with a capital letter and for some reason i didn't go ahead and format that commit the way i had previous commits and i want to change that we can change that commit message what we need to do is type git commit dash v which is a verbose flag and then dash dash amend and this is going to give us a little bit of a different looking text editor here it goes and there it is okay this looks more like a text editor we'd see in linux and here is our commit message at the top removed paragraph and you can see at the bottom of the screen even here it says unix commit edit message what we need to do or use some keys you might not be used to we're going to type the key i for insert but that will let the text editor become active and now i can type capital r backspace over the lowercase r now this is the one that i want to keep and so now i need to do shift colon whoops i need to do escape first sorry so we need to escape out of the editor and now with the escape key and now do shift colon and i want to do w for right and then q for quit now you can see we have amended our commit message so when we do get log dash dash one line you can see our removed paragraph now has the capital r but i could have changed that entire message if i wanted to okay so one of the more powerful things about git is the ability to cr to create branches and that is because we can have an active code base say a website that we've already designed and deployed and we have that as our active master branch our master code base and you can see that's what git defaults to but then we want to create a new branch and redesign the header for the website and keep all of that code in the new branch and only merge it when we have tested it and confirm that it's ready and or say we're working on a team and we assign the styling the web design part of it to one team member we assign a new header to another team member or the logic behind the search engine something like that and those can all be different branches and then merged back into the existing code base when they're ready so let's take a look at that right now we're on the branch master and we can type git branch dash dash list and list all the branches and you can see we only have the master branch so we can create a new branch let's say we're going to make a new header for our website with git branch and i'll type my new header and now i can type git branch dash dash list and you can see both branches listed and in green is the master and our prompt also shows master you can see we're still on the master so we need to switch to the my new header branch to write our new code for our new header and we'll type git check out my new header and it says switch to branch my new header and you can see at the prompt it also says my new header we are now on the my new header branch and this is where it can get confusing but we are keeping a different code base essentially we're working with the same code base we start at but now they can diverge and go in different directions however if a file isn't tracked it can be confusing so let's add a page three html file and we can do that kind of like a linux command here even though i'm on windows with touch page3 dot html of course we could also come over here into the file tree and just use the new file icon but no you can do it this way i'll enter and look in our file tree we now have page three html now see it is untracked so at this point if we switch back to the master we're not really tracking that page three html file in our my new header branch yet so if we just so git checkout master we're still going to see it over there in the file tree it's just an untracked file it doesn't really belong to either branch at this point so i'm in master and this can be confusing because you could think shouldn't that file only be in our new branch not yet because it's untracked so let's switch back to my new header now we're in the my new header branch and i'll do git add all and well let's check this oh there it shows added it just took a second to update it has been added and now we can go git commit message added new page and now we're still in the my new header branch and we have added that page three and committed it in our my new header branch now if i switch back to the master branch get checkout master look page 3 is no longer here in the file tree that's because it does not exist in the master branch it's something we've added and we've only added it to that new branch so when i switch back to my new header page three shows back up and we can edit the code we could also make changes now that we're in my new branch let's add some more paragraphs let's say we're going to add three lorem epsom paragraphs here with 10 words this is an image shortcut and we've added those paragraphs let's save the file let's add our changes stage them to be committed now let's commit the changes to index this is all in the my new header branch we check the status everything looks good there let's check the log in this branch and you can see in the log we've got added new page added new paragraphs and it shows this is all under the my new header branch now let's switch back to the master look those new paragraphs have disappeared from the index page we no longer have our third page here we are back to the way things were and that is because we have a branch system where we can develop new code and not merge it into the code that we have deployed until it is ready and so now we've got the two different branches going and we've decided that hey we made a mistake we want to rename my new header and we're going to call it my new content because clearly we've added another page not just a header we've added paragraphs and so we named that incorrectly so how do we rename a branch we use git branch dash m and now we want to say my new header name the old branch and now we want to say my new content the name of the new branch and so now when we do git branch dash dash list you can see we have master and my new content so let's switch over to the my new content branch which is the new name and you can see it still has all that new code it has the new paragraphs it has the new html page and we are in the my new content branch and that is the new name for what we were calling my new header now let's also take a look at the log for each whoops can't type here's git log one line and it shows my new content and master so let's switch back to master and look our log on the master doesn't have those new commits because it has not been merged with the new branch yet so our new content log looks different than our master log okay i should also show you a shortcut that is commonly used sometimes you can do the check out and create a new branch all on one line and this is very common practice instead of doing get branch the new name of the branch and then get checkout to switch to that branch you can type get checkout dash b which means you want to create the new branch all on the same line and then we can say my new branch and we have switched to the new branch and created the new branch all at once so now if i do git branch dash dash list you can see we have master my new branch my new content and we've already switched to my new branch now let's say we created a new branch by mistake and we simply want to remove that branch we can do that with git branch dash capital d we're going to delete the branch and say my new branch oh cannot delete new branch we're not checked out yet so let's go get check out master and we want to be on the master we don't want to be in the branch we're trying to delete now let's go ahead and type git branch dash capital d my new branch and yes we've deleted the new branch so when we go get branch dash dash list we're back to master and my new content as the existing branches okay we've learned about new branches but now let's say we've completed our code or completed our new feature header whatever we're working on and we're ready to merge it back in so you need to know how to merge your branch back into the master and that is simply with git merge and then type the branch name get merge my new content and you can see it says we fast forwarded because there were changes that the master was not up to date on so we fast forwarded and added those changes there's 11 new lines of code page 3 actually has no content but it did add the new file so two files changed with 11 insertions and now those branches are merged we can check our status everything seems fine here we're ahead of our origin master by two commits which is at github we haven't pushed our code to github yet let's check the log and now our master log has all of the changes that we were just seeing with new content and you can see master and new content are both at the same point so they have been merged and one is not ahead of the other we're all at the same point and everything is in agreement and that's great but let's say we have a problem and problems can arise with the merge so we need to go ahead and make a change in each branch that will create a problem and that happens when we change code on the same line so we're in uh master let's switch back to my new content and now we'll make a change here say this is a paragraph let's just say this is a sentence we have changed that and we will check our status here we have modified a file so we want to add and then we want to commit put corrected content and we've committed that in my new content once again that change is only in the my new content branch let's check out go back to the master now in master still says this is a paragraph now here i want to say this is a short paragraph now i've changed the same line of code in each branch so it's going to create a conflict when we attempt to merge let's do that git merge my new content and of course we're in the master branch when we're merging in other branches so git merge my new content i'll hit enter and yes we are going to have oh we didn't make our commit yet in the master i should do that first let's go git add it's warning me that i was going to lose my changes do that git add commit him i'll put changed to short paragraph okay now we have commits in each branch that are not going to agree because we have changed the same line of code in each branch so let's try that git merge again git merge my new content yes and it tells us instantly there is a conflict and there's a merge conflict in index.html automatic merge failed but we can fix the conflicts and then merge the results and it shows us this in the code and visual studio code gives us some options so you can see the incoming change from the my new content branch says this is a sentence the current change from the master branch says this is a short paragraph and now in visual studio code we can accept the current change or accept the incoming change we can accept both changes and kind of work it out later on and we can even compare the changes let's just go ahead and keep it simple for now and we want to accept the incoming change because this is really well we use the paragraph let's let's accept the current change we'll just click either one of these we've accepted the current change now notice we have a c over here for conflict at this point and it says master and it also says merging so we're not quite finished yet but we did tell visual studio code we want to accept the current change so let's check our status and it says we have got both modified because we merged in the index page from the my new content branch and so it kind of wants to know what to do and we've also got unmerged paths up here it says fix conflicts and run git commit so we fixed the conflict the next thing we need to do is run git commit and of course we could also go we don't want to merge these yet we want to work out the conflict before we merged them and with that we could do git merge dash dash abort to abort the merge but we went ahead and used the visual studio code option and it let us fix the conflict fairly quickly so we're going to do another commit both modified so we'll do git add well no no we can just commit fixed conflict oh let's see what it told us committing is not possible because we have unmerged files fixed them up in the working tree we did that oh we needed to do the add first sorry about that git add all now we'll do our commit fixed conflict and there we should be good let's check the status we're up to date and it says this is a short paragraph which was the change we accepted now everything is good to go and we can check our log yes there is our last commit message fixed conflict another powerful feature of git and working with branches is that we can go back in time to how our code was previously at a different commit level so let's once again take a look at our log one line command and there you can see our log with each commit and you can see in the last one we fixed our conflict uh we changed to a short paragraph at that commit uh added a new page down here so let's say let's take this id the shortened id compared if you just look at git log you will see a much longer id but we just need the short one let's do git checkout and let's paste in that id and now we get several things here a little bit of uh verbose action from git so let's see what it says said we now switched and we switched to that point in our commit log and it says we're now in detached head state and we can look around make some experimental changes and commit them if we switch back we'll discard those changes essentially but we could create a new branch and move forward from this point in time if we wanted to and look at these commands it says git switch with dash c which means create and that would be a new branch name or we can just undo the operation with git switch and a dash so it says right now we're at this point the uh commit we wanted to go back to and you can see it doesn't say master here or the name of any branch it's giving that commit id so let's look at our log and see what it shows now we went back in time essentially and you can see we're just back to the added new page and we're not on a branch we're in the detached head state of git and so we're back to this point in time in our project and once again we could create a new branch and make changes in that branch from this point on and ignore those commits that came after and then somehow later on merge back in if we wanted to or we could just come back here and look at how everything looked at that point in time but right now if we want to go back to where we currently are let's go to get checkout master and now if we check our log we'll have everything back the way it was and so we can go back and look at things the way they were at a specific point in time using git checkout and the id number now let's talk about undoing some things what if we made some changes even some commits and we've merged things in and then we decide we don't really like that new content at all and we want to go back to the way things were there are several ways to do that in git as well and one of those besides we just kind of did the get checkout id we went back in time and looked at the way things were let's do a well first of all it looks like we haven't saved our index page here you can see it is not saved it has the dots so let's go ahead and save that let's check our status on master and make sure everything's the way we expect it to be it says it's modified so we're going to add everything and quickly do a commit up to date changes really doesn't mean much at this point but there it is says one file change for deletions and we'll check our status there we go we're ahead of our github repo by six commits our github repos origin master that's our branch at github and there's nothing to currently commit so now let's look at that log again and there we can see our changes and we've got a fixed conflict change to the short paragraph corrected content all of those things and say we don't like any of that new content so we want to go back in time and not just look around but we actually want to go back and basically make all of these changes so they did not happen and or they've happened but we can essentially revert them and that's what we're going to do with the git revert and then put in that id number it says auto merging index and now we have got to do some things here it wants us to says this reverse this commit and it gives the long id revert added new paragraphs to index that seems fine we want to do shift colon we'll do write quit with wq press enter and it merged reverted to added new paragraphs to index so if we wanted to change that commit message at that point we could actually it's a new commit message so we could add on to it i'll show you what i mean by new commit message even though we reverted back to previous code what it did was instead of just jumping back to this added new paragraphs to index where we wanted the changes we wanted our project to be like it was at this point in time and ignore all these changes it essentially does that by creating a new commit reverting the changes with a new commit and coming up here and now it says revert added new paragraphs to index and we've saved the updates with that revert so we didn't really lose the previous code we could go back and revert to these and that's why reverting is kind of a safe change even if we want to go back ignore some things because say next week we decide no we should go back and add that new content we could once again revert and go back to this previous code we had as well and so that reverting is a great way to ignore some changes but also save those changes at the same time in case you want to go back now i'm going to show you a more destructive way that will get rid of the changes that we have made and so once again let's choose this added new paragraphs to index commit and we will choose git reset or is what we will use and we'll put that and what that is going to do is it's going to essentially uncommit it will delete those commits it will make the work the work we've done will still exist but it will be unstaged so let's do this unstaged changes after reset and so now we have modified files if we check our status you can see those other paragraphs are gone if we had those before and now let's go ahead and check our other branch it says your local changes to the following files will be written by checkout so i needed to have staged those first so it didn't switch to the new branch we'll go ahead and do that and we will commit those we'll say reset project to added new paragraph point in time okay now i'll just check out the my new content branch check the status there nothing to commit that's all good go back to the master branch and we're ahead of our origin master by three commits instead of six so we've actually gone back in time let's look at our one line and you can see this is the one we went back to added new paragraphs to index and now all those commits after that are gone and the only commit that is here is the one that i just made where we said we reset the project to the added new paragraph now that unstaged a lot of the commits and then i had to make a commit but let's say we wanted to go back a little bit further let's just go back to the well let's go back to the same one i'll just use the same one here because it's just one commit back and we can tell get don't just unstage those commits we just don't want them don't keep those around we're finished with those please delete them as you do this and that is with the hard flag so get reset put the commit id we want to go back to and with dash dash hard we're discarding any changes that came after the commit that we identify so now when we look at our log it no longer has the commits that we're after i guess the one commit it just took us back to added new paragraphs and it doesn't show anything modified here because there aren't any changes that aren't unstaged it deleted all of those changes and so that is really how you go back and remove any of those things that you no longer want and that gets us to the end we have covered a lot today and because of that i've made this get notes screen where you can pause the video and take some notes take your time start a practice project and practice all of these commands and get familiar with them and that way you'll be comfortable in actually using them when you're working on a serious project or a project of your own but just make some project like i did in this video and practice through the different commands and making changes and going to different branches and all of the things that i did in this video to help you get familiar with each aspect of the git commands and there are many more and we'll cover some more in the future hi i'm dave and i hope this tutorial has helped you learn more about working with git remember to keep striving for daily progress instead of perfection subscribe to my channel and ring the bell to be alerted when i post new tutorials i'll see you next time
Info
Channel: Dave Gray
Views: 665
Rating: undefined out of 5
Keywords: git, git tutorial, git commit, git branch, git merge, undo commits, rename branch, merge branch, git revert, git reset
Id: EGooeHH3hDY
Channel Id: undefined
Length: 33min 49sec (2029 seconds)
Published: Fri Aug 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.