Skip to content

Instantly share code, notes, and snippets.

@paulwongx
Last active December 1, 2023 18:45
Show Gist options
  • Save paulwongx/15760cb2ab5b199e7303cad194ccca3a to your computer and use it in GitHub Desktop.
Save paulwongx/15760cb2ab5b199e7303cad194ccca3a to your computer and use it in GitHub Desktop.
Git Commands

Git Commands

# Checking out a new branch
git checkout -b <branch-name>
git checkout -b feature/my-feature

# Commiting a branch
git add .
git commit -m "Your commit message here"
git push origin <branch-name>

# View branches
git branch
git branch -r #remote
git branch -a #all branches
git branch -v #view details

# Renaming a branch locally
git branch -m <old-branch-name> <new-branch-name>

# Renaming a branch remotely
git checkout -b <new-branch-name> <old-branch-name>
git push origin --delete <old-branch-name>
git push origin <new-local-branch-name>:<new-remote-branch-name>
git push -u origin <new-remote-branch-name>

# Set an upstream branch
git push -u origin <remote-branch-name> #--set-upstream
git branch -vv #verify upstream branch

# Merging with main
git checkout main
git pull origin main #pull latest changes
git merge <feature-branch>
git commit #resolve conflicts
git push origin main

# Deleting a feature branch
git branch -d <feature-branch>

# Staging a file
git add <file> #file
git add . #all

# Unstaging a file
git reset <file>
git reset #all

# Undo commits while keeping changes
git reset --soft <commit> #undo commits without deleting changes
git reset --mixed <commit> #undo commits and remove from staging
git reset --hard <commit> #undo commit, reset working directory, discard changes
git reset --soft HEAD~1 # goes back one commit from current commit

# Redo commit
git reflog
git reset --soft HEAD@{2} # Target the specific commit to go back to

# View branch pointer
git log --oneline

# Commit references <commit>
HEAD~n # Go back n commits from current commit e.g., HEAD~1
HEAD^ # Go back one commit from current commit. Same as HEAD~1
HEAD^^ # Go back to the grandparent commit of the current commit

## To remove a submodule (white arrow with folder when pushed to Github)
cd <folder_name>
rm -rf .git
git rm --cache .
cd ..
git add && git commit -m "latest" && git push origin main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment