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

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 of YOUR_PROJECT_FOLDER/.git/config.

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)
  • Add --staged no 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

git diff

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).
  • Add --hard to reset the index and the working tree (the files).
  • Add --soft to 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 pull

List commits

git log
git rev-list --remotes --pretty
  • If only need commit IDs, do not include --pretty.

Show commits newer than a specific date:

git log --since=2021-12-01

More parameters of git log:

  • --author=<pattern>: filter by author.
  • -<number>, -n <number>: limit the number of commits to output.
  • --grep=<pattern>: limit based on whether the log message matches a pattern. Add -i for case-insensitive search.

Switch branches

git checkout <branch>
git switch <branch>

Switch to a tag

git checkout tags/<tag>

Save a temporary stage/index

git stash
  • 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.