Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits

Corey Schafer
26 Oct 201521:31

Summary

TLDRThis video offers an advanced Git tutorial for undoing mistakes and rolling back commits. It covers various scenarios, including fixing bad commit messages, undesired file changes, and committing to the wrong branch. The tutorial introduces commands like 'git checkout', 'git commit --amend', 'git cherry-pick', and 'git reset', explaining their impact on the Git history. It also addresses how to handle situations where changes have been pushed and need to be undone without altering the shared history, using 'git revert'. The script is designed for those familiar with basic Git commands, aiming to help maintain a clean and accurate project history.

Takeaways

  • 😀 The video provides an advanced Git tutorial focusing on undoing mistakes and rolling back commits.
  • 🔧 It explains the difference between Git commands that change history and those that don't.
  • 📚 Assumes viewers have basic Git and command-line knowledge, with a reference to a beginner Git video.
  • 📄 Demonstrates how to undo unwanted file changes using 'git checkout' without committing.
  • 💾 Shows the process of correcting a bad commit message with 'git commit --amend'.
  • ⚠️ Highlights the importance of not changing Git history if others have pulled changes to avoid conflicts.
  • 🔀 Discusses how to handle committing to the wrong branch and moving commits to the correct branch using 'git cherry-pick'.
  • 🗑️ Covers the three types of 'git reset': soft, mixed, and hard, and their effects on the repository.
  • 🛠️ Introduces 'git clean' to remove untracked files and directories, useful for cleaning up after mistakes.
  • 🔄 Explains 'git reflog' as a way to recover lost commits that were accidentally removed.
  • 🔄 Details 'git revert' as a method to undo commits in a way that doesn't rewrite history, which is safe for shared branches.

Q & A

  • What are some common scenarios where you might need to undo changes in Git?

    -Common scenarios include needing to revert bad commits, correcting mistakes, or rolling back changes that were not intended to be committed.

  • What command can you use in Git to discard changes in a file without staging them?

    -You can use the command `git checkout -- <file>` to discard changes in a file without staging them.

  • How can you correct a commit message that was previously made in Git?

    -You can correct a commit message by using `git commit --amend` followed by the correct message.

  • What is the difference between a soft, mixed, and hard reset in Git?

    -A soft reset (`git reset --soft <commit>`) moves the HEAD to a specific commit but keeps the changes in the staging area. A mixed reset (`git reset --mixed <commit>`, which is the default) moves the HEAD and keeps changes in the working directory. A hard reset (`git reset --hard <commit>`) moves the HEAD and discards all changes.

  • How can you move a commit from one branch to another in Git?

    -You can move a commit from one branch to another by using `git cherry-pick <commit-hash>` on the target branch.

  • What does `git reflog` show and when might you use it?

    -`git reflog` shows a log of commits that you've interacted with, which can be useful for recovering commits that were accidentally lost, such as after a hard reset.

  • How can you undo a commit that has already been pushed to a remote repository?

    -You can undo a commit that has been pushed by using `git revert <commit-hash>`, which creates a new commit that reverses the changes made by the specified commit.

  • What is a 'detached HEAD state' in Git and why should you be cautious about it?

    -A 'detached HEAD state' occurs when you're not on any branch, typically after checking out a specific commit. You should be cautious because any commits made in this state will not be associated with any branch and could be lost if you switch branches without saving them.

  • How can you remove untracked files and directories from your Git repository?

    -You can remove untracked files with `git clean -f` and untracked directories with `git clean -fd`.

  • What does `git diff` show when comparing two commits, and how can it be useful?

    -`git diff` shows the differences between two commits, which can be useful for understanding exactly what changes were made and to verify that a revert commit undoes the intended changes.

Outlines

00:00

📚 Introduction to Undoing Mistakes in Git

The video begins with an introduction to advanced Git techniques for undoing mistakes and rolling back commits. The presenter assumes viewers have basic Git knowledge and offers a link to a beginner Git tutorial for those who need it. The focus is on commands that either alter or preserve the Git history, with an initial demonstration of a clean working directory and a single commit in the repository. The presenter then simulates a common mistake—making unwanted changes to a file—and shows how to revert these using 'git checkout' to restore the file to its last committed state.

05:01

🔄 Correcting Commit Messages and Amending Commits

