Is it possible to view multiple git branches at the same time for the same project?

GitBranchGit Checkout

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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?"

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

Now git includes the command worktree to do exactly that.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames RaitsevView Question on Stackoverflow
Solution 1 - Gituser229044View Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitPhilip OakleyView Answer on Stackoverflow
Solution 4 - GitMunhitsuView Answer on Stackoverflow
Solution 5 - GitktoView Answer on Stackoverflow
Solution 6 - GitmemeplexView Answer on Stackoverflow