93

I have 2 branches, which are not ready to be merged yet, but have some complementary logic, which I'd like to review (before merging)

Can I check out multiple git branches of the same project? Is it possible?

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 1
    You will be able to do so with Git 2.5+ and its new `git checkout --to=` command. See [my answer below](http://stackoverflow.com/a/30186481/6309). – VonC May 12 '15 at 09:13
  • The actual syntax is `git worktree add `. I have updated [my answer below](http://stackoverflow.com/a/30186481/6309). – VonC Dec 30 '15 at 21:05

6 Answers6

89

You can simply copy the repository to a new location (either by literally copying the directory, or using git clone --shared) and check out one branch per location.

You can also use git-worktree for creating multiple working directories from a single instance of a repository.

Otherwise, the primary means for comparing files between branches prior to merging them is git diff.

user229044
  • 232,980
  • 40
  • 330
  • 338
21

With Git 2.5+ (Q2 2015), a Git repo will support multiple working trees with git worktree add <path> (and that will replace contrib/workdir/git-new-workdir)

Those "linked" working trees are actually recorded in the main repo new $GIT_DIR/worktrees folder (so that work on any OS, including Windows).

See more at "Multiple working directories with Git?"

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note that unlike git-new-workdir, git worktree does not allow you to create multiple worktrees for the same branch :/ – NikiC Sep 04 '16 at 21:14
  • 2
    @NikiC You can by adding the `--force` flag (`-f` also works) according to [the doc](https://git-scm.com/docs/git-worktree#_options) – Louis CAD Feb 14 '17 at 08:16
  • Just a little meta info here: only one of the worktrees has a `.git` _directory_ that contains all the objects (blobs, commits, trees). All other worktrees have a `.git` _file_ that contains something like: `gitdir: /path/to/project/.git/worktrees/main` (main is a name I gave to my worktree) i.e. it points back to your base worktree – mfaani Feb 18 '21 at 22:36
  • @Honey True. And I have [documented here](https://stackoverflow.com/a/35599305/6309) how `git worktree` proposes a set of commands to manage those worktrees (`move`, `prune`, `list`, ...) – – VonC Feb 18 '21 at 22:47
2

Yes it is possible with appropriate care. However you are taking one of the copies 'away' from the regular git directory using --work-tree=<path> option, so changes there won't be seen by git unless you specially tell it. I gave an example here single-working-branch-with-git - see the UPDATED segment.

Note that the git-new-workdir doesn't work on Windows XP as it requires Unix style links.

Community
  • 1
  • 1
Philip Oakley
  • 13,333
  • 9
  • 48
  • 71
1

First thing that comes to my mind it to checkout each branch on separate project. So: 1. checkout branch A on primary clone (1) 2. create a new clone (2) 3. checkout branch B in clone 2

Second approach could be to create a new branch (aka C) and merge both branch A and B to it. If they are complimentary than this might help with your review.

Munhitsu
  • 1,014
  • 1
  • 9
  • 16
  • I have tried doing this but when I check out the branch in one repo in my intellij other intellij having clone also gets impacted .any idea about that? – Rips Sep 08 '20 at 10:41
-1

Now git includes the command worktree to do exactly that.

memeplex
  • 2,297
  • 27
  • 26
-1

As already mentioned, you can diff branches with git diff:

git diff [--options] <commit> [--] [<path>…]

This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.

Excerpt above is from Git documentation.

kto
  • 124
  • 3