Question

I get a very strange behaviour. I have a bash script that runs on Bamboo, which sets the desired git user, commits some changes, then pushes them to the master branch.

The problem is that, the commit is done with the correct user, but the push is done with a different user.

The commit looks something like:

USERX authored commit 7d365...

And then the push:

USERY pushed to master at user/repo 

The commit and push are done one after another after setting the username and email, so why is this happening, what should I look for? Here's part of my bash script:

git config user.name "$bamboo_GIT_USER_NAME"
git config user.email "$bamboo_GIT_USER_EMAIL"

git add -v CHANGELOG.rst || exit 3
git commit -v -m "Updated CHANGELOG.rst" || exit 3
git push origin "$bamboo_GIT_BRANCH" -v || exit 3
Was it helpful?

Solution

Commits are actual things (specifically, "objects") that live inside the git repository. They contain text strings giving the author and committer. Once written into an object, these are permanent and unchangeable.

  • Aside: git commit --amend does not change them; instead, it makes a new, different commit, with different data in the commit, and points the branch-head to the new commit:

    ... - E - F - G   <-- master
    

    If you then git commit --amend --author='someone else <someone@else.where>', git abandons the old tip commit (G), adding a new commit (G'):

    ... - E - F - G   [abandoned]
                \
                  G'  <-- master
    

When you do a git push, this does something very different.

Let's say, for instance, you first git fetch some commits written by someone else, from a repository at git://else.where/path/to/repo.git. Or, you write your own commits with user.name and user.email set to some unusual values.

  • Aside: your example code has a git add but no git commit.

Then you git push these commits (written by someone else, or committed with odd user.name and user.email) to ssh://mylogin@some.place/different/path/to/repo.git.

Here, you package up the commits you retrieved, and deliver them to your other server. But it's you (specifically, mylogin) logging in via ssh to some.place, delivering the commits; the commits you deliver are the permanent, unchangeable repository objects you retrieved from else.where, or made under the settings you put in user.name and user.email.

Note that git does not authenticate you or log you in to the host some.place. It's ssh that authenticates you and logs you in, as you (or rather, mylogin@some.place). Your ssh session then runs the git command that receives the git objects you send. If you ask, on some.place, what user is running ssh, you get mylogin: that's who logged in via ssh. This is completely disconnected from the author and committer names stored in the commits being uploaded.

OTHER TIPS

It's supposed to be like that.

To change the author of the last commit:

$ git commit --amend --author='somebody <somebody@example.com>'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top