234

I have a project with many branches.

I would like to work on several branches simultaneously without switching back and forth with git checkout.

Is there any way I can do that besides copying the whole repository somewhere else?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
name
  • 5,095
  • 5
  • 27
  • 36
  • 1
    possible duplicate of [Multiple working directories with Git?](http://stackoverflow.com/questions/6270193/multiple-working-directories-with-git) – user229044 Mar 07 '14 at 18:31
  • 1
    Git 2.5+ (Q2 2015) will officially support this with the new command `git checkout --to=`. See [my answer below](http://stackoverflow.com/a/30186843/6309). – VonC May 12 '15 at 09:30
  • 3
    Actually, the command will be `git worktree add []` (Git 2.5 rc2) – VonC Jul 13 '15 at 23:08

6 Answers6

240

Git 2.5+ (Q2 2015) supports this feature!

If you have a git repo cool-app, cd to root (cd cool-app), run git worktree add ../cool-app-feature-A feature/A. This checks out the branch feature/A in it's own new dedicated directory, cool-app-feature-A.

That replaces an older script contrib/workdir/git-new-workdir, with a more robust mechanism where those "linked" working trees are actually recorded in the main repo new $GIT_DIR/worktrees folder (so that work on any OS, including Windows).

Again, once you have cloned a repo (in a folder like /path/to/myrepo), you can add worktrees for different branches in different independent paths (/path/to/br1, /path/to/br2), while having those working trees linked to the main repo history (no need to use a --git-dir option anymore)

See more at "Multiple working directories with Git?".

And once you have created a worktree, you can move or remove it (with Git 2.17+, Q2 2018).

Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    This should be the new accepted answer, since 2.5.X is now the recommended version, even for windows http://i.imgur.com/oQvUhVl.png https://git-scm.com/download/win – randers Sep 04 '15 at 18:56
  • Just to confirm, the process would be same even if I already have created branches in the past and now, want to work on them simultaneously? Thanks :) – Milan Nov 02 '20 at 20:40
  • 1
    @Milan Yes: you can chackout wortrees for existing past branches. – VonC Nov 02 '20 at 20:43
  • @VonC, thanks for such a quick reply. One more thing I noticed is that after running the `git add worktree ......` command for two different branches, every time it said: "HEAD is not at ". However, there can be only one `HEAD` right? How there can be two `HEADs`? Thanks! – Milan Nov 02 '20 at 21:16
  • 1
    @Milan What version of Git are you using? (on which OS?). As I documented in https://stackoverflow.com/a/35599305/6309, `git worktree add` has been improved in recent versions of Git. – VonC Nov 02 '20 at 21:20
  • @VonC, I'm using `git version 2.17.1` on `Ubuntu 18.04 LTS`. Oh, I see there is a `HEAD` in each worktree. Thanks a lot for sharing your link. It's very helpful. I was also thinking about how I would delete the worktrees I created but after skimming through your answer, I think I would get the solution, whenever required. Thanks again! – Milan Nov 02 '20 at 21:30
  • 1
    @Milan You are most welcome. 2.17 sounds too old for me. I wasn't born yet ;) – VonC Nov 02 '20 at 21:31
  • @VonC, oh that sounds weird because I even tried updating it using `sudo apt update` and `sudo apt install --only-upgrade git` but it says `git is already the newest version (1:2.17.1-1ubuntu0.7).` ! Is there anything wrong my system? – Milan Nov 02 '20 at 21:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224006/discussion-between-vonc-and-milan). – VonC Nov 02 '20 at 21:34
  • @VonC this throws the error: `fatal: ../cool-app-feature-A: '../cool-app-feature-A' is outside repository`. – y_159 Oct 06 '21 at 04:19
  • @y_159 what git version and OS are you using? – VonC Oct 06 '21 at 04:24
  • @VonC Ubuntu 18.04 and git version 2.17.1. – y_159 Oct 06 '21 at 04:58
  • @y_159 OK. Try and upgrade to 2.30.2 with ppa (https://stackoverflow.com/a/41357503/6309), and see if the issue persists. – VonC Oct 06 '21 at 05:23
  • @VonC `fatal: ../cool-app-feature-A: '../cool-app-feature-A' is outside repository at '/home//cool-app'` – y_159 Oct 06 '21 at 11:17
  • The error description is quite different from earlier one. – y_159 Oct 06 '21 at 11:27
  • @VonC with the above mentioned link, I was able to install git 2.33.0 version. – y_159 Oct 06 '21 at 11:49
  • @y_159 ok, what command do you type? – VonC Oct 06 '21 at 11:59
  • `user@user-VirtualBox:~/repo$ git add worktree ../cool-app-feature fatal: ../cool-app-feature: '../cool-app-feature' is outside repository at '/home/user/repo'` – y_159 Oct 06 '21 at 12:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237881/discussion-between-vonc-and-y-159). – VonC Oct 06 '21 at 12:13
41

Take a look at $GIT_SRC_DIR/contrib/workdir/git-new-workdir.

According to the commit logs from a port of this repository:

a simple script to create a working directory that uses symlinks to point at an exisiting repository. This allows having different branches in different working directories but all from the same repository.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Stefan Näwe
  • 3,040
  • 1
  • 18
  • 19
  • And the similar: http://stackoverflow.com/questions/655202/git-and-c-workflow-how-to-handle-object-and-archive-files/1889179#1889179 – Tobu Jan 12 '10 at 19:05
  • 2
    I know this is old, but could you provide some explanation please. – dav_i Jan 15 '13 at 10:23
  • @dav_i: Like [this](https://github.com/git/git/commit/4f01748d51b530c297eeb5a0ece9af923d5db937) ? – Stefan Näwe Jan 16 '13 at 12:20
  • @StefanNäwe Ah I was confused - too early in the morning... updated your answer with link. – dav_i Jan 16 '13 at 13:13
2

Git supports multiple worktree at the same time. For more information see:

How ever it is very hard to support multiple worktree with IDs. For example this is an enhancement request in JGet (eclipse ID) to support worktree.

So, you have to manage project manually (command line) with lots of problems or work with a single worktree in an IDE.

Mostafa Barmshory
  • 1,849
  • 24
  • 39
1

Here's a scalable version of the accepted answer (upvoted), with sequentially named branches (app1..app10).

Note we should try to avoid duplicate checkout (here: by checking out the master branch, which will not be covered by git worktree add):

$ git checkout master && for NUM in {1..10}; do git worktree add ~/git/<repo-name>/app$NUM app$NUM; done
mirekphd
  • 4,799
  • 3
  • 38
  • 59
  • Nicely done. Upvoted. I would just use [`git switch`](https://git-scm.com/docs/git-switch) instead of `git checkout`. – VonC Aug 07 '22 at 13:56
0

I suggest my small script http://www.redhotchilipython.com/en_posts/2013-02-01-clone-per-feature.html

It will do git clone and replace the config (to "look" at original repo, so pull/push will go into "main" repo) basically, but it's simple enough to serve an abstraction from actual bootstrapping.

Konstantine Rybnikov
  • 2,457
  • 1
  • 22
  • 29
-3

Not really as Git only supports to have one working copy of the repository data within the repository directory.

If you want to commit/pull to the same repository with two different working copies, you could create a bare repository and clone it to two working copies.

Whenever you have finished something, you simply push to the "main" bare repository.

Some hints:

man git-clone

git clone --bare

BastiBen
  • 19,679
  • 11
  • 56
  • 86