Pro Git Reading Notes
Git has become an indispensable skill in development, and mastering Git is a productivity booster. I had some knowledge gaps about Git, so I spent some fragmented time reading “Pro Git, Second Edition”. It’s indeed an excellent book, as recommended by GitHub officially.
Here are some key points I’m noting down to reinforce my memory:
Git Commit Modification
- If it’s only a local commit that hasn’t been pushed yet, you can directly modify the commit message using the principle of
git commit --amend -m
- If it’s the last commit that has already been pushed to the remote, you can reset the pointer to the previous commit and then recommit, but this requires
--force
when pushing
Important Notes
- After modifying the commit message, the hash value will change. Any recommit will change the hash value
- If you want to modify a commit that’s not the most recent one, it’s possible with local history, but not for commits that have already been pushed
Overall, history cannot be changed, but the above scenarios allow for modifications.
Rebase vs Merge
Our Git workflow often involves multiple branches, and for merging changes between branches, there are two approaches: Merge and Rebase.
Merge effect as shown, note:
Rebase - changing the base, effect as shown:
Important Notes
- Merge creates a new commit
- Rebase doesn’t create additional commits
- The specific Rebase operation involves going to the temp branch, executing rebase master to handle conflicts, which changes the ancestor to master, and then performing a merge on the master branch
- Rebase changes the base, meaning it changes the pointer to the ancestor
Server-side Git Hooks
Git hooks can trigger custom scripts to execute at specific behavioral moments.
In actual project development, client-side Git hooks have been playing an important role, such as linting code style, enforcing local unit tests, etc. However, it’s important to know that hooks also have server-side support. This complements the limitations of client-side hooks, allowing for things like commit message quality checks, rejecting invalid pushes, etc.
Server-side Git hooks configuration principles are similar to local ones - remember that Git is distributed.
Final Thoughts
The “Pro Git” book provides comprehensive and practical insights into Git’s internals and best practices. Understanding these concepts - commit modification strategies, the differences between rebase and merge, and server-side hooks - can significantly improve your Git workflow efficiency and collaboration capabilities. These fundamentals are essential for any developer working with version control systems.