K Singh
K Singh

Reputation: 1730

Mercurial/Git: Different directories for different branches in same repository

I am new to git & mercurial and experimenting with these tools. I use perforce as source control at work so end up comparing the functionality with it.

Right now, I have a repository which looks something like below:

Repo_1:
   DummyProject:
   Master/Default - branch
   Release        - branch
   AnotherBranch  - branch

For Git/Mercurial, I am using Atlassian's SourceTree GUI client for operation. I have cloned this full repository locally and can see the branches in the GUI client. To switch between branches, I can checkout using this tool.

Let us say I am working in Master/Default branch and did some changes to some of the files. While I am still working on this branch some critical issue shows up and I need to make a change in the Release branch. At this point, if I switch to release branch from Master/Default branch, the SourceTree basically updates the working directory with the content of release branch. It gives me warning that I have some unsaved changes and I should either commit or shelve/stash these changes before switching to release branch.

In perforce, I would normally have 3 folders with name of the branches and I can fetch the latest from release branch and make changes on release branch without worrying of master/default branch being overwritten because both branches are in different directories. And once I am done, I can submit the changes in release branch and resume working in master/default branch.

Is there something I can do in either git/mercurial?

The closest option I could think of to do something like this is to clone the repository again (maybe from local one instead of remote to save network overhead) and then work on release branch and push the changes back to local and remote repository.

I am thinking that would be probably waste of local space because the repository would have to clone all the history and metadata again for this purpose.

Upvotes: 3

Views: 494

Answers (2)

Lazy Badger
Lazy Badger

Reputation: 97365

Contrary to Perforce (Subversion) Git/Mercurial

  • Have branches as logical, not physical object (you can have URL only for repo, not for directory||branch inside repository)
  • Have (easy) mutable history

These fundamental differences greatly change the usual workflow for former CVCS-users with "sacred" history of changes, allowing to commit WIP of any quality on any insignificant occasion (read "VCS-agnostic way" part for your use-case)

Yes, Git-worktree is a workable solution for fresh Git (with git stash on the other side), and for Mercurial you have (except noted "commit WIP + switch branch"):

Upvotes: 1

joshp
joshp

Reputation: 1892

Git has https://git-scm.com/docs/git-worktree which allows you to create more than one local working tree for a repo.

I use the approach of cloning the repo twice from the remote, keeping two or more copies locally. That works stunningly well, for me. I work in a commercial software environment where we always have back releases to support, current version heading for release, work-ahead branches, etc. There's a minimum of three active team shared branches at any time, and lots of sharing between pairs or threes of developers on feature branches.

The other tool that helps in some cases is git stash. That's a regular goto for me, for modifications that I have to make locally due to tools, but can never commit. But it's also useful for interrupting a task and switching branches for a bit.

Local storage, yes, quantitatively it's a waste, but, I don't care. What I care about is transmission time for pulls and pushes, and time spent fiddling with source control.

Others here can give more knowledge on Git. I know zero on mercurial.

Upvotes: 2

Related Questions