Git Tutorial 5: Undoing/Reverting/Resetting code changes

codebasics · Beginner ·📊 Data Analytics & Business Intelligence ·9y ago

Key Takeaways

Explains how to undo, revert, and reset code changes in Git

Full Transcript

hello and welcome to core basics coding tutorial and this tutorial we are going to see how to undo or reset or revert your code changes and get for this we are going to first clone our github repository in my previous tutorial I created this test repository I am just going to click here to copy the link and in git bash I will run git clone come on to clone the repository locally now I have this Secord directory and if you look at this directory I don't have any taste director here but once I clone it I will have it so now I got it ok now I'm going to open this test directory in 2py charm so I have opened it here already I have my Hungary code here you might be familiar with this code if you have watched my previous get tutorials all I'm going to do is make some changes and show you how you can undo those changes ok so the first item we are covering is undoing your uncommitted changes so let's say I change this code and add one new line alright so I am adding this new line I am running the program and then I realize that I am feeding too much junk to my kid you know I don't want my kid to look like this so I want to cut down on his Pisa intake so I will undo this one line alright for doing that I will go to my git bash first run git status come on I was not in this directory that's why I give me that error so I'm now in test directory running git status it is showing me I have changed hungry dot py file ok before this you see this useful tip it says you can then get check out - - filename to discard changes in your working directory so I'm just going to use this get check out - - file name is hungry dot py hit enter when I do that my Pisa change is gone all right now I can easily undo that change by removing that last line but when you are doing programming in real life you might have hundreds of lines of code changed for that you are not going to manually undo all of those lines right it's very cumbersome so that's why you you will use git checkout - - command ok now what if you have multiple files change so again you have let's say ah this file changed first of all and you have readme file change as well as the you added some garbage here and now when I do get status I will see two Faiza change one option is I do get checkout - - this file name first and then the second file name but there is a convenient option of using dot when you do dot it is going to undo all the files so when I do get status here you see both of these files are gone and when I open it here in my py charm code editor PISA is gone and that junk is gone alright so this was a quick demo on how to undo uncommitted changes but what if you have committed changes so let me first commit some changes all right so I have my PISA change I am going to commit that so always do get add to add your file to staging area then do git commit - M means message Fiza alright this commits my change how do you know it's committed you can always run git log and in the git log it is showing that this change is committed with this particular commit ID okay life is good so far I want to eat more junk food and I will say eat burger ok cool again go here and get ad now one thing you notice I always do get ad and then get do then do get commit right so there is one easy option called get commit - km now when i do - am the problem means is gonna probably add this one as well so ok i'm not going to do that so i will just do it ad hungry and then get - hamburger okay clear it and again check it log and now I have Pisa and burger both committed good all right now what I want to do is I want to undo this burger chain okay now I can't run git checkout - - because the change is already committed so for this you can use git revert come on okay now that command would need the commit ID so I am going to just copy it so double click here copy the commit ID and then run git reword that particular commit ID okay it's just showing the summary that you are rewarding that particular commit and you need to type column Q to close this UI and once you do that the change is committed so let me verify okay you see that Berger is gone okay and if you run git log here you will see there was a burger change and now there is a reward burger chain awesome so this looks good so far now one thing you might have noticed is when I did get reward it directly committed the change let's say I don't want to commit the change I want to run get reward and then I want to explicitly committed okay you can do that by using - and option so first let's look at log okay and now let's say I want to undo this work up this pizza chain okay so first you need to copy this okay and then do get every word - n so it's the same command that we ran previously but now we have - an option when you do this or a let me show here when you do this that change that P's aligned is gone all right but the change is not committed so if you do get status you will see now that file is modified it is uncommitted so now you need to explicitly commit it you can review your change and explicitly commit it and say we work visa so now when you do get log you will see the word Pisa here so the difference between supplying a - n option versus not supplying it is the - an option will not commit your changes versus if you don't supply anything it will just commit the changes okay okay now the third item we are covering today is git reset so I have all this git log history all right this shows the history in time of how my code evolved okay so in my code evolution let's say I want to go back to this status where I had Pisa and Burger both okay I realized that whatever changes I made after that they are not useful and I want to undo those changes okay so for doing that you can use git reset command so first let me copy which commit ID I want to go to so let's say I want to go to this status on Friday August 5 1954 so I will copy this and I will do get our reset - - hard let's commit ID and now I am when you say git log you'll notice all those river changes are gone and in your working directory you are back to eating peas and burger okay so git reset is actually quite powerful it takes you to any previous timestamp in your code history but at the same time it is destructive as well if you make a mistake then you will just lose all the changes so be very careful when you are using git reset command alright so that was all about undoing changes and gate thank you for watching

Original Description

In this tutorial, we will cover how to undo or revert a code change. Also I will show you how to reset your git branch to any previous commit id. Here are the topics we will conver in this git tutorial, 1) Undo uncommited changes using, git checkout -- 2) Undo commited changes using, git revert 3) Resetting changes using, git reset Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Git Tutorial: https://www.youtube.com/watch?v=xAAmje1H9YM&list=PLeo1K3hjS3usJuxZZUBdjAcilgfQHkRzW Next Video: Git Tutorial 6: Branches (Create, Merge, Delete a branch): https://www.youtube.com/watch?v=sgzkY5vFKQQ&list=PLeo1K3hjS3usJuxZZUBdjAcilgfQHkRzW&index=6 Machine Learning Tutorial With Python: https://www.youtube.com/watch?v=gmvvaobm7eQ&list=PLeo1K3hjS3uvCeTYTeyfe0-rN5r8zn9rw 🌎 My Website For Video Courses: https://codebasics.io/ Need help building software or data analytics and AI solutions? My company https://www.atliq.com/ can help. Click on the Contact button on that website. Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 47 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related Reads

📰
Scraping Indian government open data in 2026: what actually works
Learn to scrape Indian government open data for projects like Village Finder, a mapping project tracking 78,000 Indian villages
Dev.to · Manideep Chittineni
📰
2026 Enterprise Data Recovery: What IT Teams Should Look For
Learn what IT teams should look for in 2026 enterprise data recovery to minimize losses and ensure business continuity
Medium · AI
📰
I Was Mad About the Jaylen Brown Trade. So I Simulated 10,000 Seasons to See If I Should Be.
A Celtics fan uses simulation to analyze the Jaylen Brown trade, applying data science to understand its potential impact
Medium · Data Science
📰
Mosaic Plots in Data Visualization: Turning Complex Relationships into Clear Business Insights
Learn to create mosaic plots for clear business insights from complex relationships
Medium · Data Science
Up next
Modified Distribution Method (MODI) In Transportation Problem /Operations Research/Statistics
EZIKAN ACADEMY
Watch →