Unit 7: Git
Table of Contents
Git
Git is a distributed version control system (VCS). Basically Git is Google Docs for code - it manages the different versions of your files. More specifically, it allows you to keep track of changes, go back to older edits, and collaborate with other coders.
Note: Git is not the same thing as GitHub, as we will soon learn.
Install Git
- Download Git for your operating system and follow the instructions from the installation wizard
- After installing, enter
git --version
into the terminal to verify that Git is ready to use. It should print the Git version you just installed on your computer.
Terms You Should Know
Term | Definition |
---|---|
repository (repo) | Place where your project’s code is stored and tracked by Git |
local | The version of a repository on your computer |
remote | The version of a respository on the internet |
master | Default branch that is created; is the “main” branch |
README.md | A markdown (.md ) file that talks about your repo (optional, but highly recommended) |
Git Commands
All Git commands begin with git
.
If you ever need help with using a Git command, you can enter the command and add the -h
(for help) flag to view different options and the command’s syntax.
To view the Git manual for that command, add the --help
flag.
Note: Example output is example output, so what you see on your terminal may differ from what is displayed here.
Configuring Git
git config
Use git config
to configure your user settings. You should set the global username and email to your name and email by using the commands below.
Replace <name>
with your name in quotes and <email>
with your email in quotes.
$ git config --global user.name <name>
$ git config --global user.email <email>
Example
$ git config --global user.name "Jane Doe"
$ git config --global user.email "janedoe@example.com"
The user name and email that you set above will be associated with all of the commits you make on your computer.
You may also find it useful to turn on automatic colorization of Git command line output by using the command:
$ git config --global color.ui auto
To list all current config settings, you can use the command:
$ git config --list
Getting Started
git init
Use git init
to initialize a Git repo in the current directory.
$ git init
Initialized empty Git repository in /Users/janedoe/Desktop/project-directory/.git/
On a conceptual level, what this does is it sets up tracking for all of the subdirectories and files in the project directory. Any modification that is made, whether it’s adding 1000 lines of code, deleting a file, renaming a file, or adding a single space to a line of code, will be tracked.
git status
Use git status
to show the current status of a Git repo. It usually displays staged files, unstaged files, number of commits ahead/behind, and more.
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Another helpful thing about git status
is that it gives you helpful hints about different Git commands you can use depending on the current status of your repository. For example, this is what a git status
might look like while I am trying to resolve a merge conflict.
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: message.txt
no changes added to commit (use "git add" and/or "git commit -a")
Using this output, I know that I can use git merge --abort
if I want to abort the merge.