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 (stage)
- Remove files from the index
- Reset index to last commit
- Restore or unstage files
- Record changes to the repository
- See changes from previous commit
- Go back to a 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
- List commits
- Switch branches
- Switch to a tag
- Save a temporary stage/index
- Remove a local branch
- Remove a remote branch
Create a git repository
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
Set name and email for the git user
git config user.name <username> git config user.email <email>
--globalto write this info in
Add file changes to the index (stage)
git add <file>
# To add all changes git add -A
Remove files from the index
git rm --cached <file>
Reset index to last commit
git reset HEAD
Restore or unstage files
git restore <file>
- This will restore an unstaged file (before adding it to index)
--stagedno unstage a file.
# Example 1 $ cat file original $ echo new >> file $ cat file original new $ git restore file $ cat file original
# Example 2 $ git add file $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: file $ git restore --staged file $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: file no changes added to commit (use "git add" and/or "git commit -a")
Record changes to the repository
git commit -m "<Brief description of the commit>"
See changes from previous commit
Go back to a previous commit
git reset <commit>
- By default, resets the index but not the working tree (same as using
--mixed, file changes are unstaged).
--hardto reset the index and the working tree (the files).
--softto not touch the index or the working tree (file changes need to be committed).
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 rev-list --remotes --pretty
- If only need commit IDs, do not include
Show commits newer than a specific date:
git log --since=2021-12-01
More parameters of
--author=<pattern>: filter by author.
-n <number>: limit the number of commits to output.
--grep=<pattern>: limit based on whether the log message matches a pattern. Add
-ifor case-insensitive search.
git checkout <branch>
git switch <branch>
Switch to a tag
git checkout tags/<tag>
Save a temporary stage/index
- When you want to record the current state of the working directory but want to go back to a clean state (the HEAD commit), this command will save your changes on a temporary stage and will restore your files to HEAD. Then, you can change some code, commit, and go back to your work with
git stash pop.
Remove a local branch
git branch -d <branch> # '-D' to force a deletion
Remove a remote branch
git push <remote> -d <branch>
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles: