0

Please don't ask why, but I have 2 GitHub accounts. I'll call these two accounts GH1 and GH2.

The repository: The repository is private, it is in an organization, and the only members of the organization are GH1 and GH2. It is a repository that I don't want linked to my main, GH1, which is why I want there to be no trace of GH1 on that repository, and why I am only pushing to it as GH2.

The repository is being used by me to test workflows, before I deploy them to one of the public repositories of the organization.

When I push to the repo locally (as in, git push), my commit is attributed to GH2, but when I check the workflow runs, it says that it was pushed by GH1. How can I make sure that the commit is always attributed to GH2?

To clarify: The commit is attributed to GH2, the incorrect attribution to GH1 is in the GitHub Actions workflow runs.

Incorrect GitHub actions workflow attribution

(It has been modified with inspect element to redact sensitive information.)

riQQ
  • 9,878
  • 7
  • 49
  • 66
lemuria
  • 59
  • 1
  • 9
  • How are you authenticating when pushing to the repo? With a PAT? Are you using a PAT of user GH1? – riQQ Jan 17 '22 at 17:58
  • I just perform a `git push`, with GH2 author information in the local repository `.git/config`. I also have the GitHub CLI set up, and SSH set up. – lemuria Jan 17 '22 at 18:04
  • Are you using the SSH credentials of user GH1? Or is your GitHub CLI somehow authenticated as GH1? – riQQ Jan 17 '22 at 21:03
  • @riQQ Should I instead push with SSH instead? As in, `git@github.com:GH2/user/repo.git` (or maybe I got the URL wrong) – lemuria Jan 18 '22 at 05:55
  • Please edit your post to include the output of `git remote -v` in your repo. Do you get prompted for user name and password when pushing? – riQQ Jan 18 '22 at 18:10
  • I'm not prompted for a username and password when pushing. I've set up pushing via SSH instead, that should resolve it, hopefully. (https://stackoverflow.com/a/70751161/16977177) – lemuria Jan 19 '22 at 05:17

3 Answers3

0

There are two past solutions on Stack Overflow for fixing your situation.

  1. For making future commits use the correct user:
  1. For changing past commits to swap a user with another:

(Which is part of this question: How to amend several commits in Git to change author)

Part 1:

Set your git author settings. Eg:

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Then reset the author for all commits after the given SHA

git rebase -i YOUR_SHA -x "git commit --amend --reset-author -CHEAD"

This will pop up your editor to confirm the changes. All you need to do here is save and quit and it will go through each commit and run the command specified in the -x flag.

You can also change the author while maintaining the original timestamps with:

git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <new_address@example.com>' -CHEAD"

Part 2:

git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
     GIT_AUTHOR_EMAIL=correct@email;
     GIT_AUTHOR_NAME="Correct Name";
     GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
     GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all

I would read the details from the links to make sure you understand what you're doing before executing the actions.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • I've set the git author settings, and the repositories I'm committing to as GH2 are fresh, with no activity from GH1. Basically, the incorrect attribution is in the actions page that show workflow runs, specifically in: "[WORKFLOW] #0: Commit [SHA] pushed by [USERNAME]" – lemuria Jan 17 '22 at 16:19
  • See the comments from https://stackoverflow.com/a/25815116/7058266, which include the edge cases. You can edit those past commits. – Michael Mintz Jan 17 '22 at 16:26
  • Do you understand what I mean? It's in the actions page showing workflow runs, and the solutions there don't seem helpful for what I need. – lemuria Jan 17 '22 at 16:33
0

Git : fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists

Try pushing with SSH instead. I'd create 2 keys for GH1 and GH2, and then edit my SSH config to something like this:

Host github.com-GH2
        HostName github.com
        User GH2
        IdentityFile ~/.ssh/github_GH2

and in the repositories where I want to be attributed as GH2, I can simply run this git command:

git remote set-url origin git@github.com-GH2:user/repo.git

I did a push with that SSH url set, and I'm proud to say that the GitHub Actions workflow runs no longer attributes the commit to GH1 (see below screenshot).

Finally, correct GitHub attribution

lemuria
  • 59
  • 1
  • 9
0

I found a git identity manager. Hopefully I can use this to change between GH1 and GH2, but I haven't tried it yet for GitHub Actions.

https://github.com/samrocketman/git-identity-manager

lemuria
  • 59
  • 1
  • 9