The second paragraph delves into the scenario of correcting erroneous commit messages. The presenter demonstrates how to amend a commit message using 'git commit --amend'. It's highlighted that this changes the commit hash because the message is part of the commit's content. The video then addresses the situation of accidentally committing to the wrong branch and wanting to include changes in a different branch. The presenter uses 'git commit --amend' to include a forgotten file in the previous commit, illustrating how to maintain a clean commit history without additional commits.

10:02

🌿 Resolving Commits to the Wrong Branch

In this segment, the presenter tackles the common issue of committing to the wrong branch. Using 'git cherry-pick', the video shows how to move a commit from the master branch to a feature branch, thereby preserving the intended project structure. It's explained that 'git cherry-pick' creates a new commit on the target branch based on the source commit. The presenter then demonstrates how to remove the now undesired commit from the master branch using 'git reset', distinguishing between the three types of resets: soft, mixed, and hard. Each reset method is tested, showing their effects on the repository's state.

15:02

🗑️ Reverting Changes with git reset and git clean

The fourth paragraph explains how to use 'git reset' to remove commits that were made in error. The presenter uses 'git reset --hard' to revert all changes back to a single initial commit, cautioning that this method is risky as it can permanently delete changes. To further clean up, 'git clean' is introduced to remove untracked files and directories, ensuring the working directory is completely free from unintended changes. The presenter also discusses the importance of using these commands cautiously, especially when changes have been shared with others.

20:03

🔐 Recovering Lost Commits with git reflog

The final paragraph addresses the recovery of commits that were accidentally removed using 'git reset --hard'. The presenter introduces 'git reflog', which logs all actions that change the repository's state, allowing users to find and recover lost commits. By checking the reflog, the presenter locates the hash of a commit that was mistakenly deleted and restores it using 'git checkout'. The video concludes with a discussion on 'git revert', which is used to undo commits in a way that doesn't rewrite history, making it safe for collaborative environments.

Mindmap

Keywords

💡Git

Git is a version control system used for tracking changes in source code during software development. It is fundamental to the video's theme as it is the primary tool discussed for managing and undoing changes in code repositories. The script covers various Git commands to demonstrate how to handle mistakes or rollback commits, showcasing its importance in maintaining code integrity.

💡Commit

A commit in Git represents a snapshot of the project's state at a particular point in time. It is a collection of changes to the code that are recorded in the Git history. The video discusses how to undo mistakes by rolling back or amending commits, emphasizing the significance of commits in version control.

💡Checkout

The 'checkout' command in Git is used to switch branches or restore working tree files. The video uses 'git checkout' to revert changes made to a file, illustrating how it can be used to undo modifications that were not committed yet, thus preserving the repository's state.

💡Diff

The 'diff' command in Git shows the differences between commits or changes in the working directory. In the video, 'git diff' is used to display the changes made to a file, which helps in understanding what modifications have been introduced and need to be undone.

💡Amend

The 'amend' option with Git's commit command allows users to modify the most recent commit message or include additional changes. The video demonstrates how to correct a commit message using 'git commit --amend', highlighting a practical approach to fixing mistakes in commit history.

💡Branch

A branch in Git is a separate line of development within a repository. The video discusses managing branches, such as moving commits between branches, which is crucial for organizing and isolating different features or changes in a project.

💡Cherry-pick

The 'cherry-pick' command in Git is used to apply changes from one commit to another branch. The script uses 'git cherry-pick' to move a commit from the master branch to a feature branch, demonstrating how to correct mistakes in commit placement across branches.

💡Reset

The 'reset' command in Git is used to reset the current HEAD to a specified state, which can be 'soft', 'mixed', or 'hard'. The video explains the different types of resets and their effects, such as 'git reset --hard' to revert all changes back to a previous commit, which is essential for undoing unwanted commits.

💡Reflog

The 'reflog' in Git is a record of changes made in the repository, including actions that modified the tips of branches and heads. The video mentions 'git reflog' as a means to recover commits that were accidentally reset, showcasing its role in safeguarding against data loss.

💡Revert

The 'revert' command in Git is used to create a new commit that undoes all changes made by a previous commit. The video explains 'git revert' as a method to undo commits that have been shared with others, ensuring that the project's history remains intact while still correcting mistakes.

Highlights

Introduction to advanced Git techniques for undoing mistakes and rolling back commits.

Assumption of basic Git command line functionality knowledge.

Demonstration of a clean working directory and initial repository state.

Explanation of using 'git checkout' to undo file changes without committing.

Example of committing changes with an incorrect commit message.

Use of 'git commit --amend' to correct a commit message.

Caution about changing Git history and its implications for collaborators.

How to include a forgotten file in the previous commit using 'git commit --amend'.

Scenario of committing to the wrong branch and the solution using 'git cherry-pick'.

Description of three types of 'git reset' and their effects on the repository.

Use of 'git reset --hard' to revert to an initial commit and remove unwanted changes.

Cleansing the working directory of untracked files with 'git clean'.

Utilizing 'git reflog' to recover lost commits after a 'git reset --hard'.

Creating a new branch from a recovered commit to save changes.

How to undo commits that have been pushed and pulled by others using 'git revert'.

Explanation of 'git diff' to view the changes between commits before and after a revert.

Conclusion summarizing the Git tips for undoing mistakes and rolling back commits.

Transcripts

play00:00

hey how's it going everybody in this

play00:02

video we're going to be looking at a few

play00:03

common scenarios in git where you can

play00:06

find yourself needing to undo some

play00:08

mistakes or even roll back some bad

play00:11

commits um now some of these commands

play00:13

will change the git history and some

play00:15

will not and we're going to take a look

play00:17

at exactly what that means here in a

play00:19

second now this will be a slightly more

play00:21

advanced git walkthr and uh we're going

play00:24

to assume that you already know the

play00:26

basic uh command line functionality of

play00:28

git um if you aren't familiar with basic

play00:31

git commands or how git works I do have

play00:33

a video on getting started with Git in

play00:35

the command line if you would like to

play00:37

watch that video first but with that

play00:39

said let's go ahead and get started uh

play00:42

so let's take a look at the sample

play00:44

repository that I have set up here so if

play00:46

I do an ls- LA within this directory you

play00:50

can see that I have this calc. py file

play00:53

and I also have that cal. py file pulled

play00:55

up in my text editor over here and you

play00:57

can also see that we have theget

play01:00

uh directory here which just means that

play01:02

we are within a git repository so now if

play01:05

I run a git status you can see that

play01:08

currently our working directory is clean

play01:11

if I do a get log then you can see that

play01:13

we've only had one commit so far it's

play01:15

just the initial commit and also if I

play01:18

run a git Branch uh you can see that we

play01:23

have another Branch here called subtract

play01:25

feature um so both uh the subtract

play01:28

feature branch and the master Branch are

play01:30

currently exactly the same they both

play01:32

only have that one initial commit um but

play01:35

for now we're not going to bother with

play01:37

the subtract feature Branch we'll take a

play01:39

look at that uh here in a little bit so

play01:41

let's look at our first example of a

play01:43

mistake that you might want to undo um

play01:46

so first we're going to say that we've

play01:48

made changes to a file and after a while

play01:51

we realize that we don't want any of the

play01:53

changes that we've made so whatever

play01:55

whatever we've done it just isn't right

play01:57

and we want to go back to the way things

play02:00

were uh so for this example I'm just

play02:02

going to uh type in a bunch of gibberish

play02:06

here into uh the text editor but more

play02:10

realistically uh in the real world this

play02:12

would likely be a bunch of code that for

play02:14

one reason or another just isn't right

play02:16

and you want to go back to the way that

play02:18

things were um so uh for this example if

play02:21

I go over here and do a get status you

play02:24

can see that we have modified the file

play02:26

if I do a get diff you can see all the

play02:30

gibberish lines that we've put in there

play02:32

um now the way that we can fix this is

play02:34

to just use a simple G checkout uh so

play02:38

what I'm going to do here is a get

play02:41

checkout calc. py and if I hit enter

play02:45

there now if I do a get status you can

play02:48

see that our working directory is clean

play02:50

and if I do a get diff you can see that

play02:54

we don't have any changes and if I go

play02:56

over here to the file and uh this

play02:58

reloads over here you can see that we

play02:59

are back to the way that things were

play03:02

after we made our initial commit so now

play03:05

let's actually make some changes to our

play03:06

file that we do want to commit to our

play03:08

repository um so I'm going to come in

play03:10

here and change the subtract function

play03:12

and I'm just going to do a return x

play03:15

minus y so that's what we want so now

play03:18

I'm going to go back over here to our

play03:20

terminal now if I run a get status you

