1

I am using git to manage publications in latex, hence my project are very simple in nature. I am using a service where I have one repository for free and hence I want to use Solution 2 from What's the best practice for putting multiple projects in a git repository?, but I don't know how to create the branch.

Do I just checkout master and then then use

git push origin myProject

Can someone post a full workflow which creates two independent repos in branches?

Community
  • 1
  • 1
Manuel Schmidt
  • 2,429
  • 1
  • 19
  • 32
  • `git checkout -b newBranch` (or from the advice of what you had linked, `git checkout --orphan newBranch`) – Makoto Jun 08 '15 at 05:34

1 Answers1

2

When you start with git init, you get and empty repository with the default master branch set up for you.

After that you can create new branches with git branch <branch-name> and switch to it using git checkout <branch-name>. The short way to do this is git checkout -b <branch-name> where -b indicates that the branch is new and should be created.

The contents of each branch inside each repository can be totally different. The two solutions you linked work similarly, with the change of the procedure used to change what project you're working on.

Assuming the second approach that you linked (multiple repos pushing to a single remote on varying branches):

  1. Create repositories for each "project" that should exist on the remote.

    $ cd /to/the/location/of/projects/project-1
    $ git init .
    $ git remote add origin <url to remote repo>
    $ git push -u origin master:project-1
    

    Now inside the /to/the/location/of/projects/project-1 directory is an empty git repo with branch master set up by default. The last command pushes the local repo's master branch to the remote repo's project-1 branch (and tracks it with -u). NOTE: the project-1 branch is created on the remote repo using this first push command.

  2. Create another repo for the second project.

    $ cd /to/the/location/of/projects/project-2
    $ git init .
    $ git remote add origin <url to remote repo>
    $ git push -u origin master:project-2
    

    Now the second project is set to another local repo, but is pushed to the same repo as the first one, though inside another branch. The -u flag makes the local master branch track changes in the remote project-2 branch.

The remote repository (which was added as origin to each local repo) has three branches: master (created automatically if the remote software does that for you), project-1 and project-2. project-1 receives updates from your local repo for the first project. The project-2 receives updates from the second local repo you have.

Now when you work inside the first local repo and so changes, you can update them to the remote using

# Inside `project-1` directory repo
$ git commit -am "Commiting changes in project 1."
$ git push origin master:project-1

And similarly with the second local repo

# Inside `project-2` directory repo
$ git commit -am "Commiting changes in project 2."
$ git push origin master:project-2

This approach is good in the following ways:

  • Your distinct projects are actually separated to different local repositories, which makes it easy to split later on to truely different remotes if needed.

The downsides are:

  • You might do a few mistakes every now and then if you're not careful, e.g. pushing the local project-1 master to the remote project-2 branch and so on. You could create easy to remember aliases or something to combat this.

If you do not wish to use the default master branch for each local repository, you can create and checkout the matching branch names using

# Inside the `project-1` dir locally.
$ git checkout -b project-1
$ git push -u origin project-1:project-1 # the ':project-1' part might be redundant here.

...

# Inside the `project-2` dir locally.
$ git checkout -b project-2
$ git push -u origin project-2:project-2 # the ':project-2' part might be redundant here.

Bottom line: be careful with the branches you're working on. Be sure to checkout the correct one before doing changes and commiting. Be sure to push the right branches to the right remote branches. Be sure to keep good notes on what branch holds which project contents.

About cloning:

If you or someone else clones the remote repository (which contains remote branches of each different project), bu default they will clone the whole set of project branches to their development environment. This isn't the end of the world, but then the workflow inside the cloned repo initially (almost) mirrors the solution 1 which you linked in your question: 1 local repostory with all the projects on different branches.

To clone only a single branch (project in your case) you can use

$ git clone -b project-x --single-branch <remote-url>

EDIT:

To sync changes from the remote repository to local projects, you should avoid using git pull, as it automates fetches and merges which might create conflicts.

Considering you're on two local repos with master branches used for development and remote branches project-1 and project-2 sitting inside the remote repo:

# Update project 1 from remote inside the project-1 repo dir.
$ git fetch origin # fetch changes from origin
$ git merge origin/project-1 master # merge remote project-1 branch to local master branch
ojrask
  • 2,799
  • 1
  • 23
  • 23