Git cheatsheet
January 6, 2023Git is one of the version control systems, and it's a prerequisite for development jobs. This post covers most of the git commands I use.
Configuration
- Set user configuration for every project if you use multiple accountsgit config user.name "<USERNAME>"git config user.email "<EMAIL_ADDRESS>"
- Use the current branch for push commandsgit config --global push.default current
SSH keys setup
Generate separate SSH keys for Github and Bitbucket with the following command, type the filename path and passphrase.
ssh-keygenRun the following commands to activate SSH keys
eval `ssh-agent -s`ssh-add ~/.ssh/id_rsa_githubssh-add ~/.ssh/id_rsa_bitbucket
Basic commands
- The repository setup
- Initialize a git repositorygit init
- Clone an existing repository# git clone <REPOSITORY_URL>git clone git@github.com:zsevic/pwa-starter.git
- Add the remote repository# git remote add <REMOTE_NAME> <REPOSITORY_URL>git remote add origin git@github.com:zsevic/pwa-starter.gitgit remote add upstream git@github.com:zsevic/pwa-starter.git
- Update the URL for the remote repository# git remote set-url <REMOTE_NAME> <REPOSITORY_URL>git remote set-url origin git@github.com:zsevic/pwa-starter.git
- Get a list of configured remote connectionsgit remote -v
- Initialize a git repository
- Branches
- Get a list of the branchesgit branch
- Create and switch to the new branchgit checkout -b new-branch
- Checkout to a specific commit and create a new branch out of itgit log # find a hash from a specific commitgit checkout <COMMIT_HASH>git switch -c <NEW_BRANCH_NAME>
- Switch to another branchgit checkout existing-branch
- Rename the current branchgit branch -m <NEW_BRANCH_NAME>
- Delete branchgit branch -D other-existing-branch
- Fetch all the remote branchesgit fetch --all
- Get a list of remote branches without cloning the repo or verify if the user has "read" accessgit ls-remote <REPOSITORY_URL>
- Get a list of the branches
- Get the status of the local changesgit status
- Add new changesgit add some-file.jsgit add .
- Commits
- Commit the changesgit commit -m "Commit message"
- Empty commit without any filesgit commit --allow-empty -m "Trigger CI pipeline"
- Commit the changes and skip running git hooksgit commit -m "Commit message" --no-verify
- Update the latest commit message and add new changes to the latest commitgit commit -m "Commit message" --amend
- Commit the changes
- Push the changes to the remote repository
- Push the changes to the current branch when the current branch is configured as the default onegit push
- Push the changes to the remote branch# git push <REMOTE_NAME> <BRANCH_NAME>git push origin master
- Force push the changes to the feature branch# git push <REMOTE_NAME> <FEATURE_BRANCH_NAME>git push origin feature-branch -f
- Push the changes to the current branch when the current branch is configured as the default one
- Fetch and merge remote changes to the local branch# git pull <REMOTE_NAME> <BRANCH_NAME>git pull origin master
- Remove (unstage) the changes from the local stagegit reset some-file.jsgit reset
- Differences between commits
- Get a difference compared to the latest commitgit diff some-file.jsgit diff
- Get a difference between the last two commitsgit diff HEAD^ HEAD# orgit diff HEAD HEAD~1
- Get a difference compared to the latest commit
- Revert the file changesgit checkout -- some-file.js
- Merge the specified branch into the current onegit merge <BRANCH_NAME>
- Revert specific commit. The following command creates a new commitgit revert <COMMIT_HASH>
Miscellaneous
Resets
- Soft reset (commits are removed, but changes from the removed commits are staged)# git reset --soft HEAD~{NUMBER_OF_COMMITS_TO_SOFT_REMOVE}git reset --soft HEAD~2
- Hard reset (both commits and changes are removed)# git reset --hard HEAD~{NUMBER_OF_COMMITS_TO_HARD_REMOVE}git reset --hard HEAD~1 # equal as git reset --hard HEAD^
- Get the latest remote changes when pulling doesn't workgit reset --hard origin/<BRANCH_NAME>
- Soft reset (commits are removed, but changes from the removed commits are staged)
Stashing
git add .git stash save <STASH_NAME>git stash listgit stash apply --index 0Tags
- Remove the following tag locallygit tag -d v0.13.29
- Remove the following tag locally
Find removed commits
git refloggit checkout <COMMIT_HASH>Remove the initial commit
git update-ref -d HEADPatching
- Create a patch from the latest commits# git format-patch -{NUMBER_OF_COMMITS}git format-patch -1
- Apply the patchesgit apply 0001-latest-commit.patch
- Create a patch from the latest commits
Git submodules
- Add git submodule# git submodule add -- <REPOSITORY_URL> <DIRECTORY_PATH>git submodule add -- git@github.com:zsevic/pwa-starter.git template
- Retrieve the latest changes for the git submodule# git submodule update --remote <DIRECTORY_PATH>git submodule update --remote template
- Resolve conflict in the submodule# git reset HEAD <DIRECTORY_PATH>git reset HEAD templategit commit
- Add git submodule