name
name

Reputation: 5175

git working on two branches simultaneously

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?

Upvotes: 240

Views: 120617

Answers (6)

mirekphd
mirekphd

Reputation: 6763

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

Upvotes: 1

Stefan N&#228;we
Stefan N&#228;we

Reputation: 3104

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.

Upvotes: 42

VonC
VonC

Reputation: 1323743

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).

Upvotes: 250

Mostafa Barmshory
Mostafa Barmshory

Reputation: 2039

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.

Upvotes: 3

Kostia R
Kostia R

Reputation: 2565

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.

Upvotes: 0

BastiBen
BastiBen

Reputation: 19860

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

Upvotes: -3

Related Questions