DEV Community

ABIMANYU
ABIMANYU

Posted on

Menghapus File Secara Permanen dari Repo Git

Contoh menghapus settings.py (dan isinya) secara permanen dari repo Git, karena settings.py sudah ter-commit berkali-kali ke GitHub dan berisi password sensitif, kamu tidak cukup hanya menghapusnya di commit terakhir. Kamu harus menghapus jejak historisnya juga dari Git dan dari GitHub.

Langkah 1. Lakukan fresh clone

git clone git@github.com:your-username/your-repo.git
Enter fullscreen mode Exit fullscreen mode

Langkah 2. Install git-filter-repo (pengganti filter-branch)

sudo apt install git-filter-repo
Enter fullscreen mode Exit fullscreen mode

Langkah 3. Hapus settings.py dari riwayat

Jalankan perintah ini dari root project:

git filter-repo --path project/settings.py --invert-paths
Enter fullscreen mode Exit fullscreen mode

Langkah 4: Force push ke GitHub

git push origin --force --all
git push origin --force --tags
Enter fullscreen mode Exit fullscreen mode

Masalah
Git tidak menemukan remote bernama origin, atau kamu belum mengatur koneksi ke GitHub.

fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

Cara Memperbaiki

  • Cek Remote Jalankan:
git remote -v

Kalau hasilnya kosong, berarti belum ada remote yang terdaftar.

  • Tambahkan Remote origin

Tambahkan URL Git remote (dari GitHub) misalnya:

git remote add origin git@github.com:your-username/your-repo.git

Lalu cek kembali:

git remote -v

Masalah
Pesan ini muncul karena kamu menjalankan git pull, tapi Git mendeteksi bahwa:

  • Branch lokal dan remote sama-sama punya commit yang belum digabung
  • Git tidak tahu apakah kamu ingin merge, rebase, atau hanya fast-forward

* branch master -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge (the default strategy)
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
hint: Need to specify how to reconcile divergent branches.

Cara Memperbaiki

Gunakan rebase (buat riwayat commit lebih bersih)

git pull --rebase

Penutup

Terima kasih telah meluangkan waktu untuk membaca. Kami berharap artikel ini dapat memberikan nilai tambah bagi Anda.😎


Top comments (0)