1

i'm having a really big problem with git knowing that i'm new in it, wherever i push from a specific branch to remote in the merge request it shows me other commits from another branches in my merge request. this is the step that i do from my local branch:

git checkout -b firstBranch
git add <filename>
git commit -m "msg"
git push origin firstBranch.

when i do a merge request in gitlab its shows me this commit + previous commit that i pushed from other branches like secondBranch, thirdBranch etc... how can i do to only push my commit knowing that i tried the git push origin commitId:branch but it did not work

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Abdel
  • 53
  • 5

1 Answers1

2

That is because you are starting to work from a branch that is not yet merged into the main branch.... so, if you want to only see the changes of that specific feature that you are working on, you have basically 3 options:

Wait

... until the branch that you started from is merged into the main branch

Then you will only see your changes in the PR.

Rebase

... your feature on top of the main branch so that the revisions from the original branch are not part of the history of your branch anymore.... say that your feature branch has 4 commits:

git rebase --onto origin/main @~4 @

If you push now, the PR will only include the changes of your feature.

Of course, sometimes you depend on changes introduced on non-merged branches.... then you will have to use any of the other 2 options.

Use a different target branch (temporarily)

Set the PR to be merged into the branch of the PR that you started working from. Then the PR will show you only your changes. Then, when the branch that you started working from is merged, you can change the PR to point to the real main branch.

To wrap-up

Just in case, when you start working from another unmerged branch, you have to be extra careful if the base branch that you used moved (like a rebase is done on it) because if you then try to rebase your branch, you need to skip the revisions from the original base branch that you used (like I did in the git rebase --onto). If you tried a simple rebase, those original revisions from the (old) base branch are not valid anymore will also be rebased, which you definitely do not want to do... unless you really (like really) know what you are doing.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • thank you for your response but to be honnest i didn't understrand half of it. are u saying if i rebase my firstbranch in my main then create a new branch and push it to remote i will have only one commit in the merge request. for information i can't push directly to remote main. i need a merge request everytime. i can't do a rebase on remote either. and also my local have multiple merge waiting on remote that didn't merge yet. – Abdel Aug 03 '22 at 19:47
  • I think you are not yet fully conscious of all the concepts that I am juggling with in the response. So.... you can _locally_ rebase anything on top of anything. Whatever you do on your local repo won't affect anything on the remote... unless you start pushing stuff. Then, I am not asking to create any new branches. To try to play with your example... you can do (after finishing up your work on firstBranch): `git fetch origin; git rebase --onto origin/main firstBranch~4 firstBranch` (assuming it's 4 revisions in firstBranch). I think the problem is _where you start from_...... – eftshift0 Aug 03 '22 at 19:51
  • What branch is checked out right before the first checkout of your example? If you are on top of main (either local main updated from remote or on top of origin/main) then we could be talking about something funky. If you are on top of another branch, then _that_ is the problem because your branch includes revisions from _that_ branch in its history. – eftshift0 Aug 03 '22 at 19:53
  • Now _I_ am the one who is not understanding. Can you show us (adding in the original question) the output of `git log --oneline --graph` with the main branch and your feature branch so that we can better understand what you are seeing? – eftshift0 Aug 03 '22 at 19:55
  • both branches contains this stuff: `| * cedf31416be [-29177] feat(fonds_avenant): add file modele scpi stellium | * 7e9f742b0ea [feat]-28126 : adding the olympic url | * c04d707182f [FIX] upload document campagne rentier | * ae90e7d03e2 -29340 et -29339 | * 6cb32dcd616 fix/-29393 | * 14a8d0adc63 ...` – Abdel Aug 03 '22 at 20:08
  • Post in the original question (edit it)... _and_ I hope you are already checking the questions/answers that were used to point this question as being a duplicate. The whole story that I am trying to convey is probably already much better explained there. There are extremely detailed answers most of the cases from very patient souls around here :-D – eftshift0 Aug 03 '22 at 20:10
  • thank u sir i know i'm not very helpful in my details. thank u for your time, much appreciated – Abdel Aug 03 '22 at 20:12
  • Hey, no complaints.... I don't mean I am losing my temper.... what I mean is that these guys are extremely patient to write such detailed answers.... I mean, they are awesome! I have been learning **a lot** from the answers here. – eftshift0 Aug 03 '22 at 20:14
  • i think my problem is that my local branch is ahead of my remote branch with 11 commits, those commit are what i'm having everytime i merge request a new branch. – Abdel Aug 03 '22 at 20:15