5

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

2 Answers 2

6

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 <[email protected]>', 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://[email protected]/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, [email protected]). 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.

Sign up to request clarification or add additional context in comments.

5 Comments

I've corrected my question, forgot to add the commit command. Okay, so what should I do in this case to prevent pushing with a different user name?
Git doesn't have a user name in pushing, as git doesn't care. Other things—ssh, for instance, or programs that log the user who is invoking them—are what care. You need to find out how to get those other programs to believe that you are someone that you're not. This has all kinds of security implications, of course: what's to stop some random person on the Interweb from pretending to be you? However, the simplest thing in many shell scripts is to override $USER in the environment (which must be done outside of git—again, git itself doesn't care, and doesn't do that).
Alright. And if these commands are run by Bamboo, how can I check/where should I look to see which user executes the commands and thus is considered the one who pushed the commits?
I've never used Bamboo and know nothing of its own internals, so can't really help there.
Okay. Thank you for pointing me in the right direction, I'll investigate further how can I solve the Bamboo problem.
0

It's supposed to be like that.

To change the author of the last commit:

$ git commit --amend --author='somebody <[email protected]>'

1 Comment

It is not supposed to be like that, why is it? I set the user name and email and expect both the commit and push to be executed by that user, why does it change?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.