How to set custom configuration for your local Git project repository

Suppose you do not want to have the same configuration for your local repository as you have your global Git settings. Or you need to fix identity information for commits that are already made in Git commit history. In that case, this article will show you a few Git commands on how to set up the custom configuration for your local Git repository or how to fix past mistakes.

Introduction

There are two different needs in the request of changing author for your commits. Let’s make clear what are you really looking for:

  • a) Either you want to change the author before making commit.
  • b) Or you want to change the author after you have made commit.

In this article, we will look at both ways how to change the commit’s author identity.

Change Future Commits Author

There are three different ways how to change identity for your Git committer. All the procedures only affect future commits and not the past commits made in the development line of your repository (commit history).

Git Config Check

Just a quick tip. To confirm your settings for global and local configuration you can type command:

git config --list

git config --list will show global system git settings. If you run it inside a repository, it will also attach and show you the local configuration.

However, using following command might be more clearer:

git config --list --show-origin

git config --list --show-origin command does all what git config --list does. Git command also attaches a path to the origin file of each config item in the list. Arguably, the attached path makes the settings look more understandable, and you can better connect items and their values for local and global Git settings.

Git Global Configuration

If you want to change your username and email by default for all project on your machine, edit global configuration settings. You can run the git config command with --global flag that will override your default Git settings and apply for all the future commits in your repositories. Git command for it looks like this:

git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "EMAIL@example.com"

Git Local Configuration

However, if you want to change your username and email only for single project, go to your repository and make git config adjustment like this:

git config --local user.name "FIRST_NAME LAST_NAME"
git config --local user.email "EMAIL@example.com"

Simply omit the --global flag and replace it with --local flag. git config --local makes the custom configuration valid only in the repository you made a change.

Author Identity For the Next Commit

There is at least one last way how to temporary influence the author identity of the commit. Just right before making the commit, add to commit command --author flag. When making the commit, you can override even local settings.

git commit --author="FIRST_NAME LAST_NAME "

Change Past Commits Author

Changing past commit authors in Git repositories is not so straightforward as it is for future ones. The issue lies in the fundamental idea of Git – its distributed asynchronous nature. You should be aware of one simple fact that you might cause a massive problem with synchronization if you work on the project with many team members.

When you change Git commit author identity, you create an entirely new commit with a new hash; you are rewriting commit history and development branch. And while for a single individual repository user, this might not be a problem, for big teams, this might be a cause of enormous evil.

Therefore, let me give you a piece of good advice: if you are working in a team, a change in Git commit history should be adequately communicated with the rest of the team. It might happen that other team members already based their work on the existing commits, and additional changes would cause huge synchronization problems. Communicate with your team if you feel that commits might be already used as the base for other people work. Eventually, for good or bad, it might be possible even to avoid changing commit author identity, and either do rollback commit with new commit with correct author commit or, better, leave the commit history as it is. Commit change is much more critical in the long term as a person who made it (empirical experience – people come and go from teams, code stays forever).

Git Ammend With Author Flag

git ammend command is used to patch last commit in Git’s commit history. By attaching --author flag to git ammend command you can effectively rewrite commit author identity for the last commit in commit history.

Git Interactive Rebase

Interactive rebase is a powerful feature of Git that allows you to edit Git commit history very effectively.

However, be careful with interactive rebase, it might shoot you in the foot if you will be not careful! Any changes made will affect your Git commit history. I would recommend to study deeper official Git documentation for interactive rebase before making any changes with it over console [7.6 Git Tools – Rewriting History]. Otherwise I can really recommend using interactive rebase with IDE tools such as IntelliJ.

First step, you need to find commit you consider as base for your interactive rebase. Provide to git command commit’s hash like this:

$ git rebase -i -p 9be2477a

git rebase command will open the interactive editor in which you need to mark all the commits you want to change. Selected commits will be marked with the red keyword “edit”.

edit 87bee32a Added new tests
edit 1be09dcb Merge branch with hotfix for DJ-1041
edit 9be2477a Change title tag for homepage
edit c427697c Add images support in gallery

Git will now walk you through each commit. You will have a chance to change every commit individually:

Stopped at 1be09dcb... Merge branch with hotfix for DJ-1041
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

Now is your chance to rewrite the commit author identity. Either you will use --amend with --author flag. Or you will continue to next commit in edit mode until you have edited all commits which were marked for edit

$ git commit --amend --author="FIRST_NAME LAST_NAME " --no-edit
$ git rebase --continue

Git filter-branch

Last option is to use Git filter-branch command. filter-branch will allow you to go through a large chunk of commits with the help of simple script. Let us show you a simple script:

$ git filter-branch --env-filter '
OLD_COMMITTER_EMAIL="OLD_EMAIL@example.com"
NEW_COMMITTER_NAME="FIRST_NAME LAST_NAME"
NEW_COMMITTER_EMAIL="NEW_EMAIL@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_COMMITTER_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$NEW_COMMITTER_NAME"
    export GIT_COMMITTER_EMAIL="$NEW_COMMITTER_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_COMMITTER_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$NEW_COMMITTER_NAME"
    export GIT_AUTHOR_EMAIL="$NEW_COMMITTER_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

However, same as with interactive rebase, it is necessary to be aware that by using filter-branch, you will create entirely new sort of commits and change Git commit history. The new commit history might have severe consequences when synchronizing with your team, so you should do this preferably only on repositories that haven’t been published or shared.

Conclusion

In summary, we divided the methods of changing the commit author identity into two different approaches based on the commit stage. We showed three different ways to change the author information before making a commit, and we explained another three ways to change the author information after making a commit.

Did you find commands easy? Do you have your trick or know another way how to change global and local Git settings? Let us know in the comments below the article. We would like to hear your ideas and stories.

This entry was posted in Tips & tricks and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.