Giải Pháp Xử Lý Lỗi và Cấu Hình Git Nâng Cao

1. Lỗi Push Git: `error: failed to push some refs to`

$ git commit -m 'camket6'
$ git push
remote: fatal error in commit_refs
To github.com:user/repo.git
! [remote rejected] master -> master (failure)
error: failed to push some refs

Nguyên nhân: Dữ liệu nhạy cảm (API key, token) trong commit kích hoạt cơ chế bảo mật.

Khắc phục:

  1. Hoàn tác commit lỗi:
    git reset --hard HEAD~1
  2. Kiểm tra và loại bỏ thông tin nhạy cảm
  3. Commit và push lại

2. Khôi phục sau Reset

Sử dụng git reflog để tìm commit đã xóa:

$ git reflog
07a5352 HEAD@{0}: reset: moving to HEAD~1
39128d1 HEAD@{1}: commit: commit4.6

$ git reset --hard 39128d1

Cảnh báo: Lệnh này ghi đè mọi thay đổi chưa commit.

3. Quản lý Submodule

Thêm submodule:

git submodule add git@github.com:user/repo.git ten_thu_muc

Cấu trúc .gitmodules:

[submodule "ten_thu_muc"]
  path = ten_thu_muc
  url = git@github.com:user/repo.git

4. Git LFS cho File Lớn

# Kích hoạt LFS
git lfs install

# Theo dõi file
git lfs track "*.mp4"
git lfs track "thu_muc/file_lon.zip"

# Commit và push
git add .gitattributes
git commit -m "Cấu hình LFS"
git push --force

5. Cấu hình SSH Nâng Cao

File ~/.ssh/config mẫu:

Host github
  User git
  Hostname ssh.github.com
  Port 443
  IdentityFile ~/.ssh/github_key
  TCPKeepAlive yes

Host gitee
  User git
  Hostname gitee.com
  Port 22
  IdentityFile ~/.ssh/gitee_key

Tạo khóa SSH:

ssh-keygen -t ed25519 -C "user@email.com" -f ~/.ssh/ten_key

6. Lệnh Git Cơ Bản

Cấu hình

git config --global user.name "Ten"
git config --global user.email "email@example.com"

Nhánh

git checkout -b nhanh_moi
git merge nhanh_moi
git branch -D nhanh_cu

Quản lý thay đổi

git restore --staged file
git reset HEAD~2
git stash pop

7. Xử lý xung đột

  1. Mở file xung đột, tìm ký hiệu <<<<
  2. Chọn phiên bản muốn giữa lại
  3. Đánh dấu đã giải quyết:
    git add ten_file
  4. Hoàn tất merge:
    git commit -m "Merge nhanh"

Thẻ: Git-Push-Error Git-Reset Git-Submodule Git-LFS SSH-Configuration

Đăng vào ngày 2 tháng 8 lúc 23:37