Can I save changes to my old commit, obtained via `git checkout`?

advertisements

I am learning Git and I am reading this.

You can use git checkout to view the “Make some import changes to hello.py” commit as follows:

git checkout a1e8fb5

This makes your working directory match the exact state of the a1e8fb5 commit. You can look at files, compile the project, run tests, and even edit files without worrying about losing the current state of the project. Nothing you do in here will be saved in your repository. To continue developing, you need to get back to the “current” state of your project:

git checkout master

But at this case I will lost my current changes in the working directory... What if my experiment was successfull and I want to save the result also? I.e. can I save the changes of my some old commit, gotten via git checkout?


git checkout <commit-id> -b foo

Explanation

foo : This can be the name of a backup branch you would want to place your checkout commmit.

commit-id : Well , this is the commit you would like to checkout.

Result

Your checkout changes will be stored in a foo now that is local to you , so no worries. If you want to persist changes you made you'll have to use git commit; these commits will stay on foo from which you can reference them at any time.