Now let's learn how to use Git locally from our computer. While there are GUIs (graphical user interfaces) for Git on your computer like GitHub Desktop, we're going to use the command line to learn Git. That's because this is a key transferrable skill that applies to any kind of coding.
- Learning objective: Build a "mental model" of what your computer is doing.
- Git - version control software
- Repository - a folder containing your files and also containing a structure that helps keep track of changes in those files. When you intialize a repository, git creates a hidden folder (
.git
folder) that stores the changes to those files. - GitHub - a place to host git repositories and collaborate
- Local Repository - the version of a git repository on your local computer
- Remote Repository - the version of a git repository stored somewhere else that your local repository is connected to (frequently on GitHub)
- Commit - the basic unit of a git repository is a commit. It is a set of changes to a file. A commit usually comes with an id as well as a commit message that describes the change.
Within a Repository you have
- Untracked Changes - files that are in your folder but that git doesn't pay attention to.
- Staging Area - a place where you can put files before you commit them. Once files are in the staging area, git is paying attention to them.
- Commit Log (aka Git History) - all of the commits (previous changes) to all of the files in your repository.
- The working directory
git init
creates a git repo inside current working directory. This means that this command can turn a regular folder into a git repository by generating a hidden.git
folder that starts to keep track of changes.git clone
takes a git repo from somewhere else and makes a copy of that repo into your current working directory. We will frequently be cloning repos from GitHub.
- The staging area
git add .
adds changes from the working directory to the staging areagit add <filename>
adds changes to filenames specified from the working directory to the staging area
- The commit
git commit -m "commit message"
adds changes in staging area to the repositorygit log
shows
Protip: Run git status
after each command in the beginning because it allows you to visualize what just happaned.
- github - a service that hosts git remote repositories, and provides a web app to interact / collaborate on them
- remote - another repository that can be syncronized with a remote
- upstream - the name for a remote read-only repository
- origin - the name for a remote read-and-write repository
- clone - download an entire remote repository, to be used as a local repository
- fetch - downloading the set of changes (commits) from a remote repository
- pull - fetching changes and merging them into the current branch
- In order to show your remotes, you can run
git remote -v show
. The default remote is named "origin" - In order to push, you run
git push
. By default this will push from the branch you are on to a remote branch with the same name. (If you'd like to specify a branch, you can do that. The full formulation of this command isgit push <remote> <branch>
. So, for example you might saygit push origin main
to push to the "main" branch of the "origin" remote.)
Let's give it a try! We're going to clone a repository for a simple website from GitHub down to our computer where we can work with it locally. We will make some edits to the code, commit those changes and then push the changes back up to the remote repository in GitHub.