Git without fear · Chapter 1 of 28

Who are you?

By the end of this chapter you can tell Git who you are, and put your own name on a commit that went in under the wrong one. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Day one
  • 4 tasks
  • Git
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

Why you need this

Bunty started at Tapri yesterday, and his first commit says it was made by root <root@localhost>. Meera wants to know who added bun maska to the menu, and the history cannot tell her. On a team of six that is a mystery; on a team of sixty, with GitHub linking every commit to an account, it is a real problem.

Every commit records an author, a name and an email, and Git takes them from its settings. On a fresh laptop there are none, so Git either refuses to commit or uses whatever it can guess, like root. Setting them now does not help Bunty's commit either: a commit that exists keeps the author it was made with.

The lesson

No identity yet

This lab happens in the terminal. It opens in this chapter's repository, with Bunty's commit already in it. You type Git commands, then press Check: it looks at the repository and at the commands you ran.

On this fresh laptop Git knows nothing about you. Ask for your name and it prints nothing at all. Try to commit and Git stops:

Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

Set your name and email

Do what Git asks, with your real name and your Tapri email (GitHub uses the email to link commits to your account):

git config --global user.name "Asha Rao"
git config --global user.email "asha@tapri.in"
git config --global --list
user.name=Asha Rao
user.email=asha@tapri.in

git config --global user.email on its own reads one setting back.

Where the settings live

--show-origin prints the file each setting comes from:

git config --global --list --show-origin
file:/home/student/.gitconfig	user.name=Asha Rao
file:/home/student/.gitconfig	user.email=asha@tapri.in

--global writes to .gitconfig in your home folder, ~/.gitconfig for short. Without --global, a setting goes into the repository's own .git/config and wins there, which is handy when one laptop has a work and a personal identity. Interviewers like asking where these files are.

Put your name on Bunty's commit

Your new settings did not touch the history:

git log --format='%h %an <%ae> %s'
17df318 root <root@localhost> add bun maska
8280f7b Meera <meera@tapri.in> Add the menu

The classic mistake is a plain git commit --amend --no-edit. It makes the commit again but keeps the old author, and Git even says so:

[main 5a9fe5c] add bun maska
 Author: root <root@localhost>

Add --reset-author, which takes the author from your current settings (--no-edit keeps the message as it is):

git commit --amend --reset-author --no-edit
[main 9d14065] add bun maska
 1 file changed, 1 insertion(+)

Your hash will differ: it depends on your name and the time you amend. Press Next to watch what amend really does:

Amend never edits a commit. It makes a new one in its place, with a new hash, so amend only commits you have not pushed: anyone who has the old one would now have a different history. This repository has never been pushed, so it is safe here. If anything goes wrong, lab reset builds the chapter again.

Your turn

Your four tasks are at the top, and the terminal prints them too (lab tasks shows them again). Keep Bunty's message and his change as they are: only the author changes. The last task is an interview warm-up about where --global settings are kept.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Set your name for every repository

    Tell Git your name, so every commit you make shows it.

    1. Use git config with the --global option. It saves a setting for every repository on this machine.

    2. Set user.name to your own name. Put the name in double quotes if it has a space.

    3. Read the setting back to see what you saved.

    To pass: the global user.name is your own name. root and Tapri Intern do not count.

  2. Task 2

    Set your Tapri email

    Tell Git your email too. Git saves it next to your name in every commit.

    1. Use git config with --global again.

    2. Set user.email to an address that ends in @tapri.in, for example asha@tapri.in.

    3. List your global settings to see your name and email.

    To pass: the global user.email ends in @tapri.in and has no spaces.

  3. Task 3

    Put your name on Bunty's commit

    The last commit, add bun maska, was made as root. Make it again with you as its author.

    1. Do tasks 1 and 2 first. Git takes the new author from your settings.

    2. Run git log and look at the author of each commit.

    3. Make the last commit again with git commit --amend. Ask Git to reset the author and to keep the message.

    4. Run git log again to see your name on it.

    To pass: the last commit's author has your global name and email. There are exactly two commits: Add the menu, then add bun maska. Keep the message add bun maska, and keep Bun maska 50 in menu.txt.

  4. Task 4 · interview question

    Interview: where --global settings live

    Interview question: which file does git config --global write to?

    1. List your global settings with the option that shows where each one comes from.

    2. Each line starts with file: and then a path. Find that path.

    3. Save only that path in answer.txt, in the chapter folder. The short form that starts with ~ is fine too.

    To pass: answer.txt has the path of the global settings file, and nothing after it.

What you will learn

  • git config --global: your name and email, for every repository on this machine.
  • Where settings live: the file --global writes to, and how to see it.
  • git commit --amend --reset-author: make the last commit again, with you as its author.

More in Day one

Build "Who are you?" yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.