Set up Git
- Lesson 2
- 20 min
- Module 1: Understand a repository
Lesson 2 of 15 · Git and Team Development EssentialsSet up Git
1. Understand a repository
2. Work with branches and remotes
3. Collaborate through reviews
4. Resolve and recover
5. Release and hotfix practice
What you will be able to do
Make Git ready to use: install it, give it your name, and make your first repository. After this lesson, Git can record your work in the ticket-notes project.
Before you startWhat to know and to have ready
What you need to know
- How to open a terminal and move between folders with
cd, from Terminal bridge: Worked example: find your place and move.
What you need on your computer
- A terminal. On Windows, use Git Bash, which you installed in Terminal bridge: Get a terminal.
- The folder
projectsin your home folder. You made it in Terminal bridge: Guided practice: make the practice folder. If it is missing, runmkdir ~/projects. - No practice lab is necessary for this lesson, because you make
ticket-notesfrom nothing in Step 3: make a repository. If it breaks, delete it withrm -rf ~/projects/ticket-notes, then do Step 3 again.
Tested with Git 2.54 on macOS, Windows (Git Bash) and Linux. Any Git version from 2.40 works for this lesson.
A repository is a folder with a history
Section titled “A repository is a folder with a history”A repository (or “repo”) is a project folder that Git tracks. It looks like a normal folder, with one addition: a hidden folder called .git. Git keeps the full history of the project in .git.
You do not open or change the .git folder yourself. You use Git commands, and Git updates .git for you. If you delete .git, the folder becomes a normal folder again, and the history is gone.
A repository can exist in more than one place:
- A local repository is on your computer. You work in it, and Git does not need the internet for this work.
- A remote repository (or “remote”) is a copy of the same repository in a different place. Usually it is on a server such as GitHub. Your team shares work through the remote.
Git does not copy your work to a remote automatically. You send and receive changes when you decide to. You learn how in Module 2.
Step 1: check that Git is installed
Section titled “Step 1: check that Git is installed”-
In the terminal, run:
Terminal window git --version -
If a window asks you to install the “command line developer tools”, select Install. When the installation is complete, run
git --versionagain.
You installed Git in the previous lesson. In Git Bash, run:
git --version-
In the terminal, run:
Terminal window git --version -
If the output is
command not found, install Git. On Ubuntu or Debian, runsudo apt install git. On Fedora, runsudo dnf install git. Then rungit --versionagain.
The output shows a version number. Yours can be different:
git version 2.54.0Step 2: tell Git who you are
Section titled “Step 2: tell Git who you are”Each time you save a change, Git records who made it. Give Git your name and email address once. The --global option means “use this for every repository on this computer”.
-
Set your name. Replace
Your Namewith your name, and keep the quotes:Terminal window git config --global user.name "Your Name" -
Set your email address. Replace the example address with yours:
Terminal window git config --global user.email "you@example.com" -
Set the name of the first branch in new repositories to
main:Terminal window git config --global init.defaultBranch mainA branch is a line of work in a repository. You learn about branches in Module 2. Most teams now call the first branch
main. Some Git versions usemasterif you do not set this. -
Check your settings:
Terminal window git config --global --listThe output shows your values:
user.name=Amira Khanuser.email=amira@example.cominit.defaultbranch=mainGit shows the names in lowercase. This is normal.
If the output has a mistake, run the same git config command again with the correct value. The new value replaces the old one.
Step 3: make a repository
Section titled “Step 3: make a repository”-
Move to your projects folder:
Terminal window cd ~/projects -
Make a repository called
ticket-notes:Terminal window git init ticket-notesGit makes the folder and the
.gitfolder inside it:Initialized empty Git repository in /Users/amira/projects/ticket-notes/.git/ -
Move into the repository:
Terminal window cd ticket-notes -
List all the files, including hidden ones. The
-aoption means “all”:Terminal window ls -a. .. .gitThe
.gitfolder is there. On some systems, the names are on separate lines..means “this folder” and..means “the folder above”. -
Ask Git about the state of the repository:
Terminal window git statusOn branch mainNo commits yetnothing to commit (create/copy files and use "git add" to track)You use
git statusoften. It is the safest command in Git, because it only shows information and changes nothing.
Worked example: clone a repository
Section titled “Worked example: clone a repository”To clone means to make a full copy of a repository, with its history. When you join a team, you usually clone the team’s repository from a server.
You can also clone from a folder on your own computer. This shows how local and remote repositories connect, without an account on a server. Amira clones ticket-notes into a second folder:
cd ~/projectsgit clone ticket-notes ticket-notes-copyCloning into 'ticket-notes-copy'...warning: You appear to have cloned an empty repository.done.The warning is expected. The original repository has no saved changes yet, so the copy is empty too.
Next, she moves into the copy and asks Git where its remote is. The -v option means “verbose”: show more detail.
cd ticket-notes-copygit remote -vorigin /Users/amira/projects/ticket-notes (fetch)origin /Users/amira/projects/ticket-notes (push)The copy remembers where it came from. origin is the name that Git gives to the remote that you cloned from. For the copy, ticket-notes is the remote. On a team, origin is usually an address on a server, for example https://github.com/team/ticket-notes.git.
The original ticket-notes has no remote. Run git remote -v in it: the output is empty.
Your turn: inspect the two repositories
Section titled “Your turn: inspect the two repositories”Answer these questions with commands, not from memory:
- In
ticket-notes-copy, which branch are you on? Usegit status. - Does
ticket-notes-copyhave a.gitfolder of its own? Usels -a. - Does the original
ticket-noteshave a remote? Usegit remote -v.
When you finish, delete the copy. You do not use it again, and a new clone is easy to make. Go up to projects, then delete the folder:
cd ~/projectsrm -rf ticket-notes-copyShow the answers
main. A clone uses the same first branch as the original.- Yes. Every clone is a full repository, with its own
.gitfolder and its own history. - No.
git remote -vshows nothing inticket-notes, because you made it withgit init, not withgit clone.
Common mistake
Not a git repository
Symptom: you run a Git command, and Git shows this error:
fatal: not a git repository (or any of the parent directories): .gitCause: the working directory is not in a repository. Git looks for a .git folder in the working directory and in each folder above it. It did not find one. Usually you are in the wrong folder.
Fix:
- Run
pwdto see where you are. - Move into the repository, for example with
cd ~/projects/ticket-notes. - Run the Git command again.
A related mistake: you ran git init in your home folder. Now git status in your home folder lists all your files as untracked. To fix it, go to your home folder and delete only the .git folder there with rm -rf ~/.git. Check the command before you run it. Your files stay, and only the new, empty history is removed. Do not do this in a real project, because it deletes that project’s history.
Check your understanding
Amira runs git status in /Users/amira/projects. Git shows 'fatal: not a git repository'. But ticket-notes is a repository. Why does she get the error?
Show the answer
The projects folder is not a repository. The repository is the ticket-notes folder inside it. Git looks in the working directory and the folders above it, never in the folders below it. She must run cd ticket-notes first.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Practical check: show that Git knows your name and email address, and that ticket-notes is a repository with no commits.
Show the answer
Run git config --global --list: the output shows your user.name and user.email. Then run cd ~/projects/ticket-notes and git status: the output shows On branch main and No commits yet.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- A repository is a folder with a hidden
.gitfolder, which keeps the history. - You set your name and email address once with
git config --global. git initmakes a new repository.git clonecopies a repository that exists, with its history.- A local repository is on your computer. A remote is a copy in another place. Git connects them only when you tell it to.
git statusshows the state of a repository and changes nothing.
Next, you save your first change in ticket-notes.
Next: Track a first change