Git
Authors: Daniel Cui, Ryan KimWhat is Git?
Git is a version control system that tracks and manages the changes made to computer files throughout development, commonly used in software projects for collaboration, coordinating work, and maintaining a history of the source code.
Installing
Install Git from here. It may already be installed if you are using Mac or Linux.
If using Windows, install it with the installer.
- You can go with most of the defaults in the setup, but you look into some of the other options. This step-by-step guide works if you are just starting out.
- After installing, make sure you are using the Git Bash as your terminal (instead of the command prompt) or you have Git added to your PATH.
If using Mac,
homebrew
is a great way to install packages like Git.If using Linux, you can use the preferred package manager for installing, like
apt
for Debian orpacman
for Arch.- but seriously if you are using Linux, you already know this, don’t you?
You can also use a GUI like GitHub Desktop along with Git instead of a CLI, but I suggest against it. Learning to use the CLI is pretty important.
How It Works
A Git repository is simply just a directory on your computer where you tell it to track files with git init
.
When you add files in the directory, they are untracked. You can add them to the staging area with git add
,
and once you’ve made enough changes for a commit, you can write a snapshot of the current state of the directory
to the commit history with git commit
.
Often, git repos are stored in a remote location like on GitHub, where you can git push
your own commits to and git pull
changes that other collaborators have made.
Before making any commits, you will need to run the following commands in the terminal to set your Git config. This sets the metadata about the author for all commits.
git config --global user.name "<your_name>"
git config --global user.email "<your_email>"
A .gitignore
file tells Git to not track certain files in the current repository. Useful
in not committing installed packages like node_modules
or .venv
and outrageously large files.
Git uses a .git
hidden directory to track and maintain the repository. If you delete it,
you wipe you commit history, and you will have to re-initialize the directory to use Git.
There’s a lot more to Git like with branching, rebasing, tags, etc., but you should learn the basics first. This is a pretty good video tutorial, and there are many other pretty great resources online.
Common Git Commands
init
initializes a git repository in the current directorystatus
prints the status/current state of the Git repositoryadd
adds files to the staging areacommit
commits all staged files into a commit in the commit historylog
prints the commit historyremote
creates, views, or deletes connections to remote repospull
/push
updates local or remote repostash
temporarily stashes changes in your working directory to reapply laterbranch
creates a new branch in the reposwitch
switches branchesmerge
merges branchescheckout
checks out branches or past commitsrevert
/reset
/rebase
destructive commands
Note: if you can’t figure it out, just Google it! Also, the docs are here!