Electric Lazer Eyez
If you’d like to experiment with a previous commit, you can get to it with laser focused precision.
Rather than doing a
git revert or the more destructive (and dangerous)
git reset, you can do a read-only branch directly to a specific place in your git history.
Finding the commit you need.
There’s 2 ways you can view your previous commits, by typing
git log in your terminal, or by viewing your commits in your GitHub repository.
It’s usually a really long string. For example:
Check that sh*t out.
Copy the commit you’d like. In GitHub, you can click the little “copy” icon to the left of the number, or just copy it directly from your git log. (Side note: if you’re viewing a long git log in terminal, to exit the view just type the letter
Let’s assume you’re in the master branch. When you type
git checkout 19ad132a79fe0b75ba0b99432e0f1e8e06f9a000, it will instantly create a new read-only branch exactly at the commit you’ve specified. You can now experiment in this new branch as much as you’d like – install new gems, etc – and generally it will not do anything destructive to your master branch.
Keep that sh*t.
If you want to keep that read-only branch into a new branch, just type
git checkout -b new_branch_name where “new_branch_name” is a new name of your choosing.
Leave that sh*t behind.
After experimenting, if you don’t want to keep your read-only experiment you can go back to your master branch by typing
git checkout master. The read-only version of the branch you created now magically vanishes.
If you get an error that does not allow you to go back to the master, you may need to delete the read-only branch you created. Warning: make sure you’re deleting the correct branch as this completely destroys it. To delete the read only branch,
git branch -d the_branch_name. If that doesn’t work, try
git branch -D the_branch_name.