• Skip to primary navigation
  • Skip to main content
  • Skip to footer
Code The Dream School
Code the Dream Labs Logo

Code The Dream School

Main hub for class materials for Code the Dream’s classes

  • Code the Dream Home

Search Code The Dream School

More Git: Experimenting, Recovering from Mistakes

Here are some suggestions on how to make git work better for you.

Experimenting

Suppose you are going to try a big change to the code, but you are not sure it will work or that it is what you want. Or suppose you are making temporary changes to the code that you know you won’t want to keep. One way to handle this is to create a temporary branch from your current working branch. Then make changes to the temporary branch. If you decide to keep those changes, you commit them to the temporary branch. Then you check out the working branch, and you do:

git merge <temporary>

where <temporary> is the name of your temporary branch. If you don’t want to keep the changes, you checkout the working branch and delete the temporary branch.

Recovering from Mistakes

Students sometimes make mistakes with their assignments and projects, get confused, and decide they need to start over from scratch. This costs a lot of time. You do not need to start over if you understand git.

One great thing about git is that you can always restore files to the state they were in at the time of a previous commit. If you commit whenever your code is in a stable state, you can recover that state. Here are some recovery scenarios:

  1. You have made a change with strange results, and you can’t remember what you did. To see what you changed, use:
git log
(this will show you your commit history) and then do
git diff
or
git diff <commitNumber>

The git log shows the number of each commit. You can use those numbers in various git commands. You just need to use the first 8 characters of the commit number. When git diff is used without parameters, git it shows you what has been changed since the last commit. If you specify a commit number, you can see all the changes since that previous commit.

  1. You have made a change you don’t want to keep, and you have not committed the file.
git checkout -- <filename>

This restores the file to the state it was in the last commit.

  1. You have made changes to several files, and you want to go back to the last commit, discarding all the changes you have made since the last commit. Do:
git reset HEAD --hard
git clean -fd
  1. You have made changes to several files, you have not committed them yet, and you want to save those changes, but you want to restore the project to the state it was in before the last commit. Do:
git stash

You could then get those changes back with git stash apply.

  1. You have changed a file, you don’t want to keep the change, and you have committed the change. For example, you may have accidentally deleted the file and then committed the change. To recover, first use git log to find the commit number where the file was in a good state. Then do:
git checkout <commitNumber> -- <filename>

Where filename is the name of the file you want to restore.

  1. Suppose you want to look at the files as they were for a previous commit. You can do
git checkout <commitNumber>

This will checkout the whole project at a particular commit level — but don’t make changes while you are in this state. You only do this to look at the files, or perhaps to try the program to see if it was working correctly at the time of that commit. Switch back to your working branch to make changes.

  1. You have made and committed a bunch of changes you don’t want to keep, so you want to go back to the state of the branch at a previous commit level. Now, be careful with this one, as it does throw away all the changes since that earlier commit. Do NOT use it if you have already pushed the branch to github and it has been merged into main. Find the commit level you want, and then do:
git reset --hard <commitNumber>
  1. You have messed up your branch, but you have a version of the branch on Github that is in a good state. You may want to save your work just in case there is anything to remember in it. To save the messed up state, add and commit your changes. Then create a new branch. This will save the state. Then checkout the messed up branch. Then do:
git fetch origin
git reset --hard origin/<branchname>
git clean -df

Advanced Topics

There are several other git commands that you will need. They are merge and revert. What makes these commands a little more difficult is that you can get merge conflicts.

The revert command undoes all the changes associated with a particular commit. However, suppose another change has been committed that affects the reverted change. Then you will get a conflict, to be resolved in an editor, which can be tricky, because you have to figure out how to preserve appropriate logic in the code.

The merge change combines all the changes from one branch into another. However, the problem again is that there may be conflicts between the states of the code in the two branches. Again, you have to resolve the conflicts with your editor.

You do have to learn how to resolve merge conflicts, and how to minimize them in the first place, in order to work on a joint project where a team shares code. Code the Dream practicums and the projects developed by Code the Dream interns are team projects. The git process you use is described at this link.

Footer

Copyright © 2025 Code the Dream School | All Rights Reserved | Privacy Policy