play03:24

can see that the file has been modified

play03:26

in the working directory and I'm going

play03:28

to add that to the staging area and now

play03:31

if I do another G status you can see

play03:33

that that file is ready to be committed

play03:35

so now I'm going to go ahead and commit

play03:38

this file um but let's say that we make

play03:41

a get commit and I accidentally mess up

play03:44

our commit message so I'm going to say

play03:47

that we completed the multiply function

play03:50

which is wrong because we uh completed

play03:53

the subtract function so I'm going to go

play03:55

ahead and commit that with the bad

play03:57

commit message so now if I do a get log

play04:00

you can see that we've made the commit

play04:03

but the message is wrong so uh how do we

play04:07

modify this message without doing

play04:09

another commit well to do that uh we can

play04:12

just do another commit with the-- amend

play04:15

option so if I do a get commit D- amend

play04:21

then I can type in dasm for my message

play04:24

and I'm just going to say completed

play04:27

subtract function

play04:30

so now if I do a get log here you can

play04:33

see that uh we haven't added any more

play04:35

commits we still just have the two

play04:37

commits but we have changed this uh

play04:40

commit message on our last commit which

play04:43

is what we wanted now I do need to point

play04:45

out that this unique hash here is

play04:48

different than it was before we changed

play04:50

the commit message now that's because

play04:53

the commit message is part of the commit

play04:56

and changing it will also change the

play04:58

hash and when the hash changes that

play05:01

means that it changed the git history um

play05:04

so we only want to ever change the git

play05:06

history when we are the only ones who

play05:08

have had access to the changes that

play05:10

we've made um so if we've pushed our

play05:13

changes for other people to see and pull

play05:15

from then we do not want to change the

play05:18

get history because it can cause

play05:19

problems with their repositories so

play05:22

later in this video we're going to look

play05:23

at some ways in which we can make

play05:25

Corrections without changing the get

play05:27

history but as long as we're the only

play05:29

ones who have seen our changes then

play05:31

changing the history isn't necessarily a

play05:34

bad thing and it also makes for cleaner

play05:36

commits um so in the case of a bad

play05:39

commit message this is likely something

play05:41

that you'll notice right away and can

play05:43

fix it right on the spot like we just

play05:45

did now kind of a similar scenario is

play05:49

that instead of accidentally committing

play05:50

the wrong message uh let's say that we

play05:52

accidentally left off a file that we

play05:55

meant to commit so in this example let's

play05:57

say that I meant to create a get ignore

play06:00

file and I wanted to add it to that last

play06:02

commit so I'm going to create a do get

play06:05

ignore file here and now let me go ahead

play06:08

and run a get status and also let me add

play06:13

that to the staging area so I'll do the

play06:17

get add. GE ignore so now it's in our

play06:19

staging area but we don't want to make a

play06:21

new commit Let's uh so for this example

play06:24

we're saying that we wanted that to be a

play06:26

part of the last commit so again we're

play06:29

just going to do the get commit D- amend

play06:33

like we did before but this time it's

play06:35

going to do it with a file so if I hit

play06:37

enter here now it's going to bring up

play06:39

this interactive editor where we can

play06:41

change the get commit message if we want

play06:44

now notice here that this is the same

play06:46

get commit message uh from our previous

play06:49

commit now we don't want to change the

play06:51

message in this case so I'm just going

play06:53

to uh save and close out so now I'm

play06:56

going to do a get log and you can see

play06:58

that we still just have the two commits

play07:01

that we had before now if I do a g log

play07:06

D- stat um this will show us the files

play07:10

that were changed within the commit and

play07:12

you can see here that the get ignore

play07:15

file is now part of the last commit

play07:18

where we did the uh completed the subtra

play07:21

subtract function now once again just to

play07:23

point out uh this unique hash is now

play07:27

different so we once again changed to

play07:29

get history so just one more time uh

play07:32

just to remind you only do this type of

play07:34

thing if you haven't pushed your changes

play07:36

yet to other people so now if I do a get

play07:40

log and then I do a get Branch um so now

play07:45

we see our subtract Fe feature branch

play07:47

and now uh just say that it hits us all

play07:49

of a sudden that we've been making all

play07:51

of these commits to the master Branch by

play07:54

accident uh really we've been meaning to

play07:57

make these commits to our subtract

play07:59

teacher Branch now this is a fairly

