0

If I switch over to a new branch and commit the files (and push to GitHub as a backup), I found that once I switch back to the old branch, the files all became unmodified -- as if they were never modified since the last commit.

In this case, can I make the files modified like before?

I can

git rebase tmp-branch

and get those changes, but this action also apply the commit to my current branch. Is there a way to not make the commit happen, but just let the files be modified and remain uncommitted?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 2
    Is `stash`ing what you're looking for? https://git-scm.com/docs/git-stash – Goose Jun 23 '17 at 21:05
  • can stash solve it? Is it first by stashing, switch to new branch, apply stash, commit, and switch back to old branch, and apply stash again? But what if I didn't stash and I already committed in the other branch? – nonopolarity Jun 23 '17 at 21:06
  • I'm fairly certain, yes. – Goose Jun 23 '17 at 21:06

3 Answers3

2

Edit: Apparently the Git folks have thought of this already.
You can pass the -n flag to cherry-pick to apply the changes of another commit, and stop before creating a new one.

git checkout old-branch       # Go back to the old branch, files are unmodified

git cherry-pick -n tmp-branch # Apply the changes from tmp-branch's tip
Quentin
  • 62,093
  • 7
  • 131
  • 191
0

I think you're looking for the stash family of git commands

https://git-scm.com/docs/git-stash

It allows you to store changes without committing them, switch to other branches, and the "pop" the stash when you return.

Here's some basic examples of it's use

http://gitready.com/beginner/2009/01/10/stashing-your-changes.html

From that page

Add your changes to the index using

git add .

Or add individual files to the index, your pick. Stash your changes away with:

git stash

And boom! You’re back to your original working state. Got that bug fixed? Bring your work back with:

git stash apply

You can also do multiple layers of stashes, so make sure to use

git stash list

To check out all of your current ones. If you need to apply a stash from deeper in the stack, that’s easy too. Here’s how to apply the second stash you’ve got:

git stash apply stash@{1}

You can also easily apply the top stash on the stack by using:

git stash pop

A note with this command, it deletes that stash for good, while apply does not. You can manually delete stashes with:

git stash drop <id>

Or delete all of the stored stashes with:

git stash clear
Goose
  • 4,764
  • 5
  • 45
  • 84
  • could you maybe list the steps for this particular situation? And what if I didn't do stash, then is it not possible to achieve what I want in the original question? – nonopolarity Jun 23 '17 at 21:08
  • I'll try, I mostly use a GUI that supports this. If you didn't do stash, then I think you're changes were overwritten. In my GUI, it prevents you from doing that though so I'm not sure. – Goose Jun 23 '17 at 21:12
  • @太極者無極而生 nevermind, it seems you can recover unstashed changes. https://stackoverflow.com/questions/19003009/git-how-to-recover-stashed-uncommitted-changes – Goose Jun 23 '17 at 21:13
  • isn't the link above about stashed changes that were later on unstashed? – nonopolarity Jun 23 '17 at 21:17
  • @太極者無極而生 You're right, my mistake. I'm not entirely sure if you can recover unstashed changes after changing branches, but I doubt it. – Goose Jun 23 '17 at 21:18
0

Git: Stash unstaged changes

This will stash all modifications that you did not git add:

git stash -k

Note that newly created (and non-added) files will remain in your working directory unless you also use the -u switch.

git stash -k -u

Also, your working directory must be clean (i.e. all changes need to be added) when you git stash pop later on.

Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24