1

I would like to

  • Download a repository https://github.com/somedude/foorepo.git
  • delete the .git folder inside, run git init
  • Create a branch "cherries"
  • Code on the main branch

I know how to do the above. How to do the following?

  • Set above foorepo as remote for the cherries branch
  • pull commits from foorepo and apply them to cherries
  • cherry-pick interesting commits from cherries into the main branch.
Nils Lindemann
  • 1,146
  • 1
  • 16
  • 26

2 Answers2

1

I am searching for the proper git remote add ... command which adds the master branch, and only that, of the remote.

A possible option would be to "single-branch clone" the original repository:

git clone --branch master --single-branch https://github.com/somedude/foorepo.git
cd foorepo
git remote set-url --push origin https://your/own/remote/repository
git switch -c cherries --track origin/master

That way, your branch cherries can git pull from the original repository master branch, but push to another repository.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you, I think I found a solution, see my answer. Also, I do not need to push the cherries branch to another repo, just the master branch. – Nils Lindemann Mar 21 '23 at 11:22
  • @NilsLindemann The all idea behind my answer was to avoid getting *all* branches from the initial repository, while keeping an easy way to update your branch with the original repository branch. – VonC Mar 21 '23 at 12:16
  • I tested it. It is interesting, but I do not fully understand it. I noticed, it downloads the tags, how to prevent this? And how will I configure it to push/pull my code to/from a specific repo, like in my answer? – Nils Lindemann Mar 22 '23 at 07:44
  • @NilsLindemann By default, it pulls from the repo initially cloned, and push (`git remote set-url --push`) to any repository you want. – VonC Mar 22 '23 at 10:00
  • Interesting, I thought that I have read somewhere in the git docs that pulling/pushing to/from a single branch can only be done on the same remote. OK. Meanwhile, I found out how to prevent fetching tags, see my answer. – Nils Lindemann Mar 23 '23 at 01:31
1

This is how I did it: (Some commands can of course also be done via the file manager or in the Git GUI, e.g. Sublime Merge allows adding remotes via menus)

First, I clone the repo, and reinitialize a fresh git repo:

git clone https://github.com/someoneelse/theirrepo.git
mv theirrepo mylocalrepo  # I want another name
cd mylocalrepo
rm --recursive --force .git  # delete the .git folder
git init
git add --all
git commit --message 'Fresh start'

Next, I create a repo myexternalrepo on, for example, Codeberg.

Then I connect myexternalrepo/master to mylocalrepo/master:

git remote add myexternalrepo https://codeberg.org/me/myexternalrepo.git
git fetch myexternalrepo
git push myexternalrepo master --set-upstream  # '--force' can help. But use only in private repos.

mylocalrepo/master is now configured, the pull/push button(s) are functional in the Git GUI.

Now I create mylocalrepo/cherries and connect it to theirrepo/master:

git remote add theirrepo https://github.com/someoneelse/theirrepo.git
git fetch --no-tags theirrepo master  # This time I need just the master and no tags.
git checkout -b cherries theirrepo/master

The last thing to do, is to configure, that the pull button of the GUI, when in the cherries branch, only fetches the master branch of theirrepo, and also ignores the tags. These things make it more difficult to find my own branches and tags. So I make the following changes to the [remote "theirrepo"] section in .git/config.

To fetch only the master branch, I replace this refspec:

fetch = +refs/heads/*:refs/remotes/theirrepo/*

... with ...

fetch = +refs/heads/master:refs/remotes/theirrepo/master

To prevent that the tags are fetched, I add:

fetch = ^refs/tags/*
tagOpt = --no-tags

The fetch = ^refs/tags/* is a negative refspec blocking all tags. It is a workaround for the fact that setting tagOpt = --no-tags alone seems not to prevent tag fetching, which seems to be a bug in git or in its documentation, see this related question.

Now everything is configured, when on master, pulling/pushing will communicate with myexternalrepo, and when in cherries, pulling will communicate with theirrepo (pushing will refuse to work).

Nils Lindemann
  • 1,146
  • 1
  • 16
  • 26