play08:01

common mistake to commit to the wrong

play08:03

Branch because when you switch back and

play08:05

forth sometimes you just forget exactly

play08:07

where you are and um end up committing

play08:10

to uh the wrong Branch by accident so

play08:13

how do we move this commit uh right here

play08:16

this 1B 818 how do we move that commit

play08:20

to our feature Branch now to be clear

play08:22

here exactly what we want to do is our

play08:25

goal is to move this commit to our

play08:28

subtract feed teach branch and then we

play08:31

want to return our Master Branch back to

play08:33

the state of only having the single

play08:36

initial commit so the way that we're

play08:38

going to do this is we're going to use

play08:40

get cherry pick and cherry pick creates

play08:43

a new commit based off of our original

play08:46

so in this case our original is going to

play08:48

be uh this top commit here now cherry

play08:51

pick only creates a new commit based off

play08:54

of that commit it doesn't delete the

play08:57

original after it cherry-picks it so let

play08:59

me clear my screen here and so the

play09:02

process that you go through to do a get

play09:03

cherry pick is first you want to do a

play09:06

get log uh because we want to grab the

play09:10

the hash of the commit that we want to

play09:11

cherry pick um so we don't have to grab

play09:14

the entire hash usually six or seven

play09:16

characters is fine uh so I'm just going

play09:18

to copy this here and now I want to do a

play09:23

get checkout on my subtract feature

play09:26

Branch so now I'm going to switch to my

play09:28

subtract feature branch and now from

play09:31

that Branch if I do a get log you can

play09:34

see that we only have the initial commit

play09:37

now I'm going to do a get cherry pick

play09:40

and I'm going to paste in the hash that

play09:43

I had copied from the master Branch so

play09:46

now if I hit enter there and now I do a

play09:49

get log now you can see that we brought

play09:53

that commit for the complete subtract

play09:55

function we brought that commit over to

play09:57

our subtract feature branch so now we

play09:59

have that commit on our feature Branch

play10:01

but it still exists on our Master Branch

play10:03

because cherry pick doesn't delete

play10:05

commits um but we don't want that commit

play10:08

on our Master Branch we never meant to

play10:10

commit it there to begin with so how do

play10:12

we get rid of it so to do this we're

play10:14

going to use a git reset so I'm going to

play10:17

do a git checkout on master and now I'm

play10:22

going to do a get log so that we can see

play10:25

the commits that we have now there are

play10:27

three different types of resets that we

play10:29

should know about uh these are get reset

play10:33

soft get reset mixed which is the

play10:36

default and get reset hard um so first

play10:40

let's run through all of these so first

play10:42

let's run get reset soft to try to

play10:45

return uh to our initial commit so I'm

play10:48

going to grab the hash from this initial

play10:50

commit and copy that and now I'm going

play10:53

to do a get reset D- soft and then I'm

play10:58

going to paste in that hash of our

play10:59

initial commit so now if I do a get log

play11:04

you can see that we no longer have that

play11:06

second commit which is good that's what

play11:08

we wanted um but let's also look at our

play11:12

get status now you can see that we have

play11:14

some files in our staging area so soft

play11:17

will reset us back to the commit that we

play11:20

specified but it will keep our changes

play11:22

that we've made in the staging directory

play11:25

uh so we didn't lose any of our work so

play11:27

in this case our work refer to the

play11:30

addition of theg ignore file and the

play11:33

modifications that we made to the cal.

play11:35

py file so that is a soft reset uh let's

play11:39

look at a mixed reset which is the

play11:42

default um so again I'm going to grab

play11:45

the hash of the initial commit and this

play11:47

time let's do a get reset and I'm just

play11:50

going to paste in the hash because

play11:52

without any additional options it will

play11:54

default to mixed so let's go ahead and

play11:56

run that and now let's look at our get

play12:01

log we only have the one commit which is

play12:03

good but now if I do a get status you

play12:06

can see just like get reset soft it kept

play12:09

the changes um however now these changes

play12:12

aren't in the staging area now they're

play12:14

in the working directory so it's very

play12:16

similar to get reset soft but the files

play12:19

are either put in the staging or working

play12:22

directory based on which one you specify

play12:25

but in this example we we really don't

play12:27

want our changes um we really just want

play12:29

to go back to that initial commit and

play12:32

have everything the way that they were

play12:34

