tutorial, git,

Git Best Practices: Cheat Sheet dan Workflow Summary

Kiki/🎮🍉⌨️🍩💻 Kiki/🎮🍉⌨️🍩💻 Oct 02, 2026 · 11 mins read
Git Best Practices: Cheat Sheet dan Workflow Summary
Share this

Selamat! Kamu telah menyelesaikan 25 artikel tentang Git dan GitHub mulai dari basic sampai advanced topics. Artikel terakhir ini adalah ringkasan best practices dan cheat sheet yang bisa kamu jadikan referensi cepat sehari-hari.


GitHub Flow adalah workflow simple dan cocok untuk kebanyakan tim:

main (production-ready)
  └── feature/login-page (development)
      └── merge via Pull Request + code review

Step-by-Step:

1. Clone repository:

git clone https://github.com/username/repo.git
cd repo

2. Buat branch baru:

git checkout -b feature/login-page

3. Kerjakan fitur, commit berkala:

git add src/Login.js
git commit -m "feat: Add login form UI"

4. Push branch ke remote:

git push -u origin feature/login-page

5. Buat Pull Request di GitHub

6. Code review, diskusi, dan revisi

7. Merge PR ke main (via GitHub UI)

8. Pull update main ke lokal:

git checkout main
git pull

9. Hapus branch lokal:

git branch -d feature/login-page

Commit Messages Best Practices

Gunakan Conventional Commits format:

<type>(<scope>): <subject>

<body>

<footer>

Types:

  • feat: Fitur baru
  • fix: Bug fix
  • docs: Update dokumentasi
  • style: Format code (tidak mengubah logic)
  • refactor: Refactor code
  • test: Tambah atau update test
  • chore: Update build tools, dependencies

Contoh:

git commit -m "feat(auth): Add JWT token validation"
git commit -m "fix(api): Fix null pointer exception in user endpoint"
git commit -m "docs: Update README with setup instructions"

Template Commit Message:

Create .gitmessage template:

# Type: feat|fix|docs|style|refactor|test|chore
# Scope: auth|api|ui|database|etc
# Subject: imperative, lowercase, no period at end
# --- 50 chars limit ---

# Body (optional):
# Explain what and why, not how
# --- 72 chars limit ---

# Footer (optional):
# Closes #123
# Breaking Change: ...

Set sebagai default:

git config --global commit.template ~/.gitmessage

Branching Strategy

Branch Naming Convention:

feature/<feature-name>    # Fitur baru
bugfix/<bug-description>  # Bug fix
hotfix/<urgent-fix>       # Production hotfix
release/<version>         # Staging release

Contoh:

git checkout -b feature/user-authentication
git checkout -b bugfix/fix-login-redirect
git checkout -b hotfix/critical-security-patch

Git Commands Cheat Sheet

Setup & Config

# Set username dan email
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

# Set default editor
git config --global core.editor "code --wait"

# Alias shortcuts
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.cm commit

Repository Basics

# Init repository
git init

# Clone repository
git clone https://github.com/user/repo.git

# Clone with shallow history
git clone --depth 1 https://github.com/user/repo.git

# Clone specific branch
git clone -b develop https://github.com/user/repo.git

Working with Files

# Check status
git status

# Add files
git add file.txt
git add src/
git add .

# Commit
git commit -m "commit message"

# Add and commit in one command
git commit -am "commit message"

# Amend last commit
git commit --amend -m "new message"

# Unstage file
git reset file.txt

# Discard changes
git restore file.txt
git checkout -- file.txt

Branching

# List branches
git branch
git branch -a  # include remote

# Create branch
git branch feature-x

# Switch branch
git checkout feature-x
git switch feature-x

# Create and switch
git checkout -b feature-x
git switch -c feature-x

# Delete branch
git branch -d feature-x
git branch -D feature-x  # force delete

# Rename branch
git branch -m old-name new-name

Merging & Rebasing

# Merge branch
git checkout main
git merge feature-x

# Merge with no fast-forward
git merge --no-ff feature-x

# Abort merge
git merge --abort

# Rebase
git rebase main

# Rebase interactive
git rebase -i HEAD~3

# Abort rebase
git rebase --abort

# Continue rebase after resolving conflicts
git rebase --continue

Remote Operations

# Add remote
git remote add origin https://github.com/user/repo.git

# List remotes
git remote -v

# Fetch updates
git fetch
git fetch origin

# Pull (fetch + merge)
git pull
git pull origin main

# Pull with rebase
git pull --rebase

# Push
git push
git push origin main

# Push new branch
git push -u origin feature-x

# Force push (dangerous!)
git push --force
git push --force-with-lease  # safer

Histori & Logs

# View log
git log
git log --oneline
git log --graph --oneline --all

# View specific file history
git log file.txt

# View changes
git diff
git diff HEAD~1 HEAD
git diff main..feature-x

# View commit details
git show commit-sha

# Search commits
git log --grep="bug fix"
git log --author="John"

