Cheatsheet โ
๐ Most Useful Git Commands โ
| Description | Command |
|---|---|
| Fetch changes from the remote and reapply local commits on top | git pull --rebase |
| Undo the latest commit, keeping changes staged | git reset HEAD~1 |
Modify the message of a specific commit, where n represents the number of commits from the current commit (HEAD) | git rebase -i HEAD~n Replace pick with reword for the desired commit, save, and exit. Then update the commit message. |
| Squash the last 3 commits into one interactive session | git rebase -i HEAD~3 Edit "pick" to "squash" for commits 2 and 3. If you mark one or more lines as "squash", they will be combined with the one above. pick 2598ef1 fix: password squash 8ff64e1 fix: modify public port squash 5a96fa8 fix: version gitlab-ci Edit the squash commit and force push to remote git push origin ${branch} -f |
By Category โ
๐ง Setup โ
| Description | Command |
|---|---|
| Set your name for Git | git config --global user.name "Your Name" |
| Set your email for Git | git config --global user.email "you@example.com" |
| Check your Git configuration | git config --list |
๐ Repository Management โ
| Description | Command |
|---|---|
| Initialize a new Git repository | git init |
| Clone an existing repository | git clone <repo_url> |
| Show the current status of the repository | git status |
| View commit history | git log |
| View concise commit history | git log --oneline |
๐งน Clean Up โ
| Description | Command |
|---|---|
| Remove untracked files | git clean -f |
| Remove unused remote-tracking branches | git prune |
| Optimize the repository | git gc |
โ๏ธ Staging and Committing โ
| Description | Command |
|---|---|
| Stage a specific file | git add <file> |
| Stage all changes | git add . |
| Commit staged changes with a message | git commit -m "Commit message" |
| Edit the last commit message | git commit --amend -m "Updated message" |
๐ Branch Management โ
| Description | Command |
|---|---|
| List all branches | git branch |
| Create a new branch | git branch <branch_name> |
| Switch to an existing branch | git checkout <branch_name> |
| Create and switch to a new branch | git checkout -b <branch_name> |
| Merge a branch into the current one | git merge <branch_name> |
| Delete a branch | git branch -d <branch_name> |
๐ Undoing Changes โ
| Description | Command |
|---|---|
| Discard changes in a working file | git restore <file> |
| Unstage a file | git reset <file> |
| Reset all changes to the last commit | git reset --hard HEAD |
| Revert a specific commit | git revert <commit_hash> |
๐ต๏ธ Viewing Differences โ
| Description | Command |
|---|---|
| Show unstaged changes | git diff |
| Show staged changes | git diff --staged |
| Compare two branches | git diff <branch_1> <branch_2> |
๐ Remote Repositories โ
| Description | Command |
|---|---|
| Link a remote repository | git remote add origin <repo_url> |
| View linked remote repositories | git remote -v |
| Remove a remote link | git remote remove origin |
๐ ๏ธ Advanced โ
| Description | Command |
|---|---|
| Stash changes without committing | git stash |
| List stashed changes | git stash list |
| Reapply stashed changes | git stash apply |
| Apply a specific commit to the current branch | git cherry-pick <commit_hash> |
| Reapply commits on top of another branch | git rebase <branch_name> |
๐ Tips โ
- Use a
.gitignorefile to exclude files from being tracked. - Use
git bisectto identify the commit that introduced a bug. - Combine
git logwith options like--graphor--decoratefor a more visual history.
Bonus โ
Configure SSH key to secure connection โ
- Generate public/private keys
bash
ssh-keygen -t ed25519 -C "mail@gmail.com"- Edit
~/.ssh/config
bash
# git clone git@github.com:username/myrepo
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
# If you have multiple user, configure like below
# git clone git@github.com-username:username/myrepo
Host github.com-username
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed22519_dedsxc
# git clone git@gitlab.local:username/myrepo
Host gitlab.local
Port 2222
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/gitlab_ed25519- In Github or Gitlab, add the generated public key
Sign commit with GPG โ
- Import public gpg keys in Git remote server (Github, Gitlab etc.)
bash
# Create GPG public & private key
gpg --full-gen-key
# Get GPG private key
gpg --list-secret-keys --keyid-format=long
# Output example:
## ------------------------------------
## sec rsa4096/XXXXXXXXXXXXXXXX 2016-03-10 [expires: 2017-03-10]
## uid Hubot <hubot@example.com>
## ssb rsa4096/XXXXXXXXXXXXXXXX 2016-03-10
# Export public gpg key associate to the sec ID
## sec rsa4096/XXXXXXXXXXXXXXXX
gpg --armor --export XXXXXXXXXXXXXXXX- Set private key in git client
bash
# Set the primary private key
git config --global user.signingkey XXXXXXXXXXXXXXXXTIP
Set a second private key by adding ! in the end git config --global user.signingkey XXXXXXXXXXXXXXXX!
- Configure gpg to sign all commit by default
bash
git config --global commit.gpgsign true- Configure gpg to sign 1 commit
bash
git commit -S -m "My commit message"Configure git for child folders โ
If you have multiple Git repositories with different users, you can configure each repository or folder to use a specific GPG key and user.
- Create
.gitconfig_includefile in~/topLevelFolder1/.gitconfig_include. The content of.gitconfig_includewill be like this:
[commit]
gpgsign = true
[user]
signingkey = <PRIVATE_KEY_ID>
name = Username
email = username@mail.com- In
~/.gitconfig, add the following in the end of the file:
[includeIf "gitdir:~/toplevelFolder1/"]
path = ~/topLevelFolder1/.gitconfig_includeNow all git project located in ~/topLevelFolder1/ will use .gitconfig_include configuration.
Related issue in StackOverflow.
