How to Configure The refspec

Reginald LynchAugust 15th 2021, 1:09

In this post, we'll be using the jgit repository as our server repository, but we have to make a clone of it to a bare repository so we can push it. You can't push to the checked-out branch on a non-bare repository, as this can overwrite the work area and index.

Step 1 : Create a bare repository from the jgit repository and create a new Git repository

git clone --bare https://git.eclipse.org/r/jgit/jgit jgit-bare.git
git init refspec-tests
cd refspec-tests
git remote add origin ../jgit-bare.git

Step 2 : We also need to change the branch names on some of the branches to match the example for namespacing;

for br in $(git branch  -a | grep "stable-"); do new=$(echo $br| sed 's/-///'); git branch $new $br; done

Step 3 : Let's set up our new repository to only fetch the master branch. We do this by changing the fetch line under [remote "origin"] in the configuration file (.git/config), as follows:

[remote "origin"]
    url = ../jgit-bare.git
    fetch = +refs/heads/master:refs/remotes/origin/master

Step 4 : Now, we will only fetch the master branch and not all the other branches when executing a git fetch, git pull, or a git remote update origin, as follows:

git pull

git branch -a

Step 5 : Let's also set up a separate refspec to fetch all the stable* branches to the local repository as follows:

[remote "origin"]
    url = ../jgit-bare.git
    fetch = +refs/heads/master:refs/remotes/origin/master
    fetch = +refs/heads/stable*:refs/remotes/origin/stable*

Step 6 : Now, fetch the branches locally, as shown in the following command:

git fetch

Step 7 : We can also set up a push refspec that specifies where branches are pushed to by default. Let's create a branch called develop and create one commit, as shown in the following commands:

git checkout -b develop
echo "This is the developer setup, read carefully" > readme-dev.txt
git add readme-dev.txt
git commit -m "adds readme file for developers"

Step 8 : Now, let's create a push refspec that will send the content of the develop branch to integration/master on origin:

[remote "origin"]
    url = ../jgit-bare.git
    fetch = +refs/heads/master:refs/remotes/origin/master
    fetch = +refs/heads/stable*:refs/remotes/origin/stable*
    push = refs/heads/develop:refs/remotes/origin/integration/master

Step 9 : Let's push our commit on develop as follows:

git push

As the integration/master branch didn't exist on the remote side, it was created for us.