Home > Software engineering >  Checkout several branches from Git
Checkout several branches from Git

Time:02-10

I want to clone some Git repository on local machine, and create 3 folders:

master - for master branch of the project
tag777 - for the specific tag of the project
commit888 - for the specific commit from master branch

I want these 3 folders with the specific versions of the project to reside on local machine simultaneously.

So the question is - do I have to clone git repo 3 times or it is sufficient to clone it only once and create these 3 folders after cloning? What commands do I have to run?

For now I want only to "download" these 3 version of the project and don't want to modify or commit anything.

CodePudding user response:

Usually, people would clone once and then switch to a specific tag or specific commit whenever needed

In your case the steps would be

git clone <URL>/your-repo.git

The content of the directory your-repo would then contain the content of master (by default). To be sure, you can specify the branch when cloning using the --branch option

cd your-repo
git checkout tag777

The content of the directory your-repo would then corresponds to the content associated with tag777

Then if you do

git checkout commit888

The content of the directory your-repo would then corresponds to the content associated with commit888

If for some reasons, you really want to have three separate folders, you would then

git clone --branch master <URL>/your-repo.git  your-repo-master
git clone --branch tag777 <URL>/your-repo.git  your-repo-tag777
git clone  <URL>/your-repo.git  your-repo-commit888; cd your-repo-commit888; git checkout commit888

In that example, you end up with 3 folders your-repo-master, your-repo-tag777, your-repo-commit888 containing what you were looking for. The --branch option can be used with a branch name but also with a tag-name.

CodePudding user response:

You want to look into Git Worktree. It basically allow you to create multiple working directories backed by the same .git folders.

git clone https://github.com/your/repo master
git worktree add -b tag777 ../tag777 tag777
git worktree add -b commit888 ../commit888 commit888

This puts the .git directory only 1 time on disc, saving you a lot of data and keepinp stuff in sync a little easier. https://git-scm.com/docs/git-worktree

  • Related