Search code examples
gitgit-branchgit-commitgit-stashgit-reset

How do I save work in progress without using git-stash?


I have a git directory with this situation:

ProgSoul@PROGSOUL-LENOVO:~/esercizio3_2$ git status
Sul branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   A

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   A

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        B

In this situation I have:

  • there is a file named A in the index with a string "AA" inside it
  • I modified the file A with "AAA" inside it but I didn't add this change on the stage. So in the working directory I have A with a string "AAA" inside it
  • an untracked empty file B

My teacher wants me to temporarily suspend my work, create a BUGFIX file, commit it and restore my previous situation.

I achieved it through these commands:

git stash --include-untracked
touch BUGFIX
git add BUGFIX
git commit -m "Aggiunto file BUGFIX"
git stash pop --index

With these commands I saved my initial situation and restored it once my fix has been commited. My teacher also asked me to reach this goal without using git-stash.

I followed the help in the stash documentation:

git checkout -b WIP
git commit -a -m "WIP"
git checkout master
touch BUGFIX
git add BUGFIX
git commit -a -m "BUGFIX"
git checkout WIP
git reset --soft HEAD^

With git reset --soft I restored the index but the changes not staged for commit have been lost.

With git reset --mixed I restored the changes not staged for commit but the index has been lost.

How can I restore the same initial situation once I commit the fix without using git stash?


Solution

  • Thanks to every answer I received. I achieved two possible solutions:

    git worktree add -b BUGFIX ../bugfix_temp master
    pushd ../bugfix_temp
    touch BUGFIX
    git add BUGFIX
    git commit -a -m "emergency BUGFIX"
    popd
    rm -rf ../bugfix_temp
    git worktree prune
    

    OR

    touch BUGFIX
    git add BUGFIX
    git commit --only BUGFIX -m "emergency BUGFIX"