Home > Net >  git: get working directory staged and non staged back after committing to new branch
git: get working directory staged and non staged back after committing to new branch

Time:06-02

While on a branch b1 with staged and unstaged files, I created a new branch (backup-changes) and committed those.

// Create the backup branch from the branch you are on
$ git checkout -b backup-changes

// make sure it is created and you are on the backup branch
$ git branch -a

// check status and add and commit to the backup branch just created 
$ git status
$ git add .
$ git commit

// do a dry run to see things what are things going to be
$ git push -u origin backup-changes --dry-run

// push to remote
$ git push -u origin backup-changes

I did not want to stash it since I wanted a backup on a remote server.

Now, I switch back to the branch b1, and want to get back all the staged and unstaged files as they were before I switched to "backup-changes" and committed? Is that possible?

CodePudding user response:

I can't understand the usefulness of a backup in the Git universe. But there are two possible ways:

First possibility is git cherry-pick <commit-id> in the destination branch (branch b1). Do this only if you understand the behavior of cherry-pick!


The seconde way is to git reset the last commit in the branch backup-changes with:

git reset --soft HEAD~1

And then save the reseted files with git stash:

git stash save "get back all the staged and unstaged files"

Now you can change the branch back:

git checkout b1

And then make the stashed files effective:

git stash pop

This will removes the stash, if you want to keep the stash in the stash-list, do only git stash apply and the stash is farther saved and showed as git stash list. For more information, see the git stash documentation

  •  Tags:  
  • git
  • Related