Mahmoud Zalt
Mahmoud Zalt

Reputation: 31130

How to push a single commit (without its child commits) to a different repository?

Say I have:

Commit 5 < HEAD
Commit 4
Commit 3
Commit 2
Commit 1

I want to Push (Commit 5) to different repo WITHOUT commits 2,3,4. Is that possible?


The reason why I need it:

I have 2 projects:

While working on my (Project-1) I might need to do some changes to the (Starter).

What I currently have is 2 remotes in (Project-1):

Normally while working in (Project-1) I push all my commits to the origin master branch.

However, when I do changes to the (Starter) I commit them alone and I try to push that specific commit Commit 5 to the (Starter) repository. This is working fine! using the git push starter c78d92e32ec1a:master.

But the problem is when I push Commit 5 it also push all the previous commits (2,3,4), which in my case are related to the (Project-1).

Is it logically possible to only push specific commit even if it's not the last one. Example merge Commit 4 with (Starter) repo.

Upvotes: 1

Views: 1432

Answers (2)

Mahmoud Zalt
Mahmoud Zalt

Reputation: 31130

OMG it's working

  • select one of the following options:
    • option 1: starting from scratch
      • clone the starter - set the origin remote pointing to the starter repository url.
    • option 2: already have the project:
  • add starter remote (git remote add starter [email protected]:username/starter.git)
  • see both remotes (git remote -v)
  • composer install “ignored by git so you will see the same file when switching branches, will read composer.json and install what’s needed"
  • add the .env file “ignored by git so you will see the same file when switching branches"
  • checkout a new “starter" branch (git checkout -b starter)
    • see both branches (git branch -v)
    • fetch everything from the starter branch (git fetch --all)
    • override the starter branch with the starter repo “have the exact copy of the starter repository in the starer branch" (git reset --hard starter/master)
    • make sure the starter branch has the latest changes from the starter repository (git pull starter master)
  • checkout the master branch (git checkout master)

    • work on the master branch normally

      • work on the project code and commit.
        • push your commits whenever you want to the remote repository of your project (git push origin master) “or to any other branch"
      • work on the starter code and commit as well "the part of the code that is reusable and beneficial for the starter to have” {just careful to never include project code with your starter commits}

        • READY TO SEE THE MAGIC!

          • checkout the starter branch (git checkout starter)
          • update the starter from its master repository (git pull starter master)
          • cherry pick your commits from the starter “find the commits ID’s from the (git log) while you are on the master branch” (git cherry-pick 98a3d7g4h2dfs)
          • see the commits are all good and ready to push (git log)
          • push the starter to a new branch on its repository (git push starter starter:feature-branch-name) “important to push to a branch, not to master directly"
          • from github create a PR and merge it anytime you want
          • if you merged in the previous step then do pull the merge commit (git pull starter master)
    • (optional) merge the starter with the project using any of these options:
      • option 1: merge and keep the project changes (git merge -X ours starter)
      • option 2: merge and overwrite your project with the starter (git merge -X theirs starter)
      • option 3: merge and solve the conflict manually (git merge starter)

Upvotes: 0

Jeff Puckett
Jeff Puckett

Reputation: 40861

Git is very well suited for what you're trying to achieve, however I'm afraid you should reorganize a bit.

Given the information in your question, you only have one branch and two remotes for two distinct projects. You want two different branches.

Start by checking out the most recent commit for starter that does not have parent commits of Project-1.

git checkout Commit1SHA

Create a new branch for your starter project, and set it to track starter master.

git checkout -b stmaster
git branch stmaster -u starter/master

For this time only, we can fix up the tree with a cherry pick, but things will be cleaner moving forward.

git cherry-pick Commit5SHA

Now a push will only send commits 1&5 to starter

git push

From here out, you're setup for a good rebase strategy to keep your merge lines tidy. You can continue working on Project 1, making commits, and pushing normally.

git checkout master
echo "hello world" > newfile.txt
git add .
git commit -m "create hello world"
git push

Now when you want to make changes to starter, checkout that branch for work.

git checkout stmaster
echo "starter changes" > more.txt
git add .
git commit -m "create more changes"
git push

Now rebase those changes into project 1 to keep commit lines straight.

git checkout master
git rebase stmaster
git push

Upvotes: 2

Related Questions