Skip to content

Commit

Permalink
Merge pull request #904 from lexnederbragt/patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
kekoziar authored May 3, 2023
2 parents fc7f1bd + 4612235 commit 895618b
Showing 1 changed file with 66 additions and 2 deletions.
68 changes: 66 additions & 2 deletions episodes/05-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,72 @@ moving backward and forward in time becomes much easier.
>
> `git checkout` can be used to restore a previous commit when unstaged changes have
> been made, but will it also work for changes that have been staged but not committed?
> Make a change to `mars.txt`, add that change, and use `git checkout` to see if
> you can remove your change.
> Make a change to `mars.txt`, add that change using `git add`,
> then use `git checkout` to see if you can remove your change.
> > ## Solution
> > After adding a change, `git checkout` can not be used directly.
> > Let's look at the output of `git status`:
> > ~~~
> > On branch main
> > Changes to be committed:
> > (use "git reset HEAD <file>..." to unstage)
> >
> > modified: mars.txt
> >
> > ~~~
> > {: .output}
> >
> > Note that if you don't have the same output
> > you may either have forgotten to change the file,
> > or you have added it *and* committed it.
> >
> > Using the command `git checkout -- mars.txt` now does not give an error,
> > but it does not restore the file either.
> > Git helpfully tells us that we need to use `git reset` first
> > to unstage the file:
> > ~~~
> > $ git reset HEAD mars.txt
> > ~~~
> > {: .language-bash}
> >
> > ~~~
> > Unstaged changes after reset:
> > M mars.txt
> > ~~~
> > {: .output}
> >
> > Now, `git status` gives us:
> > ~~~
> > $ git status
> > ~~~
> > {: .language-bash}
> >
> > ~~~
> > On branch main
> > Changes not staged for commit:
> > (use "git add <file>..." to update what will be committed)
> > (use "git checkout -- <file>..." to discard changes in working directory)
> >
> > modified: mars.txt
> >
> > no changes added to commit (use "git add" and/or "git commit -a")
> > ~~~
> > {: .output}
> >
> > This means we can now use `git checkout` to restore the file
> > to the previous commit:
> > ~~~
> > $ git checkout -- mars.txt
> > $ git status
> > ~~~
> > {: .language-bash}
> >
> > ~~~
> > On branch main
> > nothing to commit, working tree clean
> > ~~~
> > {: .output}
> {: .solution}
{: .challenge}
> ## Explore and Summarize Histories
Expand Down

0 comments on commit 895618b

Please sign in to comment.