Setup

Check Git Version

git --version

Set Username

git config --global user.name "Your Name"

Set Email

git config --global user.email "you@example.com"

Check Configuration

git config --list

Repository

Initialize Repository

git init

Clone Repository

git clone <repository-url>

Example:

git clone git@github.com:username/project.git

Status & Changes

Check Status

git status

See Changes

git diff

See Staged Changes

git diff --staged

Add & Commit

Add One File

git add <file>

Add All Changes

git add .

Commit

git commit -m "your commit message"

Add & Commit

git commit -am "your commit message"

-a hanya bekerja untuk file yang sudah pernah di-track Git.


Log

View Commit History

git log

Compact Log

git log --oneline

Graph

git log --oneline --graph --all

Branch

List Branches

git branch

Create Branch

git branch <branch-name>

Switch Branch

git switch <branch-name>

Create & Switch

git switch -c <branch-name>

Delete Branch

git branch -d <branch-name>

Rename Current Branch

git branch -m <new-name>

Merge

Merge Branch

git switch main
git merge <branch-name>

Abort Merge

git merge --abort

Remote

Check Remote

git remote -v

Add Remote

git remote add origin <repository-url>

Change Remote URL

git remote set-url origin <repository-url>

Remove Remote

git remote remove origin

Push & Pull

Push

git push

Push New Branch

git push -u origin <branch-name>

Setelah itu cukup:

git push

Pull

git pull

Fetch

git fetch

Fetch All Branches

git fetch --all

Push Local Branch to Remote

git push origin <branch-name>

Undo Changes

Unstage File

git restore --staged <file>

Discard Changes

git restore <file>

Hati-hati: perubahan yang belum di-commit bisa hilang.

Undo Last Commit but Keep Changes

git reset --soft HEAD~1

Undo Last Commit and Unstage Changes

git reset HEAD~1

Hard Reset

git reset --hard HEAD~1

Hati-hati: perubahan dapat hilang.


Stash

Save Changes Temporarily

git stash

List Stashes

git stash list

Apply Latest Stash

git stash pop

Apply Without Removing Stash

git stash apply

Delete Stash

git stash drop

Tag

Create Tag

git tag v1.0.0

List Tags

git tag

Push Tag

git push origin v1.0.0

Push All Tags

git push --tags

Remote HTTPS → SSH

Check remote:

git remote -v

Change to SSH:

git remote set-url origin git@github.com:username/project.git

Test SSH:

ssh -T git@github.com

Common Workflow

Workflow Git yang paling sering digunakan:

git status

git add .

git commit -m "add new feature"

git push

Untuk bekerja dengan branch:

git switch -c feature/todo

# coding...

git add .
git commit -m "add todo feature"

git push -u origin feature/todo

Quick Reference

Task Command
Initialize git init
Clone git clone <url>
Status git status
Add git add .
Commit git commit -m "message"
Log git log --oneline
Branch git branch
Switch git switch <branch>
Create branch git switch -c <branch>
Merge git merge <branch>
Pull git pull
Push git push
Fetch git fetch
Remote git remote -v
Stash git stash
Restore git restore <file>
Unstage git restore --staged <file>
Tag git tag <name>

Most Used Commands

Kalau cuma mau mengingat perintah yang paling penting:

git status
git add .
git commit -m "message"
git push
git pull
git switch -c feature/name
git switch main
git merge feature/name
git log --oneline
git remote -v