um so again let's copy the initial

play12:38

commit hash and this time we're going to

play12:40

try a get reset hard so get reset hard

play12:44

is going to make all of our tracked

play12:47

files match the state that they were in

play12:50

uh at the hash that we specify and you

play12:52

have to be careful with get reset hard

play12:54

because it will get rid of your changes

play12:56

but in this example that's what we

play12:58

that's what we want so I'm going to do a

play13:00

get reset D- hard and paste in that hash

play13:05

and now if I do a get log you can see

play13:08

that we have the one commit and now I'm

play13:11

going to run get status so notice that

play13:14

the modifications that we made to the

play13:16

calc. py file are gone um but that theg

play13:20

ignore file is still there now if you

play13:22

remember I said that the git reset hard

play13:25

reverts all of the tracked files back to

play13:27

the state that they were but but it

play13:29

leaves any untracked files alone so we

play13:33

can uh see here that we still have some

play13:35

leftovers with the get reset hard uh but

play13:38

luckily getting rid of untracked files

play13:40

is extremely simple so to get rid of

play13:42

untracked files we can just use get

play13:45

clean so if I do a get clean and then

play13:48

I'm going to specify the D and the F

play13:51

option so the D option gets rid of any

play13:54

untracked directories and the F gets rid

play13:56

of any untracked files so if I run that

play13:59

now if I do a get status you can see

play14:03

that our working directory is clean and

play14:05

thatg ignore file is gone now that clean

play14:08

command is a good command to know U

play14:10

because it actually comes in handy more

play14:12

often than You' think um for example one

play14:15

time I was accidentally uh I

play14:17

accidentally unzipped a file within my

play14:19

git repository and it created just a ton

play14:22

of untracked files and subdirectories

play14:25

and I could have gone through and tried

play14:26

to pick all those out manually and

play14:28

delete all of them but the easiest way

play14:30

to remove all those untracked files is

play14:32

just with this single get clean command

play14:36

okay so we learned that the get reset

play14:39

hard totally totally deletes all of our

play14:42

changes uh but what if you accidentally

play14:44

ran that and it turns out that you did

play14:47

need some of those changes so let's

play14:48

pretend for the sake of this walk

play14:50

through that we don't have uh those

play14:52

changes still saved to the subtract

play14:54

feature branch and we thought that the

play14:56

reset deleted all of the changes that

play14:58

we've made

play14:59

so if you did run a get reset hard on

play15:02

some changes that you really needed then

play15:05

are you completely out of luck well not

play15:07

entirely so you could be out of luck if

play15:10

a lot of uh time has gone by since you

play15:13

ran that get reset um I don't know the

play15:16

exact amount of time but I think get

play15:18

garbage collects uh those commits after

play15:22

30 days or so um but get ref log is what

play15:26

we're going to want to use here so get

play15:28

ref log shows commits in the order of

play15:31

when you last reference them so let me

play15:34

run a get ref log here and you can see

play15:38

that it kind of shows us exactly a

play15:41

walkthrough of exactly what we've been

play15:43

doing so you can see here at the bottom

play15:45

is where I made my initial commit and

play15:47

then we committed our complete multiply

play15:51

multiply function which was wrong and

play15:53

then you can even see where you amended

play15:55

commits so this was an amended commit

play15:57

where we changed our message and another

play15:59

amend that's where we added the do get

play16:01

ignore file and it shows us that we

play16:04

checked out and moved from Master to

play16:06

subtract feature and then did our cherry

play16:08

pick and our checkout and then our reset

play16:11

now if you'd accidentally got rid of

play16:13

files that you didn't mean to get rid of

play16:15

then so for this example I'm going to

play16:17

grab the hash before the reset and copy

play16:20

that and I'm going to do a get checkout

play16:24

of that hash and now from here I'm going

play16:27

to run a get log so you can see when I

play16:30

run a get log from this hash uh that we

play16:33

have our changes back this is the commit

play16:36

that we reset and got rid of all those

play16:39

changes now right now we are in a

play16:41

detached head State now I'm not going to

play16:43

go fully into what this means uh but

play16:47

basically it means that we aren't on a

play16:49

branch uh and where we currently are it

play16:52

will be trashed at some point in the

play16:54

future uh so to save these changes we

play16:57

need to make a branch from it so from

play16:59

here I'm going to do a get branch and

play17:02

