Git push new branch as specific user picks up old

2019-07-25 14:45发布

问题:

In order to trigger some automation, I need to push to a gitlab repo as a specific user with a branch name following some specific formatting. For the sake of reference, let's call that user Joe Programmer <jp@company.com> and the branch name example-branch.

There's a pre-existing commit that I want to grab and push to gitlab, so I change my user

git config --local user.name "Joe Programmer"
git config --local user.email "jp@company.com"

delete the branch, if it exists, and push that delete to the repo as well

git push origin --delete refs/heads/example-branch
git branch -D example-branch

checkout from my known-good commit

git checkout good_commit

and branch from it

git checkout -b example-branch good_commit

then I commit with an empty change set and push to origin.

git commit -m "triggering automation" --allow-empty
git push origin example-branch

However when I look on Gitlab, I see that it's picked up not only my empty commit (as Joe Programmer) but also the previous commit I've called good_commit above, attributed to the actual person whose made that commit.

How do I most-easily omit good_commit, so the only thing that pushes to remote is my commit as Joe Programmer?

回答1:

One option, but probably not perfect, is to amend the last commit.

So instead of an empty commit, you will do:

git commit  --amend --no-edit --author "Joe Programmer <jp@company.com>"
git push origin example-branch

As far as I understood, you need a change of the author, so also added the --author

So the downside is the duplicate commits with the same content (but different hash) per branch, but maybe it's better than those empty "triggering automation" commits.



标签: git gitlab