Skip to content

Cheatsheet โ€‹

๐Ÿ“‹ Most Useful Git Commands โ€‹

DescriptionCommand
Fetch changes from the remote and reapply local commits on topgit pull --rebase
Undo the latest commit, keeping changes stagedgit 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 sessiongit 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 โ€‹

DescriptionCommand
Set your name for Gitgit config --global user.name "Your Name"
Set your email for Gitgit config --global user.email "[email protected]"
Check your Git configurationgit config --list

๐Ÿ“‚ Repository Management โ€‹

DescriptionCommand
Initialize a new Git repositorygit init
Clone an existing repositorygit clone <repo_url>
Show the current status of the repositorygit status
View commit historygit log
View concise commit historygit log --oneline

๐Ÿงน Clean Up โ€‹

DescriptionCommand
Remove untracked filesgit clean -f
Remove unused remote-tracking branchesgit prune
Optimize the repositorygit gc

โœ๏ธ Staging and Committing โ€‹

DescriptionCommand
Stage a specific filegit add <file>
Stage all changesgit add .
Commit staged changes with a messagegit commit -m "Commit message"
Edit the last commit messagegit commit --amend -m "Updated message"

๐ŸŒ Branch Management โ€‹

DescriptionCommand
List all branchesgit branch
Create a new branchgit branch <branch_name>
Switch to an existing branchgit checkout <branch_name>
Create and switch to a new branchgit checkout -b <branch_name>
Merge a branch into the current onegit merge <branch_name>
Delete a branchgit branch -d <branch_name>

๐Ÿš‘ Undoing Changes โ€‹

DescriptionCommand
Discard changes in a working filegit restore <file>
Unstage a filegit reset <file>
Reset all changes to the last commitgit reset --hard HEAD
Revert a specific commitgit revert <commit_hash>

๐Ÿ•ต๏ธ Viewing Differences โ€‹

DescriptionCommand
Show unstaged changesgit diff
Show staged changesgit diff --staged
Compare two branchesgit diff <branch_1> <branch_2>

๐Ÿ”— Remote Repositories โ€‹

DescriptionCommand
Link a remote repositorygit remote add origin <repo_url>
View linked remote repositoriesgit remote -v
Remove a remote linkgit remote remove origin

๐Ÿ› ๏ธ Advanced โ€‹

DescriptionCommand
Stash changes without committinggit stash
List stashed changesgit stash list
Reapply stashed changesgit stash apply
Apply a specific commit to the current branchgit cherry-pick <commit_hash>
Reapply commits on top of another branchgit rebase <branch_name>

๐ŸŒŸ Tips โ€‹

  • Use a .gitignore file to exclude files from being tracked.
  • Use git bisect to identify the commit that introduced a bug.
  • Combine git log with options like --graph or --decorate for a more visual history.

Bonus โ€‹

Configure SSH key to secure connection โ€‹

  1. Generate public/private keys
bash
ssh-keygen -t ed25519 -C "[email protected]"
  1. Edit ~/.ssh/config
bash
# git clone [email protected]:username/myrepo
Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

# If you have multiple user, configure like below
# git clone [email protected]:username/myrepo
Host github.com-username
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed22519_dedsxc

# git clone [email protected]:username/myrepo
Host gitlab.local
  Port 2222
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/gitlab_ed25519
  1. In Github or Gitlab, add the generated public key

Sign commit with GPG โ€‹

  1. 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 <[email protected]>
## ssb   rsa4096/XXXXXXXXXXXXXXXX 2016-03-10

# Export public gpg key associate to the sec ID 
## sec   rsa4096/XXXXXXXXXXXXXXXX
gpg --armor --export XXXXXXXXXXXXXXXX
  1. Set private key in git client
bash
# Set the primary private key 
git config --global user.signingkey XXXXXXXXXXXXXXXX

TIP

Set a second private key by adding ! in the end git config --global user.signingkey XXXXXXXXXXXXXXXX!

  1. Configure gpg to sign all commit by default
bash
git config --global commit.gpgsign true
  1. 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.

  1. Create .gitconfig_include file in ~/topLevelFolder1/.gitconfig_include. The content of .gitconfig_include will be like this:
[commit]
        gpgsign = true
[user]
        signingkey = <PRIVATE_KEY_ID>
        name = Username
        email = [email protected]
  1. In ~/.gitconfig, add the following in the end of the file:
[includeIf "gitdir:~/toplevelFolder1/"]
    path = ~/topLevelFolder1/.gitconfig_include

Now all git project located in ~/topLevelFolder1/ will use .gitconfig_include configuration.

Related issue in StackOverflow.

Released under the MIT License.