Skip to content

Latest commit

 

History

History
326 lines (263 loc) · 4.97 KB

File metadata and controls

326 lines (263 loc) · 4.97 KB

Git Commands Reference

This document contains frequently used Git commands for common development tasks.

Basic Setup

Configure Git User

git config --global user.name "[name]"
git config --global user.email "[email]"

View Configuration

git config --list

Branch Management

Create a New Branch

git branch <branch-name>

Create and Switch to New Branch

git checkout -b <branch-name>
# or (Git 2.23+)
git switch -c <branch-name>

List Local Branches

git branch

List All Branches (Local + Remote)

git branch -a

Switch to a Branch

git checkout <branch-name>
# or (Git 2.23+)
git switch <branch-name>

Delete a Local Branch

git branch -d <branch-name>
# Force delete (if not merged)
git branch -D <branch-name>

Delete a Remote Branch

git push origin --delete <branch-name>

Rename a Branch

# Rename current branch
git branch -m <new-branch-name>
# Rename specific branch
git branch -m <old-branch-name> <new-branch-name>

Staging and Committing

Check Status

git status

Stage All Changes

git add .

Stage Specific File

git add <file-path>

Unstage Changes

git reset <file-path>

Commit Changes

git commit -m "<type>(<scope>): <subject>"

Commit with Body and Footer

git commit
# Opens editor for multi-line commit message

Amend Last Commit

git commit --amend
# Without changing commit message
git commit --amend --no-edit

View Commit History

git log
# One line per commit
git log --oneline
# With graph visualization
git log --graph --oneline --all

Remote Management

List Remote Repositories

git remote -v

Add Remote Repository

git remote add <remote-name> <repository-url>

Remove Remote Repository

git remote remove <remote-name>

Rename Remote

git remote rename <old-name> <new-name>

View Remote Details

git remote show <remote-name>

Pushing and Pulling

Push to Remote

git push <remote-name> <branch-name>
# Example
git push origin feature/src_v2_app_comments

Push All Branches

git push <remote-name> --all

Push with Upstream Tracking

git push -u <remote-name> <branch-name>

Pull from Remote

git pull <remote-name> <branch-name>

Fetch from Remote (without merging)

git fetch <remote-name>

Force Push (Use with Caution)

git push --force <remote-name> <branch-name>
# Safer alternative
git push --force-with-lease <remote-name> <branch-name>

Stashing

Stash Changes

git stash
# With description
git stash save "<description>"

List Stashes

git stash list

Apply Stash

git stash apply
# Apply specific stash
git stash apply stash@{0}

Pop Stash (Apply and Remove)

git stash pop

Delete Stash

git stash drop stash@{0}

Merging and Rebasing

Merge Branch

git merge <branch-name>

Rebase Current Branch

git rebase <branch-name>

Abort Merge/Rebase

git merge --abort
git rebase --abort

Viewing Changes

View Unstaged Changes

git diff

View Staged Changes

git diff --staged

View Changes in Specific File

git diff <file-path>

View Changes Between Branches

git diff <branch-1> <branch-2>

Undoing Changes

Discard Changes in Working Directory

git checkout -- <file-path>
# or (Git 2.23+)
git restore <file-path>

Discard All Changes

git reset --hard HEAD

Revert a Commit (Create new commit)

git revert <commit-hash>

Reset to Previous Commit

# Soft reset (keep changes staged)
git reset --soft HEAD~1
# Mixed reset (keep changes unstaged)
git reset --mixed HEAD~1
# Hard reset (discard changes)
git reset --hard HEAD~1

Useful Aliases

Add these to your Git config for faster commands:

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --graph --oneline --all'

Common Workflows

Create and Push a New Feature Branch

git checkout -b feature/src_v2_app_comments
# Make changes
git add .
git commit -m "feature(src_v2_app_comments): add new feature"
git push -u origin feature/src_v2_app_comments

Update Branch from Main

git fetch origin
git rebase origin/main
# or merge
git merge origin/main

Clean Up Local Branches

# Delete merged branches
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

Sync Fork with Upstream

git fetch upstream
git checkout main
git merge upstream/main
git push origin main