user2331417
user2331417

Reputation: 19

Save changes temporarily in git without committing

Can anyone suggest to save my work on git temporarily as I have lost my 15 days work because of machine failure. Git stash is not useful as it saves data locally and committing for 30 days and then merge is also not good idea as when we want to move data to production there will be 30-40 commits. Is there any way to save work temporarily and access from other system.

Upvotes: 0

Views: 2461

Answers (5)

ccpizza
ccpizza

Reputation: 31756

Sometimes people might want to avoid committing for whatever reason especially when working in a team, because you want to have a nice git history, or you don't want to logically split your work into multiple commits.

Probably the least elegant solution is to use git patch and then use whatever other channels to transfer the patch file to the target machine.

  1. First, make sure you stage all the files you want to include. git add -u will not add new, untracked files. Using git add . from the project root is usually a bad idea (❗️) so better stage the untracked files explicitly one by one with git add newfile or use the tools provided by your IDE.

  2. Create the patch with:

git diff --patch --staged > wip.patch
  1. On the other machine (from the repo's root) import the patch using:
git apply wip.patch

ℹ️ Intellij IDE's provide an easy visual way to both create and apply patches via Git > Patch.... For vscode there an extension that attempts to do the same.

Upvotes: 0

Catherine
Catherine

Reputation: 229

You can create a temporary branch for this purpose. You can commit and push it to the remote, to access it from different systems. As you said, if committing for 30 days, there will be 30+ commits.

We can squash them to one single commit using the git squash command.

After that, you can merge this commit to your production also:

git rebase --interactive HEAD~[30]

This would combine your last 30 commits into one.

Upvotes: 0

Jörg W Mittag
Jörg W Mittag

Reputation: 369536

Committing is the way to save changes in Git. Since you dis-allow committing, there is really nothing else that can be done, at least not within Git.

Of course, there are lots of things that can be done outside Git, backups being one of them. As the old saying goes: all important data has backups, and if you don't have backups, then your data wasn't that important to begin with.

Upvotes: 0

Saeed Alizadeh
Saeed Alizadeh

Reputation: 1467

following trick might help:

1-you can create a diff file: e.g by command: git diff > temp.diff

2-commit it and push to server

3-in another machine pull and apply the diff by command : git apply temp.diff

4-loop through until finish your task

5-at the end you can delete the temp.diff file and commit real changes

Upvotes: 0

Yomna Hesham
Yomna Hesham

Reputation: 472

Make a new branch for these temporary changes with a significant name showing that this branch is like a draft. When you return to continue your work, merge it to your original working branch and destroy the draft one.

Upvotes: 2

Related Questions