Reputation: 518
okay so lets say I have two branches
1) new-branch
2) master
I have made some changes to new-branch but did not commit them yet, I want to switch to master now, but my workspace is not clean as I have made some changes since the last commit in new-branch. I don't wanna make a commit and I don't wanna carry those changes to master branch, I just wanna have a quick look at the master branch and come back to where I left in new-branch, Is it possible? If yes how to do it?
Upvotes: 2
Views: 3207
Reputation: 488183
I recommend considering using git worktree add
, at least if your Git is at least version 2.15. To do this, you might run this command from the top level of your work-tree:
git worktree add ../project.master master
which will create a whole separate work-tree in ../project.master
. You can then look at your master
branch in this separate work-tree, any time you like. You can even do work in master
, if that's a thing you would normally do (many groups and organizations suggest not doing this at all though).
There are really two or three questions here, I think. In particular I think you've gone into an XY problem here: you are currently working in your new-branch
, but you would like to view the files as seen in your master
branch. So you then thought: I know how to view files and went from that to how can I save everything without making a commit? and have asked the question you asked, rather than the original, actual problem: How can I view the files I would have, if I checked out some other branch such as master
?
The git stash
answer is sort of the answer to the question you actually asked. See any of the answers by Noël Kra, customcommander, and/or Yusef Maali. That is, you can run git stash
, which saves your as-yet unsaved work somewhere, then erases your as-yet unsaved work (which is actually saved now) and enables you to switch branches.
Technically, all git stash
does is make some commits (usually two; sometimes three, if you ask for a third commit) that are not "on" any branch. So while that's sort of the answer to the question you asked, it also sort of isn't, because you asked Can I switch branch without making commit? and git stash
makes commits.
Sometimes, you can switch branches without making a commit even when you have unsaved changes. That is, git switch master
or git checkout master
may actually just work. The drawback here is that it brings the uncommitted work along with the change in branches. It's also not clear to newbies to Git as to why Git behaves this way. For (much) more about that—eventually; don't look yet—see Checkout another branch when there are uncommitted changes on the current branch.
But you don't really need to switch branches. You really want to look at files in the snapshot at the tip of master
. To get the entire set of such files, you can use git worktree
. The git worktree
sub-command, first introduced in Git 2.5, allows you to have multiple different git checkout
-s at the same time, as long as each one is on a different branch and in a different working tree (directory / folder—use whichever term you like) in your file system.
If you only want to view one file from master
, consider using:
git show master:path/to/file.ext
which will just display the contents of that file, as seen in the commit that is the tip of branch master
. You only need a whole checkout, as in git worktree add
, if you need to look at lots of files at the same time, in a fancy viewer for instance.
Upvotes: 1
Reputation: 40
To temporary save your changes use
git stash
To retrieve them again after switching back use
git stash pop
You can also apply the changes but keep the changes in your stash like follows:
git stash apply
With this command you could apply your changes on multiple branches.
Source: https://www.git-scm.com/docs/git-stash
Upvotes: 0
Reputation: 18901
Here's one possible workflow (YMMV)
You're in new-branch
, made some changes but want to quickly go back to master
git stash
git checkout master
master
new-branch
; run git checkout new-branch
git stash pop
While git stash
is definitely something to consider, you should not be worry of making commits that you can trash away if you need to:
new-branch
run git commit -a -m TMP
master
run git checkout master
git checkout new-branch
git commit -a --amend
git reset HEAD^
Upvotes: 5
Reputation: 2431
You need to use
git stash
Stashing takes the dirty state of your working directory – that is, your modified tracked files and staged changes – and saves it on a stack of unfinished changes that you can reapply at any time.
Take a look at the official documentation with examples:
https://git-scm.com/book/it/v2/Git-Tools-Stashing-and-Cleaning
A common workflow:
git checkout master
... do some work ...
git stash <- save all the uncommitted works
git checkout new-branch
... do some work on new-branch ...
git commit <- commit on new-branch
git checkout master
git stash pop <- reapply all the changes you previously have on master
Upvotes: 3