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.