How to change a patchset and push it as a new one? - gerrit

Is it possible to fetch an existing patchset (that has not been merged into my local machine), change and push it as a new Patch Set?

#Uncletall put all the steps there and the link, the only thing is that you should not delete the changeId and you should do a git commit --amend. I am giving him a +1.
It should be like this
On Gerrit, go to the review, select "checkout", on the Download field as opposed to "pull", "cherry-pick", or "patch", then copy the command.
On the git project paste the copied link from above
This will create a detached head, which is a branch with no name (I've been through the desert on a horse with no name, It felt good to be out of the rain.)
Name that horse! git checkout -b new_branch_name
Change what you want and do a git add on the files you want.
Do git commit --amend and keep the same Change-Id.
Push your changes:
git push origin <new_branch_name>:refs/for/<thatgerritbranchyouwanttochange>

Just follow the below steps:
cherry-pick your patch (from gerrit UI) to your machine.
Modify the content and run git add <modified file>.
Amend the last commit using git commit --amend that pops up a COMMIT-EDITMGS window. Save it accordingly.
Push your change to gerrit using git push origin HEAD:refs/for/branch_name
It will create a new patch set.

Consult Trying out a Change in the official documentation.
Here is what you do:
Checkout the change as described in the documentation
Create a local branch from the FETCH_HEAD
Modify your code
Commit the change using git --amend and remove the Change-Id in the commit message
A new Change-Id will automatically be added and this will result in a new Change Set
Push your change for review and Gerrit will see it as a new Change Set
As pointed out by #magnus-bäck, I was describing how to create a new Change-Set. If you want to add a new Patch Set to the current review you should NOT remove the Change-Id.

Related

Cannot do a pull request because branch is "identical to main"

I want to do the pull request for my chapter_3.
However, it states that my main and chapter_3 are identical.
How to make my chapter_3 not identical with main? Below i also attached my git reflog
$ git add .
$ git commit -m "chapter 3"
$ git push origin chapter_3
Simply, in your local repository, add a new commit (or now commits) to chapter_3 and push.
Then, since chapter_3 has new commits that main has not, you will be able to initiate a pull request from chapter_3 to main.
The problem is: you were not on local branch chapter_3 when you did your commit. You were on main.
In order to avoid any mishaps, I would:
clone the repository again
create a chapter_3 branch
report your work for chapter 3 there (in the new local clone)
add commit and push
That is:
git clone https://github/com/<me>/<myRepo> newClone
cd newClone
git switch -c chapter_3
# work
git add .
git commit -m "Add chapter 3"
git push -u origin chapter_3
Then you can make your PR.
Notes:
replace <me> by your GitHub account name, and <myRepo> by your target repository name. Don't use < and >: they are placeholder makers.
replace newClone by a new local folder name which does not yet exist (it will be created by the git clone command).

Why does XCode constantly do "Merge remote-tracking branch 'origin/master'"

I am new to XCode. Recently started working with a team on an iPhone project. We're using GitHub for our source control, and pulling/comitting/pushing straight from XCode (5.1).
We're all working on master at the moment.
I have noticed that our commit log has a ton of the following statements:
Merge remote-tracking branch 'origin/master'
These seem pretty superfluous. These items are almost always identical to a previous commit from another user.
Example Scenario:
User A and user B do a PULL to start working on code.
User A edits SomeViewController.m and does s COMMIT and a PUSH.
User B edits AnotherViewController.m and does a COMMIT and a PUSH.
User B is greeted with an error [from XCode] that he must PULL before he can COMMIT.
User B does a PULL, then a COMMIT.
Now there are 3 items in the commit log:
The change from User A
The change from User B
Merge remote-tracking branch 'origin/master'
But #1 and #3 are identical! What are we doing wrong? Or is this just a neccessary evil of using Git and XCode together?
One solution is to use the rebase command instead of a classic pull (fetch + merge).
You can check the option when doing a pull in XCode.
That would replay your local commits on top of the (fetched and updated) remote tracking branch.
In comamnd line, a nice option is:
fetch
try a merge
if not, try a rebase
See ""Merge remote-tracking branch..." What is this? I don't even. " and
"Why am I merging “remote-tracking branch 'origin/develop' into develop”?"
# download the latest commits
git remote update -p
# update the local branch
git merge --ff-only #{u}
# if the above fails with a complaint that the
# local branch has diverged:
git rebase -p #{u}

Git Working tree is dirty

If I execute git review git shows me "Working tree is dirty" error.
I made a commit and I sent to review. After that I update the branch from the upstream using git pull. Now I need to modify the previous commit message, so, there are my commands:
1) git reset <id-of-the-commit-to-modify>
2) git commit --amend
vim was opened to modify my commit. But here appears information about my commit and others commits as well and I don't know why. However, I modified the commit message and write/close vim.
3) git review
This command raise this error:
Errors running git rebase -i remotes/gerrit/master
doc/source/configuration.rst: needs update
doc/source/developing.rst: needs update
tools/sample_data.sh: needs update
Working tree is dirty
What I doing wrong?
git reset <id-of-the-commit-to-modify> without a mode option is defaulted to --mixed . This is what is said on the reset in the manual
--mixed
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
This basically means your index has been reset, but not your working files. So all files that you pulled from upstream are still left in your working tree.
You should use git reset --hard <id-of-the-commit-to-modify> This will reset your index and remove the files that came with your upstream pull. You can then amend your commit and send it for review.
once a change was pushed to Gerrit, then it can be fetched directly. On the Gerrit review board you can find the chekcout command for each patchset, something like this: git fetch ssh://ebalbar#gerrit.ericsson.se:29418/TCC/TitanSim refs/changes/86/129686/5 && git checkout FETCH_HEAD Then, you can amend the commit as usual, and push the new change again. AFter that checkout your local branch and reset it with the remote branch: git reset --hard origin/<remote_branch> also a nice answer how to modify a commit which was pushed directly.

