Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Last active May 22, 2023 12:11
Show Gist options
  • Save munkacsitomi/84f51a11c63afab9e92d3032fba0a822 to your computer and use it in GitHub Desktop.
Save munkacsitomi/84f51a11c63afab9e92d3032fba0a822 to your computer and use it in GitHub Desktop.
Useful Git commands
# get all branches
git fetch
# undo any unplanned changes
git reset --hard
# revert last commit
git reset --hard HEAD^
# reset to develop branch
git reset --hard origin/develop
# set the author name and email address to your commits
git config --global user.name "Your Name"
git config --global user.email your@email.com
# check current branches
git branch
# checkout an existing branch
git checkout main
# create and checkout a new branch
git checkout -b feature/new-branch-name
# set upstream and push from a new local branch
git push –-set-upstream origin feature/new-branch-name
# check current state
git status
# stage all changes
git add -A
# stage new files and modifications, without deletions (on the current directory and its subdirectories)
git add .
# stage modifications and deletions, without new files
git add -u
# add a commit message
git commit -m "Insert commit message here"
# merge origin main to feature branch
git merge origin/main
# delete local tag '12345'
git tag -d 12345
# delete remote tag '12345' (eg, GitHub version too)
git push origin :refs/tags/12345
# alternative approach
git push --delete origin tagName
git tag -d tagName
# force fetch tags
git fetch --tags -f
# delete branch locally
git branch -D localBranchName
# delete all branches locally except main master and develop
git branch | grep -v "main" | grep -v "master" | grep -v "develop" | xargs git branch -D
# delete branch remotely
git push origin --delete remoteBranchName
# check logs
git log --oneline
# log your commits
git log --author=yourUser
# check origin
git config --get remote.origin.url
# check origin fetch and push
git remote -v
# check more info about origin
git remote show origin
# change last commit message
git commit --amend -m "New commit message"
# force push changes to origin branch (useful if you already changed the commit message locally and want to change it in origin branch)
git push -f origin branch-name
# after you changed your last commit message you can push it
git push --force branch-name
# cherry picking multiple commits
git cherry-pick commit1 commit2
# delete stale references from the local copy
git remote prune origin
# fetch updates (i.e. re-sync with origin) and prune
git fetch --prune origin
# stash current changes include untracked
git stash -u
# apply changes from stash
git stash pop
# apply changes from stash without removing the stash from the stash stack
git stash apply
# apply specific changes from stash
git stash apply stash@{3}
# show all stashed items
git stash list
# clear all stashed items
git stash clear
# if you commmited a change to main but didn't push, follow the steps to add it to a new branch
git reset HEAD~1
git stash
git checkout anotherbranch
git stash pop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment