What’s Git
Git is a free and open source distributed version control system.
This post serves as a set of notes to quickly refer to. For detailed explanations, see these excellent free resources:
- Pro Git by Scott Chacon and Ben Straub
- Happy Git and GitHub for the UserR by Jenny Bryan
Getting Going
Check to see if you have Git installed already:
- On Windows, open the command prompt and type
git --versionor search for the application Git Bash.- If you see
git version 2.43.0.windows.1or something similar, you’re good to go.
- If you see
git is not recognized as an internal or external command, you need to install Git.
- If you see
- On Mac, open the terminal and type
which gitorgit --version.- If you see something like
/usr/local/bin/gitorgit version 2.39.3 (Apple Git-145), you’re good to go.
- If you see
command not found, you need to install Git.
- If you see something like
To download or update Git
- go to https://git-scm.com/downloads and install like any other software
- to update on Mac with Homebrew:
brew upgrade git - to update on Windows:
git update git-for-windows
Tell Git who you are
- at the terminal (mac) or command prompt or git bash (Windows)
- type
git config --global user.name "your name"(can be your full name or github username) - type
git config --global user.email "your email"(should be the email you use with GitHub) - check the result with
git config --list
- type
Register a free GitHub account
- pick a username that is short, timeless, professional, and (preferably) all lowercase
Link your GitHub account and your local Git
- Git Credential Manager is the best way to store your GitHub credentials
- on Windows, GCM is included with Git for Windows
- on Mac, you can install GCM via Homebrew with
brew install --cask git-credential-manager
Git Commands
The basics
To copy down a GitHub repository to your local machine
git clone https://github.com/your-username/your-repo.gitto copy down a repository from GitHub to your local machinegit remote --verboseto check the remote was cloned successfully
To add a remote to an existing local repository
git remote add origin https://github.com/your-username/your-repo.gitto add a remote to an existing local repository with nickname origingit remote --verboseto check the remote was added successfullygit remote show originto see more details about the remotegit remote remove originorgit remote set-url origin _url_to remove or edit the url aliased by origin
Most common workflow commands
git add newdoc.txtto stage a file for a commitgit commit -m "a commit message"to commit all staged filesgit push origin mainto push local commits (on branch main) up to the remote repository (origin)git push -u origin mainto push local commits up to the remote repository and have local main track remote maingit pull origin mainto pull down the latest changes (on branch main) from the remote repository (origin)
Checking status
git statusto check on the state of your repositorygit log --onelineto see a list of commitsgit diffto see the changes you’ve made since the last commit
Branching
git branch [branch-name]to create a new branchgit checkout [branch-name]to switch to the branchgit checkout -b [branch-name]to create a new branch and switch to it in one commandgit checkout mainto switch back to the main branchgit merge [branch-name](when on main) to merge the branch into the main branch
Intermediate
Coming Soon