Reputation: 697
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:
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)?
Upvotes: 7
Views: 1551
Reputation: 6046
The answer of @phd is correct and probably the right one for your needs. Just consider that it is a partial backup, including your working directory only. What I am saying is that doing that you are losing the changes of the index. Instead, to store the changes of the index too, you would need to create a further commit. So, starting from the master:
git checkout -b my_backup
git commit -m "first the index"
git add .
git commit -m "and now the working dir"
git checkout --detach HEAD
git reset HEAD~1
git reset --soft master
git checkout master
It is quite long but you could move it inside a script (perhaps with parameters for the backup branch name and the current branch) and it is easy to understand too.
git stash
does a similar thing, but the script would have been a little more complex and I am not sure the stash
logic won't be different in the future. Currently, a full stash creates 3 commits: one with the index, one with untracked changes and another one which is a merge commit of the previous two and of the HEAD
itself.
Upvotes: 2
Reputation: 94676
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.
Upvotes: 7
Reputation: 104
You are on the right track. In order to save your changes to remote, you'd have to commit to a non-master branch and push (e.g. backup_2021-02-13)
What most people do is do development in a branch until you are completely done, then merge your changes to Master (ff, squashed) like you did. This development branch is essentially day-to-day "shelved" changes.
To see the pending changes in your branch you just "git diff master" to see the changes.
Upvotes: 1