3

From my book, I am trying to learn how to simulate multiple git users on my local system itself. I will pretend to be all those multiple users. I followed my book's instructions to simulate multiple users committing changes to a repo. The book output shows two different people when git log is executed. But, my output shows only one user, that is me. How do I make more users so that I can simulate the scenario ?

commit aed341198f614860bfb68f5fd5845f191773fa36
Author: sid smith <bob.smith@aol.com>
Date:   date

    Bobs first commit after changing the first line

commit edabfcc8a432b07f92a564147ee6ebb8865f5d18
Author: sid smith <sid.smith@aol.com>
Date:   date

    Base commit from source

Edit - Is there a web-based git where I can login (as multiple users) through multiple browsers to simulate multiple commits in git ?

sid smith
  • 533
  • 1
  • 6
  • 18
  • To undo the changes I made, I simply used `git reset --soft "HEAD^"` as mentioned here - http://stackoverflow.com/questions/927358/how-to-undo-the-last-git-commit – sid smith Jun 07 '14 at 06:26

1 Answers1

2

You need to change your user.name and user.email when you commit.

To do so without affecting your git config, you can set environment variables for a specific commits in order to do that commit "as someone else".

GIT_AUTHOR_NAME="anotherName" GIT_AUTHOR_EMAIL="another@email" \
GIT_COMMITTER_NAME="anotherName" GIT_COMMITTER_EMAIL="another@email" \
git commit -m "commit done as another person"

That will apply only for the current commit.
All the others will be done with the user.name and user.email values you see in git config --global --list.

See the section "Environment Variables" of the git manpage for all the variable you can set.

Another way is to override the config on the git command with the -c option of the git command:

git -c user.name="anotherName" -c user.email="another@email" commit -m "..."

(lowercase '-c', not uppercase '-C', which is another option)

That is easier to set as an alias, which in Windows is called doskey:

dokey gituser1=git -c user.name="anotherName" -c user.email="another@email" $*

(the $* is to get all the othe parameters you will pass to that command)

You would use that as:

gituser1 commit -m "commit done as another person"

I get an error

C:\repo>
GIT_AUTHOR_NAME="Bob" GIT_AUTHOR_EMAIL="bob.smith444.bob@aol.com" 
  'GIT_AUTHOR_NAME' is not recognized as an internal or external command, 
  operable program or batch file.

Indeed, Windows shell wouldn't support that syntax directly.

As mentioned in "Setting environment variable for just one command in Windows cmd.exe", you would need to type:

cmd /C "set GIT_AUTHOR_NAME=\"anotherName\" 
&& set GIT_AUTHOR_EMAIL=\"another@email\" 
&& set GIT_COMMITTER_NAME=\"anotherName\" 
&& set GIT_COMMITTER_EMAIL=\"another@email\" 
&& git commit -m \"commit done as another person\""

(one giant line)

