Git: a version control system
Git is a free and open-source distributed version control system you can use from your terminal with simple and easy-to-understand commands.
All commands need to be executed inside your project’s main folder.
Table of Contents
- Create a git repository
- Clone an existing repository
- Get repository status
- Set name and email for the git user
- Add file changes to the index
- Reset last index
- Record changes to the repository
- See changes from previous commit
- Add a remote
- Push changes to the remote repository
- Fetch changes from the remote repository
- Fetch and pull changes from the remote repository
Create a git repository
git init
Clone an existing repository
git clone <url>
git clone https://github.com/user/repo.git
- This will create a directory with the project files
Get repository status
git status
Set name and email for the git user
git config user.name <username>
git config user.email <email>
- Add
--global
to write this info in~/.gitconfig
instead ofYOUR_PROJECT_FOLDER/.git/config
.
Add file changes to the index
git add <file>
# To add all changes
git add -A
Reset last index
git reset HEAD
Record changes to the repository
git commit -m "<Brief description of the commit>"
See changes from previous commit
git diff
Add a remote
git remote add <name> <url>
git remote add github-repo https://github.com/user/repo.git
- If you’ve cloned a repo, you don’t need to add a remote unless you don’t want to use the remote you’ve cloned.
Push changes to the remote repository
# Default branch is "master"
# If you've cloned a repository, default remote name will be "origin"
git push <remote-name> <branch>
git push github-repo master
Fetch changes from the remote repository
git fetch [<remote-name>]
Fetch and pull changes from the remote repository
git pull
Featured content: