Reputation: 87
Summary
I am working on a script. As a part of this script, I need to programmatically
I'm imagine each of these steps would involve some set of git commands. I ideally want there to be no side affects from these three operations -- i.e., step 3 should restore all local changes and the repository is in the same state.
What I've Tried So Far
This almost works:
git stash --include-untracked
git name-rev --name-only HEAD # Save to <HEAD_REV>
git checkout <NEW_REV>
# Do stuff
git checkout <HEAD_REV>
git stash pop
This mostly works, but I'm not sure if there are some edge states where it will fail to restore the repository state. Can anyone more experienced in git tell me if there's a gold-standard way to do this?
Upvotes: 1
Views: 43
Reputation: 52226
One way to get a throwaway checkout is to use git worktree
:
git worktree add --detach ../tmpworktree <new_rev>
cd ../tmpworktree
# work work ...
cd ../repo
git worktree remove ../tmpworktree
Upvotes: 3
Reputation: 30317
1 you should consider running git clean -f
after doing stuff.
2 I learned recently that you can run git checkout -
to go back to where you were before (very much like git checkout HEAD@{1}
but more powerful because it will checkout a branch if that is where you were before). Analog mechanism to cd -
. Gotta love it!!! <3
Upvotes: 2