NOTE - In windows/cmd, you must remove all the backslashes and the last double quote to make this work.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • oh ! So i set the username and e-mail each time I play a new user ? – sid smith Jun 07 '14 at 06:13
  • @sidsmith yes, that is the idea. You can even make an alias to simplify the command. – VonC Jun 07 '14 at 06:35
  • I get an error - `C:\repo>GIT_AUTHOR_NAME="Bob" GIT_AUTHOR_EMAIL="bob.smith444.bob@aol.com" 'GIT_AUTHOR_NAME' is not recognized as an internal or external command, operable program or batch file.` – sid smith Jun 07 '14 at 06:44
  • I am not able to run the command. How do I run it ? Also, how do I make an alias. I am new to git and I have never heard of alias. Please tell me. Thanks. – sid smith Jun 07 '14 at 06:46
  • @sidsmith I have found another way to set a different user on commit: see my edited answer (`-c` option), and its associated alias (`doskey` in Windows) – VonC Jun 07 '14 at 07:11
  • I get an error - error: pathspec 'commit' did not match any file(s) know to git. To fix this error, you need to remove all the back slash \ from your answer and also the last double quote. Thanks. It works now. – sid smith Jun 07 '14 at 17:09
  • @sidsmith what command are you typing, and are you within the git repo when you are typing it? – VonC Jun 07 '14 at 17:10
  • I am within bob's repo, which is actually a bare repo or copy of sid smiths repo. This command worked - `cmd /C "set GIT_AUTHOR_NAME="bob" && set GIT_AUTHOR_EMAIL="bob.smith444.bob@aol.com" && set GIT_COMMITTER_NAME="bob" && set GIT_COMMITTER_EMAIL="bob.smith444.bob@aol.com" && git commit -m "Bobs first commit after changing the first line"` – sid smith Jun 07 '14 at 17:14
  • Now, `git config --global --list` shows only sid smith. I wonder why it does not show bob smith as well. What does that command mean ? – sid smith Jun 07 '14 at 17:17
  • Btw, is there a way to reduce the size of the command that works ? I don't want to edit,copy-paste it each time i play a new user. Thanks. – sid smith Jun 07 '14 at 17:19
  • 1
    @sidsmith your global config includes your identity used by default for any commit. A shorter version of the command that works uses the `-c` options I mention in my answer. Or again, you can try and alias it with doskey. – VonC Jun 07 '14 at 17:22
  • Thanks. I tried to do a pull, as Bob, using your command and replacing the git command at end by git pull. I don't see any message. How do I know if it worked ? My book shows an output `already up-to-date.` I am thinking if I should not this on my local system. Its so confusing and complicated. Is there a web-based git where I can login (as multiple users) through multiple browsers to simulate multiple commits in git ? – sid smith Jun 07 '14 at 19:36
  • 1
    @sidsmith yes, a pull "as a different user" won't do anything different from a regular pull. So if you local repo is already up-to-date, a different pull won't change that. – VonC Jun 07 '14 at 19:38
  • Thanks VonC. This multi-user on one system looks complicated. Instead of that, is there a web-based git where I can login (as multiple users) through multiple browsers to simulate multiple commits in git ? – sid smith Jun 07 '14 at 19:40
  • @sidsmith it is actually very simple. Git has no notion of "user database", so user.name and user.email is the only way to include user identity into the repo history. There is no "webapp" on a local level, since Git is distributed across network and user base. There are webapps in a centralized server though, for git repo management, like http://demo.gitlab.com/ or http://try.gogits.org/. – VonC Jun 07 '14 at 19:43
  • 1
    @sidsmith You can actually log on GitHub and doing commits as multiple users if the repo allows those different users to do so (meaning if you have added those account as "collaborators") – VonC Jun 07 '14 at 19:44
  • Can I also do the same in bit bucket ? If yes, then how do I use bitbucket to simulate a realistic multiple user scenario ? – sid smith Jun 08 '14 at 00:01
  • @sidsmith sure: http://blog.bitbucket.org/2013/05/14/edit-your-code-in-the-cloud-with-bitbucket/, so simply create multiple BitBucket account, push a repo and use the other account to modify. But remember, Git is a *distributed* VCS (Version Control System) which isn't made to be used centrally. – VonC Jun 08 '14 at 04:01
  • Thanks VonC. Page says nothing found. Please tell me what this means - Git is isn't made to be used centrally. I am new, so I need help to understand this. – sid smith Jun 08 '14 at 05:21
  • 1
    @sidsmith http://blog.bitbucket.org/2013/05/14/edit-your-code-in-the-cloud-with-bitbucket/ (`http://blog.bitbucket.org/2013/05/14/edit-your-code-in-the-cloud-with-bitbucket/`) – VonC Jun 08 '14 at 05:28
  • 1
    @sidsmith on DVCS: http://stackoverflow.com/a/2563917/6309 and http://stackoverflow.com/q/2704996/6309 – VonC Jun 08 '14 at 05:29
  • Thanks for helping me patiently. I think I am ready to sail now. – sid smith Jun 08 '14 at 05:33