threads / discuss / 44391

Is the entire working copy “at one branch”?

Subject: Is the entire working copy “at one branch”?

## tl;dr

3 messages between Oct 28, 2016 and Oct 31, 2016.

replies: 2people: 2as markdown or json

Stefan Monov· Oct 28, 2016, 15:01 UTC · lore

This is a very basic question but maybe I just don't know how to phrase my query well enough to find an answer.

Let me begin with a SVN example. SVN doesn't have a "current branch" as a property of a working copy, so I'll give the example with revisions instead. Consider a working copy with the dirs `foo` and `bar` in it. In SVN one can `update` foo to an earlier version, but keep `bar` at `HEAD`.

Is it possible in an analogical git working copy to checkout one branch in dir `foo` without changing the branch checked out in `bar`?

If not, why not?

TIA, Stefan Monov

Alexei Lozovsky· Oct 29, 2016, 09:47 UTC · re: Stefan Monov · lore

Re: Is the entire working copy “at one branch”?

Hi Stefan,

Generally with git, your entire working copy will have the same revision (set to current branch, aka HEAD). The idea behind this is that your working copy of a repository should always be in consistent state.

You can check out specific files or directories from another revision (mimicking "svn update -r1234 filename"):

    $ git checkout branch-or-sha-hash -- filename

However, SVN tracks the 'revision' thing on per-file basis, while in git this is a property of the working copy. So if you do like above then git will be telling you that the 'filename' has been changed (as it is certainly different from its pristine version in HEAD):

    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
            modified:   filename
So it's generally not recommended to do such a thing.

Another thing that you _can do_ in git to mimick SVN is the 'standard layout'. There is a feature called "git worktree" which allows you to have SVN-like directory structure with multiple directories linked to different working copies:

    $ mkdir my-project
    $ cd my-project
    $ git clone my-project-repository master
    $ mkdir branches
    $ cd master
    $ git worktree add -b branch-1 ../branches/branch-1
    $ git worktree add -b branch-2 ../branches/branch-2
After that you will have directory structure like this:
    $ tree my-project
    my-project
    ├── branches
    │   ├── branch-1
    │   │   ├── 1
    │   │   ├── 2
    │   │   └── 3
    │   └── branch-2
    │       ├── 1
    │       ├── 2
    │       └── banana
    └── master
        ├── 1
        └── 2
You can work with these working copies separately, like you
would be working with SVN. Commits in 'master' will go to the
'master' branch, commits made in 'branches/branch-1' will go
to the 'branch-1' branch.
Stefan Monov· Oct 31, 2016, 10:41 UTC · re: Alexei Lozovsky · lore

Re: Is the entire working copy “at one branch”?

Thanks Alexei!
On Sat, Oct 29, 2016 at 12:47 PM, Alexei Lozovsky <a.lozovsky@gmail.com> wrote:
Show 60 quoted lines
> Hi Stefan,
>
> Generally with git, your entire working copy will have the same
> revision (set to current branch, aka HEAD). The idea behind this
> is that your working copy of a repository should always be in
> consistent state.
>
> You can check out specific files or directories from another
> revision (mimicking "svn update -r1234 filename"):
>
>     $ git checkout branch-or-sha-hash -- filename
>
> However, SVN tracks the 'revision' thing on per-file basis, while
> in git this is a property of the working copy. So if you do like
> above then git will be telling you that the 'filename' has been
> changed (as it is certainly different from its pristine version
> in HEAD):
>
>     $ git status
>     On branch master
>     Changes to be committed:
>       (use "git reset HEAD <file>..." to unstage)
>
>             modified:   filename
>
> So it's generally not recommended to do such a thing.
>
> Another thing that you _can do_ in git to mimick SVN is the
> 'standard layout'. There is a feature called "git worktree" which
> allows you to have SVN-like directory structure with multiple
> directories linked to different working copies:
>
>     $ mkdir my-project
>     $ cd my-project
>     $ git clone my-project-repository master
>     $ mkdir branches
>     $ cd master
>     $ git worktree add -b branch-1 ../branches/branch-1
>     $ git worktree add -b branch-2 ../branches/branch-2
>
> After that you will have directory structure like this:
>
>     $ tree my-project
>     my-project
>     ├── branches
>     │   ├── branch-1
>     │   │   ├── 1
>     │   │   ├── 2
>     │   │   └── 3
>     │   └── branch-2
>     │       ├── 1
>     │       ├── 2
>     │       └── banana
>     └── master
>         ├── 1
>         └── 2
> You can work with these working copies separately, like you
> would be working with SVN. Commits in 'master' will go to the
> 'master' branch, commits made in 'branches/branch-1' will go
> to the 'branch-1' branch.

← back to recent threads