Undo & Reset

# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

# Undo file to specific commit
git checkout commit-sha -- file.txt

# Revert commit (create new commit)
git revert commit-sha

# Clean untracked files
git clean -fd

Stash

# Stash changes
git stash
git stash save "work in progress"

# List stashes
git stash list

# Apply stash
git stash apply
git stash apply stash@{1}

# Apply and drop
git stash pop

# Drop stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

Tags

# Create tag
git tag v1.0.0

# Create annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"

# List tags
git tag

# Push tag
git push origin v1.0.0

# Push all tags
git push --tags

# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0

Advanced Commands

# Cherry-pick commit
git cherry-pick commit-sha

# View reflog
git reflog

# Garbage collection
git gc --aggressive --prune=now

# Find who changed a line
git blame file.txt

# Search in code
git grep "search term"

# Bisect (find bug commit)
git bisect start
git bisect bad HEAD
git bisect good commit-sha

GitHub-Specific Tips

Pull Request Best Practices:

✅ Keep PR small (< 400 lines) ✅ Write descriptive title dan description ✅ Add screenshots untuk UI changes ✅ Link related issues: Closes #123 ✅ Request specific reviewers ✅ Respond to review comments

GitHub CLI:

# Install gh
brew install gh  # macOS
choco install gh  # Windows

# Login
gh auth login

# Create PR
gh pr create --title "Add login feature" --body "Description"

# List PRs
gh pr list

# View PR
gh pr view 42

# Checkout PR locally
gh pr checkout 42

# Merge PR
gh pr merge 42 --squash

Security Best Practices

1. Never Commit Secrets

Use .gitignore:

.env
*.key
*.pem
secrets.yml

Check for leaked secrets:

git log --all -- .env

Remove committed secret:

git filter-repo --path .env --invert-paths

2. Sign Commits dengan GPG

# Generate GPG key
gpg --gen-key

# List keys
gpg --list-secret-keys --keyid-format=long

# Configure Git
git config --global user.signingkey YOUR_KEY_ID
git config --global commit.gpgsign true

# Signed commit
git commit -S -m "Signed commit"

3. Enable 2FA di GitHub

✅ Settings → Security → Two-factor authentication → Enable


Productivity Tips

1. Git Aliases

Add to ~/.gitconfig:

[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    unstage = reset HEAD --
    last = log -1 HEAD
    visual = log --graph --oneline --all
    amend = commit --amend --no-edit

Usage:

git co main
git br -a
git visual

2. Shell Prompt with Git Branch

For Bash: Add to ~/.bashrc:

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\u@\h \W \[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

For Zsh: Use Oh My Zsh with git plugin enabled.


3. VSCode Git Integration

✅ GitLens extension: Enhanced Git visualization ✅ Git Graph: Visual commit history ✅ Source Control panel: Built-in Git UI


Troubleshooting Common Issues

1. Merge Conflict

# View conflicts
git status

# Edit files, remove conflict markers
<<<<<<<, =======, >>>>>>>

# Mark as resolved
git add file.txt

# Complete merge
git commit

2. Accidental Commit to Wrong Branch

# Reset commit (keep changes)
git reset --soft HEAD~1

# Switch to correct branch
git checkout correct-branch

# Commit again
git commit -m "commit message"

3. Forgot to Pull Before Push

# Pull with rebase
git pull --rebase

# Resolve conflicts if any

# Push
git push

4. Need to Edit Old Commit Message

# Interactive rebase
git rebase -i HEAD~3

# Change "pick" to "reword" for commit to edit
# Save and close editor

# Edit commit message in new editor

# Force push (if already pushed)
git push --force-with-lease

Final Checklist

✅ Always pull before starting work ✅ Create branch untuk setiap fitur ✅ Commit small dan sering ✅ Write descriptive commit messages ✅ Review own changes sebelum commit ✅ Run tests sebelum push ✅ Create PR untuk code review ✅ Merge hanya setelah approval ✅ Delete branch after merge ✅ Keep main branch production-ready


Kesimpulan

Git adalah tool yang powerful, tapi bisa overwhelming untuk pemula. Dengan mengikuti best practices dan workflow yang konsisten, Git akan menjadi alat yang sangat membantu dalam development sehari-hari.

Key Takeaways dari 25 Artikel:

  1. Git Basics: Init, add, commit, log, diff
  2. Branching: Branch, merge, rebase, stash
  3. Remote: Clone, push, pull, fetch
  4. GitHub: Fork, PR, code review, organizations
  5. Advanced: Hooks, submodules, monorepo, reflog
  6. Optimization: GC, LFS, shallow clone, sparse checkout

Selamat! Kamu sekarang punya fondasi kuat untuk menggunakan Git dan GitHub seperti pro. Happy coding! 🚀


Resources:

Kiki/🎮🍉⌨️🍩💻
Written by Kiki/🎮🍉⌨️🍩💻
hello I'm friend K.