Gerrit hook API

I'm using Gerrit Code review.
I have a problem with it. There are some hooks with Gerrit, one of them is:
patchset-created --change <change id> --change-url <change url>
--project <project name> --branch <branch> --uploader <uploader>
--commit <sha1> --patchset <patchset id>
Gerrit will call it if you push some change to Gerrit.
The commit parameter passed in is the sha1 commit number, but what I want is the commit log. For example, if I do git commit -m "id:110", I want to get id:110.
How can I get it? Is there any API I can use?
You can use git log or git show, run from within the correct repository. Your hook will be passed the repo via --repository. The hook will be called from a useful working directory (I just checked into this last week, I think it cd's into the repo before running the hook?)
git log $SHA1 or git show $SHA1 depending on which information you want.
That said, I'd prefer a better solution. I'm looking to add the diff / patchset to the information, and it is not available via any of the gerrit hooks.
i have solve the problem myself.it seems that gerrit has not such apis.
but , if patchset-created hook is called , the change parameter it passed in is usefull,
if refer to a private key of gerrit database table ,named Change ,every git push to gerrit it record such change to the Change table , include the commit message , so , you can use the change parameter to query the table , so got the commit message from the hook !

How to switch repositories from one to the next

I was using a github repository from a previous developer.
I am the only coder on this project, so I forked the project over to my own github repository.
Now I would like to commit soley to my repo.
Unfortunately, I realized that I never changed my .git/config , so I was still committing to the old repo. I just changed it to the appropriate url, and when I type :
$> git status
It returns :
=> Working directory clean.
But I know its not because I have several commits I've made. So my local box has different code then what it is pointed to on my repository.
My question is this. Obviously I'm halfway through the process of doing this. Do I need to re-fork to update, and then I'm good. Or is there a special command I need to run to let my local box know its 'git status' command is targeting a new repo to compare itself to? Equally, am I missing something else very important :D ?
Thank you everyone.
You can use git remote to manage your remote
rename origin
git remote rename origin old_origin
add a new origin
git remote add origin git://github.com/my/forked/repo.git
git fetch origin # will create all the remote branches references
# in your local repo
You can also easily setup a new upstream for your current master branch (git 1.7 and more):
git branch --set-upstream master origin/master
The "nothing to commit (working directory clean)" message of git status won't prevent you to push.
After changing the origin, you should see:
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by xxx commits.
#
nothing to commit (working directory clean)
That means you have some commits to push to your new origin.
Note: "git remote"(man) rename failed to rename a remote without fetch refspec, which has been corrected with Git 2.39 (Q4 2022).
See commit 5a97b38 (22 Sep 2022) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 20a5dd6, 10 Oct 2022)
remote: handle rename of remote without fetch refspec
Reported-by: John A. Leuenhagen
Signed-off-by: Jeff King
We return an error when trying to rename a remote that has no fetch refspec:
$ git config --unset-all remote.origin.fetch
$ git remote rename origin foo
fatal: could not unset 'remote.foo.fetch'
To make things even more confusing, we actually do complete the config modification, via git_config_rename_section().
After that we try to rewrite the fetch refspec (to say refs/remotes/foo instead of origin).
But our call to git_config_set_multivar() to remove the existing entries fails, since there aren't any, and it calls die().
We could fix this by using the "gently" form of the config call, and checking the error code.
But there is an even simpler fix: if we know that there are no refspecs to rewrite, then we can skip that part entirely.
git status only shows you the status of your working directory, not the entire repository. It seems like you only need to change the remotes that git push and git pull use by default. Open up .git/config and find your branch.<branch> entries and change them, as well as your remote.<remote> entries. For example, your master entry may look like this:
[branch "master"]
remote = origin
merge = refs/heads/master
Just change remote to reference your (forked) remote.
Also, your remote entry may look like the following:
[remote "myremote"]
url = git://github.com/me/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
You can add a push entry so that your master branch is pushed by default:
[remote "myremote"]
url = git://github.com/me/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
push = master
In whole, but I should include it in this answer. Is that before hand I manually altered my .git/config to include my new repository url. That's why I didn't have to rename or add any origin as Von suggested.
Then I just guessed this and performed
$> git fetch origin
Which returned
From git#github.com:gotoAndBliss/hq_channel
17326ca..043d395 master -> origin/master
* [new branch] panda_streaming -> origin/panda_streaming
+ 6ec9bf8...becbcc6 testing -> origin/testing (forced update)
Then on git status I got
# On branch testing
# Your branch is ahead of 'origin/testing' by 9 commits.
I git pushed origin testing, and I think I'm on target now.

Resources