git init #
Initialize a Git directory as a repository.
git init
git add #
Track files with Git (A.K.A. Git now knows and cares about these files)
git add <filename>
or add everything in the folder
git add .
git commit #
Commit staged files to the repository history.
git commit -m "your message here"
or interactive commit message
git commit
git remote #
Set up a remote repository (e.g. GitHub, GitLab, etc.) Most SCM providers will give you the command!
git remote add origin <url>
View current remotes:
git remote -v
git push #
Push commits to a remote repository.
git push
Push and set the upstream tracking reference (first push of a branch):
git push -u origin <branch-name>
After -u is set once, future pushes from that branch only need:
git push
git branch, git checkout, and git switch #
Create a new branch:
git branch <branch-name>
Switch to an existing branch:
git checkout <branch-name>
or (newer syntax):
git switch <branch-name>
Create and switch in one step:
git checkout -b <branch-name>
or (newer syntax):
git switch -c <branch-name>
List Branches:
git branch
Remove a branch:
git branch -d <branch-name>
git pull and git fetch #
Fetch any branches from origin to your local working copy:
git fetch
Pull any upstream changes into your working copy:
git pull
git checkout #
Check out a specific commit (detached HEAD state — you're looking, not working):
git checkout <commit-hash>
Check out a specific branch:
git checkout <branch-name>
Git Configuration Options #
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "nvim"
Git Stages #
| Stage/State | Description |
|---|---|
| Untracked | File exists but Git has never been told about it. |
| Staged (Indexed) | git add was run for this file and Git is now tracking it. |
| Modified (Unstaged) | A committed file was edited. Git sees the difference but it is not staged yet. |
| Committed | The file has been committed with git commit and is now part of the repo history. |
| Ignored | Git has been instructed to ignore the file via .gitignore. |
Git SCMs #
Think of a Pull Request (PR) more as a "Merge Request". Some SCM platforms like Gitlab specifically call them MRs.