1

Supplementary question with: Git merge removed the latest code after merging the origin branch

Branch: feature-1
You can see there are many commits in it.
enter image description here But when I generate a merge request, it doesn't show my all commits. Basically doesn't display commits before 13th Sept in Merge Request commits tab.
enter image description here So in which case the above will happen, there are other devs who are also working in the same branch due to the same functionality if that matters.

Side question: Doesn't GitLab checks all files difference during MR (Merge request)? Or just shows the diff. of files which in commit list?

Update

22159e61

enter image description here

b0204dee

enter image description here

DS9
  • 2,995
  • 4
  • 52
  • 102
  • Any `git` based website like GitLab will use `git merge` internally, so just refer to the [tutorial of this command](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging) – Alexey S. Larionov Sep 20 '21 at 14:59
  • But why it doesn't shows commits before that date (i.e. 13th sept)? What could cause such issue? – DS9 Sep 20 '21 at 15:26
  • I would assume all of the commits that aren't showing up in the MR, are already in the target branch. Can you confirm that? – TTT Sep 20 '21 at 15:34
  • If you're MR'ing into `master`, then if this command returns a commit then you know that commit is already in the master branch: `git rev-list origin/master | grep 22159e61` – TTT Sep 20 '21 at 15:35
  • @TTT yes for some reason it returns the record, i have added the screenshot above, how the commit of one branch individually get committed in master branch? – DS9 Sep 20 '21 at 15:46
  • i have fetched all hash using git rev-list origin/master and then searched the commits which is not appearing in MR and all hashes are already in master branch, i don't know how though? is there a way to identify that? I am sure that i didn't uploaded my changes into master branch (i.e. didn't generated the MR earlier) – DS9 Sep 20 '21 at 15:52
  • @DS9 you didn't have to check them all, the one I gave you was the top commit that must have already been in `master`, and by definition all other commits before it would have to be there as well. I assume either you or someone else already MR'd those into `master`, or somehow you managed to push those commits into `master` without an MR. (If protection isn't enabled on the `master` branch.) – TTT Sep 20 '21 at 15:54
  • i am on windows :( so grep didn't work. Also master branch is protected. I am the only one with "Maintainer" permission, so trying to figure out what went wrong & when. I have added ss of that commit in question. Is there a way to figure out the cause? – DS9 Sep 20 '21 at 15:58
  • 1
    If you look at the history of `master` (the graphical view might be most helpful), and then go find commit 22159e61 in that view, you'll see how it got into `master`. (Most likely a merge commit with an MR number.) Side note, I recommend installing Git Bash on Windows. It's a much better command line experience than Windows command line or Powershell. And, you get the added advantage that most help you find on SO assumes you are running Git commands in a *NIX shell. – TTT Sep 20 '21 at 16:04
  • Figured it out, some developer created branch from intermediate branch (i.e. UAT) and my changes was already in UAT and then manually replace the files in which i have made changes. so it was not visible in MR (only their changes was visible in MR) and hence it got merged into master. – DS9 Sep 20 '21 at 16:04
  • What happened in `b0204`? It looks like some sort of merge but the thing being merged appears to be this very branch. What happened here? Did you do something weird like merge and then change the name of the branch? – matt Sep 20 '21 at 16:06
  • @matt i have added ss of b0204dee too. Basically one of developer used some kind of weird method to force gitlab to only shows difference of his changes. We have this structure: UAT -> MASTER. so for some reason he has created branch from UAT and then manually replace the files from MASTER branch and generated the MR. So that might be the reason my commies went ahead but files didn't into MASTER branch. and hence cause me the both the issues which i am facing. – DS9 Sep 20 '21 at 16:14
  • @matt can that be the reason behind the such weird issues. If that is the case then how can i make sure to avoid this in future. Because i was just relying on GItlab > Merge Request > Changes tab to view the changes made by user and not on the commits tab. – DS9 Sep 20 '21 at 16:19
  • I do not understand what you are saying happened in `b0204` but it sounds very bad. — Anyway, a merge request works like a merge. (Because it _is_ a merge.) If some of your commits have already been merged into `master` (because of whatever the hanky-panky was, previously) then they will not appear when you merge this branch into `master` now. – matt Sep 20 '21 at 16:20
  • yes you are right, but is there a way to overcome this issue? i.e. someone create branch from UAT (which has my changes) and do their changes in it. But their changes got cleared for Master release and what they did is manually removed my changes (may be replacing the files from master manually in which they have not made changes) and pushed on Master. – DS9 Sep 20 '21 at 16:27
  • It can be a use a tool to make someone's life difficult. i can merge someone's branch into mine then manually remove their changes and they will have a hard time to figure it out. – DS9 Sep 20 '21 at 16:37
  • Does this answer your question? [How can I fix a reverted git commit?](https://stackoverflow.com/questions/5354682/how-can-i-fix-a-reverted-git-commit) – Joe Sep 21 '21 at 07:04
  • @Joe I have got my answer from torek. I have changed question title as suggested in answer. – DS9 Sep 21 '21 at 07:07

1 Answers1

2

Based on the discussion in the comments, it seems that your earlier commits were already merged. It's just the case that the person who made that merge undid your changes.

What this means is simple: you cannot use the original commits any more. They're already in the repository, followed by a subsequent commit that undid your work. You must now redo your work in new commits.

In other words, I think the question is now "How can I copy my old commits to new commits that achieve the same result, after someone backed out all my work?" That makes this a question about Git, rather than about GitLab.

Fortunately, it's easy to do, using either git cherry pick—this is the manual method—or git rebase, which automates cherry-picking. It's probably easier with git cherry-pick, even though this is the manual method, because git rebase itself is too clever. It may, depending on circumstances and invocation, notice that the commits are already there, and thus choose not to copy them.

So, let's look at how you copy commits that someone wrecked and then undid. Note that we do not do this in some fancy GUI, or in a web browser. We need command line Git, preferably with a sensible shell like bash, although it can be done in horrible CLIs like CMD.EXE. (I'm sure it can be done in PowerShell too, but PowerShell tends to be of the opinion that everything Microsoft does, like using UTF-16-LE, is good, and in fact that's not good. So I'll use bash and $ prompts here. Hah, I see TTT already made a similar comment. )

The first step is to figure out which commits you want to copy. You can use git log, e.g.:

$ git log --all --decorate --oneline --graph

or you can use any of the methods shown in Pretty Git branch graphs, but remember that the goal here is to get the commit hash IDs so that you can cut and paste them with your mouse, into a file.

Next, cut and paste each of the desired commit hash IDs into a file. Use whatever editor you prefer to create a simple text file, grab each hash ID (and perhaps the --oneline text here as well) and copy it to this file. Save the file, in case things go wrong or get interrupted.

Now, in another window—so that you can keep the editor open on this file—start up another shell and maneuver into the repository where you'll be working. Run git fetch to get your own repository up to date with the remote repository on GitLab. Then check out a new branch based on the remote's master or main or whatever branch you intend to merge into. (You might need to clean up any unfinished work first.) I'll assume you'll be merging into master here:

  • git fetch: to fetch new commits from origin
  • git status: to make sure everything is clean and ready to begin work
  • git switch -c new-foo-feature-123 origin/master: to create a new branch named new-foo-feature-123 starting from the correct commit

Plug in a better name than new-foo-feature-123, of course.

Now, using the hash IDs saved in the other editor window, copy the hash ID of the first commit to copy. Then, in the bash window, type in:

$ git cherry-pick ☐

(note that there is a space after the word cherry-pick here; that's why the little box is there, to show the blank) and then paste in the hash ID from the editor window. If that hash ID is abc1234 you'll now have:

$ git cherry-pick abc1234

Press the RETURN or ENTER key to run this cherry-pick command now. If it succeeds, great! If it has a merge conflict, solve the merge conflict, add files as needed, and run git cherry-pick --continue to finish the operation. In any case, mark, in the editor window, that this commit is now done, once it's done—this keeps track of your progress.

Now cherry-pick the second commit to copy in exactly the same way. Mark the commit "done" in the editor window, once it's done.

Repeat for each commit you need to copy. When all commits are copied successfully, you now have a new branch you can use with git push:

git push origin new-foo-feature-123

(using your Better Branch Name of course).

You can now make a new merge request with the new branch. Close out the old one as superseded by the new one.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks a lot @torek, i have already started using git bash on windows :). One follow-up question, Is it normal practice to check "Commits" & "changes" both tab in Gitlab. I was only checking the "Changes" tab, so is it good approach to also check all the commits as well. What is the standard/good method? – DS9 Sep 21 '21 at 06:59
  • Also changed the title as well :) – DS9 Sep 21 '21 at 06:59
  • I don't use GitLab so I don't know which tabs do what there. – torek Sep 21 '21 at 08:07
  • any way thank you so much for providing detail answer, i have followed the steps and get back my all changes. – DS9 Sep 21 '21 at 11:11