I'm just going to call this backup and

play17:04

now if I run get Branch command to look

play17:07

at all my branches you can see that

play17:09

currently we're in a detached head State

play17:12

and that's we'll get garbage collected

play17:14

at some point and now we have our Master

play17:16

Branch our subtract feature Branch we

play17:18

also have this backup Branch so now let

play17:21

me check out the master branch and now

play17:26

from the master Branch if I do a get

play17:29

Branch you can see that we still have

play17:31

our backup Branch so those changes that

play17:34

we thought that we'd lost uh now we have

play17:37

those in this Branch here so just to

play17:39

confirm that we still have those changes

play17:41

uh let's do a get checkout of that

play17:44

backup branch and now from here let's do

play17:47

a get log and now you can see that we

play17:49

have those changes that we thought that

play17:51

we'd lost so the get ref log can really

play17:55

be a lifesaver if you thought that you

play17:58

had lost some critical files that you

play18:01

really didn't mean to delete or that if

play18:03

you accidentally did a reset on

play18:05

something um The Ref log can really help

play18:08

you out if you know how to use it okay

play18:11

so in these examples so far I mentioned

play18:13

that you shouldn't change the get

play18:15

history if other people have pulled

play18:17

those changes already uh well what if

play18:20

you are in a situation where you really

play18:22

need to undo some commits but other

play18:25

people have already pulled those changes

play18:27

so in a situation like that you're going

play18:29

to want to use get revert uh revert

play18:32

creates new commits to reverse the

play18:35

effect of some earlier commits uh so

play18:38

this way you don't rewrite any history

play18:40

just to clarify that it's not going to

play18:42

modify or delete our existing commits

play18:45

that we've made it's going to create new

play18:48

commits on top of those uh that

play18:49

completely undo all of the changes so

play18:52

that our history remains intact so just

play18:55

to show an example of this I'm going to

play18:57

run a git log and you you can see that

play18:59

we have our two commits here so now what

play19:02

if this completed subtract function uh I

play19:05

wanted to undo that but other people

play19:07

have already checked that out well uh I

play19:10

can just copy the hash here and I'm

play19:12

going to do a get revert and then paste

play19:16

in that hash and now it's going to come

play19:18

up here with a message and I'm just

play19:20

going to save that and exit out of that

play19:22

and now if I close out of there and do a

play19:25

get log you can see that we now have

play19:29

three commits and these two bottom

play19:32

commits are completely untouched both of

play19:35

their hashes are still the same as they

play19:37

were before the revert now we have this

play19:39

additional commit that says that it

play19:42

reverted the complete subtract function

play19:45

so if I go over here to my cal. py file

play19:48

and reload it you can see that it's uh

play19:50

it undid that return statement that we

play19:52

typed in um now one nice little trick

play19:55

with uh the git diff command is that if

play19:59

I do a if I do a git log here and then I

play20:02

do a git diff of this

play20:07

hash and this hash here it's going to

play20:11

show me the difference between the

play20:13

complete subtract function commit and

play20:16

then the uh reverted version of that so

play20:19

you can see that whenever I compare

play20:21

these two commits uh all it did was go

play20:23

in here and it complet it saw all the

play20:26

changes that uh we made in our

play20:29

complete subtract function commit and

play20:31

then it just redid or undid those so for

play20:34

that commit we took out the past line

play20:37

and added in the return xus Y Line and

play20:41

in the revert it took out the return xus

play20:44

Y and added in the pass so it just did

play20:46

the complete opposite so now whenever

play20:48

you push these changes and somebody else

play20:51

um pulls those down uh their history is

play20:54

not going to be corrupted because all of

play20:57

this history is the same and all they're

play20:59

going to get is these are these new

play21:01

commits that undid those uh previous

play21:03

commits well that about does it for this

play21:06

video I hope that these uh G tips on how

play21:09

to undo some of your mistakes and roll

play21:12

back some previous commits I hope that

play21:14

this was useful for you all um if you do

play21:16

have any questions just ask in the

play21:18

comment section below uh be sure to

play21:20

subscribe for future get videos and

play21:22

thank you for watching

Rate This

5.0 / 5 (0 votes)

関連タグ
Git TutorialVersion ControlUndo CommitsReset CommandsCherry PickGit RevertRepository ManagementCode RevisionGit HistoryDeveloper Tips
英語で要約が必要ですか?