Skip to content

Instantly share code, notes, and snippets.

@kairusds
Created June 26, 2025 06:02
Show Gist options
  • Save kairusds/5aef0b04068487505753ee7a077ac3f4 to your computer and use it in GitHub Desktop.
Save kairusds/5aef0b04068487505753ee7a077ac3f4 to your computer and use it in GitHub Desktop.
Commands I use for Git
# Using HEAD as the commit hash is equal to using the
# latest remote commit's hash.
# Revert file to commit hash or the remote's latest version
# of the file
git checkout HEAD -- filepath/file
git checkout <hash> -- filepath/file
# View the diff for staged files(files used with `git add`)
git diff --staged
# Clone repo with submodules
git clone --recursive REPO_URL
# Don't clone the entire history of a repo to save space
git clone --depth 1 REPO_URL
# Allows merging of remote and local when `git pull` finds
# conflicts in commit history
git config --global pull.ff true
# Unstage all files
git reset HEAD
# Reset local files state to the specified commit hash
git reset --hard <hash>
# Force push if there's conflicting commit history between
# local and remote like hard resets.
git push -f
# Use an editor for setting a commit message after staging
git commit
# Quick one-liner commit message without opening the editor
git commit -m "message"
# Can be run after git add
git commit -am "message"
# Edit previous commit message
git commit --amend
# List repository remotes
git remote -v
# Clone a single specific tag/branch
git clone <url> -b <branch/tag> --single-branch
# Update commits from upstream
# "master" is the default branch name
git remote add upstream [email protected]:ORIGINAL_OWNER/REPOSITORY.git
git fetch upstream
git checkout master
git merge upstream/master
# Revert a commit without resetting the history
git revert COMMIT_HASH
# Delete all commit histories and start all over from scratch
git checkout --orphan new
# Add all
git add -A
git commit -am "message"
# Delete old branch, replace with your actual branch name
git branch -D branchname
# Rename current branch to master or something else
git branch -m master
# Force push with the new branch to origin
git push -f origin main
@Aikoawawa
Copy link

thanks gayman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment