Search code examples
gitbackup

Git: Backup uncommitted changes to remote repository


I want to backup local, uncommitted changes to the remote repository in order to backup the changes (in case the local hard drive breaks etc.)

(In TFSC, I would have just "shelved" the changes into a shelveset (which "lives" on the TF server))

What I tried:

  • I created a new branch "backup_2021-02-13" and switched to it
  • I committed the changes.
  • I pushed it to the remote repo.

At this point of time, my changes have been saved on the remote repository (meaning that I could recover the information in case of a broken local hard drive)

But now I have the following problem: Both in the "master" branch as well as in the "backup_2021-02-13" branch, I cannot see the previously pending changes: I can't see them on the backup branch because they have already been committed (yes, the changes are there, but already committed). I can't see them on the master branch because there, these changes do not exist at all.

But I want to continue working as if the backup process never happened (with all uncommitted changes still being uncommitted).

So, what I did was, I merged the changes from the backup branch to the master branch (no-commit, ff-only, squash). This way, I have my uncommitted changes in the master branch as uncommitted changes, as it was before any backup work.

But I doubt that this is the normal way of doing this.

So, what is the correct (and easiest) way to backup local uncommitted changes to a remote repository, but at the same time leaving the current (master) branch as it is (with all the uncommitted changes still being uncommitted)?


Solution

  • Your process is ok. After switching to master you just need to restore changes committed in the backup branch:

    git checkout master
    git checkout backup_2021-02-13 -- .
    

    The 2nd command restores all file from branch backup_2021-02-13. Continue working.