CompSci 308 Spring 2024 |
Advanced Software Design and Implementation |
No matter how experienced you become with GIT, at some point you will make a mistake an commit
or push
something you do not want in the repository. Certainly, making smaller commits and paying attention when you commit will always be the first and best defense against mistakes (as well as never using git add .
).
We recommend using these two GIT commands before all of your commits to ensure you know exactly what is being changed:
git diff
: show the differences between the current, edited, version of a file and the latest version in the repositorygit status
: show the status of all files in the GIT folder (untracked, tracked and changed, or added for this commit)However, once you have committed a mistake (pun intended), it is useful to be able to perform an "undo" — sometimes it will be possible to simply update the last action, but sometimes it may require creating a new commit that over writes previous changes. As long as the mistake is fixed, the history you leave is less of a concern for this course. Some GIT commands that will be useful include:
git reset
: rewinds your repository’s history all the way back to the specified commit, as if those commits never happened!git revert
: create a new commit that is the opposite of a given commitgit stash
:
save your modified tracked files on a stack of unfinished changes that you can reapply at any timegit rebase
: this is an advanced command, with many options, that attempts to smartly combine commits in a variety of ways. It is very popular (so you will see many articles about it) and very easy to mess up (so we do not recommend using it until you are very comfortable with GIT)This humorous site also has more details about each command, including different options for each.
Each person should practice at least the following scenarios on your project repository (where you have some interesting commits to work with) to practice making and fixing GIT mistakes:
stash
them, then make some more changes and commit
them, then retrieve your stashed work and commit
that.gitignore
(note the leading period) from your project, then run the (terrible) command git add .
to see what was added using git status
add
by using reset --hard
to get the repository back to its latest version (including the .gitignore
file)