1

Note: I don't have admin privilege on GitLab so I can't use mirroring repositories. I need to do via gitlab-ci with git commands.

I have got 2 different repositories. Source repo on GitLab, target repo on Github.

Source gitlab repo contains files like below:

├── A
├── B
├── C
│   ├── 1
│   │   ├── 2

Target github repo contains files like below:

├── D
├── E
├── F
│   ├── Z
│   │   ├── 5
│   │   ├── 6

I want to update the target repo to be exactly the same as the source gitlab repo files hierarchy. If the files in the target repo are different from the source repo, git commands should remove the differences on the target repo. But when I run my git commands I'm getting below result. It's adding all files together in the target repo. I just want to sync gitlab repo with github repo. If we have any differences I would like to delete all this and store files that only coming from the source gitlab repo.

This my result on the target github repo:

├── A
├── B
├── C
│   ├── 1
│   │   ├── 2
├── D
├── E
├── F
│   ├── Z
│   │   ├── 5
│   │   ├── 6

This is my expectation for target repo:

├── A
├── B
├── C
│   ├── 1
│   │   ├── 2

Here is my git commands:

- git config --global user.name "$github_username"
- git config --global user.email ""
- git fetch --all
- git remote add origin2 https://$github_username:$github_token@$github_url
- git fetch --all
- git checkout origin2/main
- git merge origin/main --strategy-option=theirs --allow-unrelated-histories
- git push https://$github_username:$github_token@$github_url HEAD:main

With above git commands, I'm also getting Merge remote-tracking branch 'origin/main' into HEAD extra commit in each push. I don't also want to see this extra commits. How can I avoid this also and how should I update the commands.

Arty-chan
  • 2,572
  • 1
  • 19
  • 25
hammer89
  • 343
  • 1
  • 5
  • 13
  • If you want to *check out* a particular commit, do that: don't *merge* your work with their work, just check out the particular commit. For instance, consider using detached HEAD mode with `git switch --detach origin/main`. Note that `--strategy-option` tells Git how to resolve *conflicts*, but it will still merge work, not just use their commit. – torek Nov 25 '21 at 06:54
  • @torek didn't get completely. Can you write an answer by showing it on my code? – hammer89 Nov 25 '21 at 07:41
  • A repository doesn't hold files. A repository holds *commits*. Each commit then holds files. To make repository B hold the same commit(s) as repository A, fetch from A, then push to B. Have B `git checkout` any particular commit you want. I'm not going to write your code for you though. – torek Nov 25 '21 at 08:14
  • @torek got it now. I followed your suggestion. But it's not suitable for my goal. Because I need to exclude one specific file(commit :) )before pushing B target repo. But if I follow this way I need to write `git add. , git commit` commands between fetching and pushing steps. This will be caused an extra commit on target history. Shortly I need to exclude file between fetch and pushing commands without extra commit history. If its not worked, I need to way with merge or rebase methods if its possible. – hammer89 Nov 25 '21 at 13:26
  • This negates your earlier statement *I want to update the target repo to be exactly the same as the source gitlab repo files hierarchy.* Either you want them exactly the same (all commits 100% identical), or you don't. If you want a file gone, you'll need to make a new commit in which the file is absent. – torek Nov 25 '21 at 23:56
  • Then forget about identical commit history. They can be different from each other. The only thing that matters to me is that the files that will come with the commits are exactly the same in both repositories. Exclude one file during sync it. – hammer89 Nov 26 '21 at 06:00
  • OK - given the new set of constraints, I'd probably fetch from the source repository (`origin` in this example), create a branch name at `origin/master` with `git switch -C for-target origin/master`, remove the one file you want gone (`git rm`), commit (`git commit -m "slave from source, minus one file"` plus error checking), and push (`git push -u secondary HEAD:whateverbranch`). If you want to maintain a history of the commits sent to `secondary` here there are alternatives. – torek Nov 26 '21 at 08:01

0 Answers0