Trying to fix merge conflict by rebasing, change X closed - gerrit

I am currently trying to fix some merge conflicts that are coming up in Gerrit. I've tried to rebase and submit my commits manually from Gerrit, but it hasn't been working out. What I'm trying to do now is follow this guide to rebase in order to fix the merge conflicts. Once I've manually fixed the merge conflicts in VScode, I try to push temp-branch with git push origin HEAD:refs/for/master. It gets rejected with this message:
! [remote rejected] HEAD -> refs/for/master (change <link to B> closed)
4e6b4ca (HEAD -> temp-branch) A
97bf4ds B
5392gsa (origin/master, origin/HEAD, master) C
...
17sfv2t B
I'm assuming this problem is due to the fact that 17sfv2t B has already been merged, but I'm not too sure. This is my first time with the rebasing process/Gerrit, so I might have missed some important details. Any help on this issue would be greatly appreciated!

Yes, you're right, the problem is due to the fact that the change with commit B is closed.
To solve this issue, first of all, you need to clarify why your "git log" has two B commits (97bf4ds a 17sfv2t) and how is possible to have a commit with a non-hexadecimal hash (17sfv2t), this is really weird.
It seems that the issue is related to commit 97bf4ds. Probably this commit has (by mistake) a Change-Id (a line in the commit description like, for example, "Change-Id: I0ff3092b7970f354df98c81a7d53f60ca826a55c") which is related to the closed change in Gerrit. Is this commit done by you? Why this commit has a Change-Id from a closed change?
Probably all you need to do is fix this Change-Id (using the "git rebase -i" command) before pushing the commits to Gerrit, but it's impossible to know for sure because you didnĀ“t specify exactly what are you doing and what commands have you exactly executed to get in this situation.

Related

Is it possible to delete a pull-request on BitBucket?

I cannot find an option to delete a PR on BitBucket.
Am I overlooking something or it's really not possible?
You can decline a pull request which has the same result -- stopping / removing the PR.
As per the link jonrsharpe mentioned, to the right of the merge button there are 3 dots. Under that menu you should have a delete option if you have permission to delete.
This is available only for BitBucket Server, not on BitBucket.org.
In BitBucket.org there is no option to delete the PR.
For Bitbucket Cloud, there are no way to do this. One way of getting by is to have a 'dev/junk' branch, used for declined or useless pull requests.
Then just edit the existing request to go into this branch and merge.
Data is still there, in case you need it some day, or if it's sensitive info you can remove the whole branch. If its already declined before, well, nothing can be done then other than recreating that repo
See https://jira.atlassian.com/browse/BCLOUD-8089 for the update on this feature request and vote on it!
I wanted to delete a pull request of a branch that had already been merged and deleted. Even though I am an admin of my project, I cannot see any "delete" options. For me what worked was to recreate the deleted branch from the main branch and push it. E.g.
git checkout master
git checkout -b [deleted branch name]
git push -u origin [deleted branch name]
Then I opened Bitbucket and the branch showed up as "merged" and disappeared from the PRs list.
You cannot delete the PR in bitbucket.org .
Using Decline option will do exactly what you want - the PR won't be visible in the tab Pull requests (you need to sort PR by Decline to see it) but on tab Branches you will see that in column Pull request you have removed your problematic PR.
PS you cannot undo Decline of PR, so take care

Is there a way to add an alternative fix in Gerrit?

Right now, I am committing the changes for a bug fix (ticket), and then do a
git push origin HEAD:refs/for/master
and then go through the code review on Gerrit. If a coworker proposed a totally different fix, is there a way to try this alternative fix somehow as the same Gerrit code review but as an "alternative way"?
I thought of using this way:
do a git log to look at the commit number exactly before the commit
git checkout that commit
git checkout -b ticket-1234-alternative to start another branch and try the alternative fix
So this method starts from how the project was when you make the fix, and then lets you do an alternative fix, as a separate branch. Is there a way that might not involve creating a different branch but using the same branch and possibly associate it with the same Gerrit code review?
If you want to try a different fix you can choose one of the following:
1) You can amend your original commit (git commit --amend) and push the new commit as the patchset2 to the same change. If you decide to go back to the original commit you'll need to push it again as a patchset3.
2) You can make a new commit based in the parent commit (as you have suggested) and push to a new change on Gerrit. When you decide which change will be used you just need to abandon the other one.

How do I load the second to last committed version of my project using git?

