Git is a version control system. It is used to track changes in files and to collaborate with other developers.
Table of content
sudo apt install gitDownload the installer from git-scm.com.
Download the installer from git-scm.com.
git config --global user.name "John Doe"
git config --global user.email "john.doe@example.com"# clone using SSH
git clone git@github.com:USER/REPOSITORY.git
# clone using HTTPS
git clone https://github.com/USER/REPOSITORY.git
# clone using Personnal Access Token
git clone https://TOKEN@github.com/USER/REPOSITORY.gitNote: All provided examples use Github as a remote repository. You can use any other remote repository like Gitlab or Bitbucket.
# create a new directory
mkdir my-project
# go into the directory
cd my-project
# create some files
touch README.md
echo "# My Project" >> README.md
# initialize the repository
git init# Add all files
git add .
# Add a specific file
git add file.txtgit commit -m "Commit message"git fetchgit pushgit pullCreate new branch from the main
# checkout main
git checkout main
# ensure to be up to date
git pull
# create a new branch
git checkout -b branch-nameSwitch to a branch
git checkout branch-nameMerge a branch
# let's suppose we are on the feat/my-feature branch
# checkout main
git checkout main
# ensure to be up to date
git pull
# merge the branch
git merge feat/my-featureDelete a branch
git branch -d branch-nameList branches
git branch
# List remote branches
git branch -r
# List all branches
git branch -aList commits
# basic
git log
# List commits with diff
git log -p
# List commits with diff and stats
git log -p -stat
# List commits with diff and stats and graph
git log -p -stat --graphRevert a commit
# Revert the last commit
git revert HEAD
# Revert a specific commit
git revert COMMIT_HASHReset a commit
Warning: This command is destructive. It will remove all commits after the specified commit.
# Reset the last commit
git reset --hard HEAD
# Reset a specific commit
git reset --hard COMMIT_HASH# List tags
git tag
# Create a tag
git tag v1.0.0 -m "Version 1.0.0"
# Push a tag
git push origin v1.0.0# Rebase the current branch on the main branch
git rebase main
# Squash 3 last commits into one
git rebase -i HEAD~3# Stash all changes
git stash
# Stash specific files
git stash push file1.txt file2.txt
# List stashes
git stash list
# Pop the last stash
git stash pop
# Apply a stash
git stash apply stash@{0}
# Delete a stash
git stash drop stash@{0}# Diff between the working directory and the staging area
git diff# Show the status of the working directory
git status# Remove untracked files
git clean -f# Show the origin
git remote show origin
# Change the origin
git remote set-url origin NEW_URL