Hướng dẫn đổi Git commit author

Vấn đề
Khi làm việc với nhiều dự án khác nhau, chúng ta cần set commit author khác nhau, tương ứng với từng khách hàng.
Giải quyết
Case 1: Chưa commit code
Ở đây xử lý khá đơn giản.
- Set config git user cho project:
$ git config user.name "Dev"
$ git config user.email "dev@haposoft.com"
- Set global, mặc định cho tất cả các project nếu không set trong project thì author sẽ ăn theo config này:
$ git config --global user.name "Dev"
$ git config --global user.email "dev@haposoft.com"
Case 2: Đã có 1 commit, cần change author cho commit này
$ git commit --amend --author="Dev <dev@haposoft.com>" --no-edit
- Kiểm tra lại bằng git log:
$ git log
commit ae64c857e38e95e55415e93fd60c388b06c9f727
Author: Dev <dev@haposoft.com>
Date: Fri May 7 14:54:16 2021 +0700
Fix text justify
Case 3: Đã có 1 dãy các commit, cần change tất cả:
Ví dụ, bạn có commit history id như sau A-B-C-D-E-F
F là HEAD, và bạn muốn đổi author commit của C và D thành "Dev <dev@haposoft.com>"
-
Chọn ra điểm rebase, ví dụ
git rebase -i B
-
Nếu bạn muốn rebase cả A, dùng lệnh
$ git rebase -i --root
-
Thay đổi dòng chứa commit
C
vàD
từpick
->edit
, Lưu lại và thoát(dùng:wq
) -
Bắt đầu rebase từ
C
$ git commit --amend --author="Dev <dev@haposoft.com>" --no-edit
# Tiếp tục rebase sang D
$ git rebase --continue
$ git commit --amend --author="Dev <dev@haposoft.com>" --no-edit
$ git rebase --continue
-
Rebase hoàn tất
-
Nếu đã push lên remote server thì dùng
git push -f
update.