Suppose that I made some changes and did this:
git add -A
git commit -m "comment commit_1"
Now I made more changes and did this again:
git add -A
git commit -m "comment commit_2"
Now, I basically want to discard commit_2 and start modifying my project again at the point of commit_1.
How do I do this?
If you don't mind losing the revisions at all (as if they never happened at all), you can do:
git reset --hard HEAD~
If you want to keep the current history and get a new version on top of what you have where you get rid of the changes of the revision, you can do:
git checkout HEAD~
git reset --soft the-branch
git commit -m "Taking back changes from the last revision"
# And if you like the result of this:
git branch -f the-branch
git checkout the-branch
There you go.
Now, I basically want to discard commit_2 and start modifying my project again at the point of commit_1.
If you literally want to discard commit_2, then it may be that you can use
git reset --hard HEAD^
(Note that at the time I wrote this, another post incorrectly told you to use HEAD~2; that would discard both commits, which is not what was asked.)
There are caveats to using commands that remove commits from a branch.
The first is that you could lose the changes you made in affected commits. That may seem a strange warning - you asked how to discard the changes - but realizing that mistakes happen, it's cause to be careful with these commands. (Just to clarify - if you did something like this accidentally, the changes would not be immediately lost forever; the reflogs offer a little safety net.)
Other issues arise if your repo has remotes and you've pushed the commit(s) in question to any of the remotes. It doesn't sound like that's an issue here, but if it is then there is more you should consider before removing commits from a branch.
But if you've looked it over and determined that you really do just want to discard the commits, the above should work. Do note, HEAD^ is just one of the possible expressions that refers to the second-to-last commit. At the moment it may be the simplest thing to use, but it won't always refer to that particular commit. HEAD^ really means "the commit which is the parent of the currently-checked-out commit".
If you're on the master branch, then master^ would also work - and would not depend on what's currently checked out. Even that changes whenever the master branch moves; if for some reason you need an expression that would continue to refer to the particular commit, you could create a tag
git tag mytag master^
and then mytag would work; or you could look up the commit ID of the commit in question (though it won't be a user-friendly name)
But I digress...
This is different from - and more drastic than - merely loading the 2nd-to-last commit as the question title suggests. To simply update your working tree with the 2nd-to-last commit's state, you could
git checkout HEAD^
This moves the HEAD (i.e. changes what's checked out) and updates the index and work tree, but it still keeps the commit on master in case you need it again later. However, it also puts you in a state called "detached head", meaning that git isn't really expecting you to create new commits from here.
If you want to both keep the old commit around, and be in a state where you can add commits on top of commit_1, then you need to create a new branch. If you did the previous checkout command, you could then
git checkout -b mybranch
and new changes could be committed to the new branch while leaving the original commit_2 on master. Or, you could put a branch (or tag) on commit_2 and then move master back to commit_1.
git checkout master
git branch mybranch
git reset --hard HEAD^
That may seem like a confusing number of options; but really that's because each one serves a somewhat different purpose, and only someone involved in your project is likely to have all the context to decide which makes sense.
So tl;dr - it sounds like you might be looking for git reset, but just doing that alone is only suitable if you really intend to permanently discard the changes from commit_2. If you really would want to preserve them for future reference (But maybe just weren't sure if that's an option) then there are ways to do that, too.

What does the git commit --squash option do and why would it be useful?

I've been looking for an alternative solution for squashing a series of commits in a branch. What I've done in the past is to use git rebase -i HEAD~<#commits> and then chose which commits to squash. Typically I picked the latest commit, and squashed redundant commits in between.
This approach works reasonably well, but I was hoping for a faster way. I noticed that there is a --squash option for git commit. But I'm not sure how this works. What does it do exactly, and how do you use it? Would this be a solution for my problem?
From the documentation:
--squash=<commit>
Construct a commit message for use with rebase --autosquash. The commit message subject line is taken from the specified commit with a prefix of "squash! ". Can be used with
additional commit message options (-m/-c/-C/-F). See git-rebase[1] for
details.
--squash (also the --fixup flags) takes a commit as an argument, and formats the message for use with --autosquash.
It's possible to set rebase.autosquash = true in your config to make the whole process shorter.
If you want to squash your last N commits:
git reset --soft HEAD~N && git commit
This will put the head on HEAD~N, so index and the working directory will not be altered at all, meaning that you can now use git commit and create a single commit for all the changes.
Consider you are working on feature branch where you have done say 15 commits. Now you are done with the task and want to merge it master. These 15 commits will make history of master branch long, better option is to squash them in single commit , give it meaningfull comment. Master history is clean and good for you also to review in future.
Git merge master --sqash.
Now IDE also provide very good support for this option.

Unable to Merge

As regular user of bitbucket never face this problem before. Created Pull Request in regular way and when trying to merge it throws error without any details about reasons. Tried for several days with gap in between and also tried in different timing. What will be the reason and how to solve it.
Since you don't know any details about the error, just merge your branches in your command line:
git merge my-feature-branch
git push --all
(run this in the branch where you want my-feature-branch to be merged)
Also, if you consider so, you can always open an issue here.
For future reference, it also works when we open bitbucket instance in incognito.

Resources