8 Perintah Git Esensial

Beberapa perintah Git yang esensial selain yg dasar-dasar. Wajib tau, lebih bagus kalo hafal.

1. Bikin Branch dari Commit Tertentu

Bikin branch dari commit tertentu.

$ git checkout -b [nama branch] [commit hash]

Bikin branch lokal yang nge-track remote branch yang udah ada,

$ git checkout -b [nama branch] --track origin/[nama branch]

2. Merge Non-FF

Kalo mau selalu ada merge-commit tambahin opsi --no-ff.

$ git merge A --no-ff

Bedanya pake flag --no-ff ato nggak kira-kira begini:

3. Stash

Git stash utk nyimpen perubahan konten tanpa harus commit.

$ git stash

Secara default, perintah stash ga nyimpen file baru yg belum pernah di-commit. Kalo mau stash semuanya, termasuk file baru, pake opsi -u atau --include-untracked.

$ git stash -u

Kalo mau simpan semuanya termasuk file yang di-ignore, pake opsi -a atau --all.

$ git stash -a

Kalo mau stash pake nama,

$ git stash push -m "nama stash"

Mau liat daftar stash,

$ git stash list

Mau ambil (apply) tanpa ngapus stash,

$ git stash apply stash@{nomorindex}

Ambil stash & hapus,

$ git stash pop stash@{nomorindex}

4. Reset

Untuk batalin commit (undo).

$ git log --one-line
b6356d2 (HEAD -> master) commit 4
b412042 commit 3
e786b17 commit 2
eaad14e commit 1

Reset 2 commit terakhir,

$ git reset HEAD~2

$ git log --oneline
e786b17 (HEAD -> master) commit 2
eaad14e commit 1

Reset ke commit tertentu.

$ git log --one-line
b6356d2 (HEAD -> master) commit 4
b412042 commit 3
e786b17 commit 2
eaad14e commit 1

$ git reset e786b17

$ git log --oneline
e786b17 (HEAD -> master) commit 2
eaad14e commit 1

5. Revert

Revert itu mirip reset bedanya pointer HEAD bukan mundur tapi maju karena ada commit baru yang batalin commit lama yang jadi target revert.

$ git log --oneline
33bddc7 (HEAD -> master) commit 4
33b6ca8 commit 3
e786b17 commit 2
eaad14e commit 1

$ git revert HEAD~2

$ git log --oneline
8b0e0bc (HEAD -> master) Revert "commit 2"
33bddc7 commit 4
33b6ca8 commit 3
e786b17 commit 2
eaad14e commit 1

Visualisasinya kurang lebih gini,

6. Rebase

Rebase sedikit mirip sama merge. Kalo merge itu bawa commit dari branch lain ke dalam branch yang lagi aktif, rebase mindahin commit di branch aktif ke depan commit branch yang dipake rebase.

#branch: master
$ git log --oneline
a9a6bac (HEAD -> master) master commit 4
5b7a66d master commit 3
ce8ba1b master commit 2
49aec98 master commit 1

#branch: features
$ git log --oneline
23d1fd3 (HEAD -> features) feature commit 2
6edc135 feature commit 1
5b7a66d master commit 3
ce8ba1b master commit 2
49aec98 master commit 1

# rebase features ke depan master
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: feature commit 1
Applying: feature commit 2

$ git log --oneline
8a0f4dd (HEAD -> features) feature commit 2
e1138e7 feature commit 1
a9a6bac (master) master commit 4
5b7a66d master commit 3
ce8ba1b master commit 2
49aec98 master commit 1

Visualisasinya kurang lebih begini (biru master, kuning features),

7. Hapus Remote Tag

Untuk ngapus tag di origin,

$ git push --delete origin [tagname]

8. Reflog

Selain nunjukin daftar commit, git reflog juga nunjukin daftar perintah yang pernah kita jalanin di repo lokal kita.

$ git log --oneline
8a0f4dd (HEAD -> features) feature commit 2
e1138e7 feature commit 1
a9a6bac (master) master commit 4
5b7a66d master commit 3
ce8ba1b master commit 2
49aec98 master commit 1

$  git reflog
8a0f4dd (HEAD -> features) HEAD@{0}: rebase finished: returning to refs/heads/features
8a0f4dd (HEAD -> features) HEAD@{1}: rebase: feature commit 2
e1138e7 HEAD@{2}: rebase: feature commit 1
a9a6bac (master) HEAD@{3}: rebase: checkout master
23d1fd3 HEAD@{4}: checkout: moving from master to features
a9a6bac (master) HEAD@{5}: commit: master commit 4
5b7a66d HEAD@{6}: checkout: moving from features to master
23d1fd3 HEAD@{7}: commit: feature commit 2
6edc135 HEAD@{8}: commit: feature commit 1
5b7a66d HEAD@{9}: checkout: moving from master to features
5b7a66d HEAD@{10}: commit: master commit 3
ce8ba1b HEAD@{11}: commit: master commit 2
49aec98 HEAD@{12}: commit (initial): master commit 1

Also in this category ...


Leave a Reply

Your email address will not be published. Required fields are marked *