Use Git Like A Senior Engineer
Git is an easy and enjoyable program to use once you understand how it works.
I have been using the features of Git for a long time in different teams and projects. I am still forming my opinion on certain workflows (like whether to squash or not), but the main tools are powerful and can be customized (and automated with scripts).
Going through Git logs
It is difficult to read the information in the Git logs without making changes.
git log is basic
Git log provides a lot of detail, but it is usually too much information and not what you need.
git log
We need to stop pretending that these logs are interesting or useful. They contain too much information that we don't need right now and they are not interesting. We should find a better way to get a better understanding of what has been happening in our project.
git log with more visibility
We can quickly get a summary of the git commits in our project by using the "--graph" and "--format" options.
git log --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%an%C(reset)%C(bold yellow)%d%C(reset) %C(dim white)- %s%C(reset)' --all
Wow! These are some good-looking logs! There’s even a semblance of a branched tree beside it.
These logs show you who has been working on what, when changes were made, and where your changes fit into the bigger picture.
--graph adds the tree graph to the left. It’s not the most stylish graph, but it helps to visualize changes in the project’s branches. (Read the docs here.)
--format lets you customize the format of your logs. There are preset formats to choose from, or you can write your own format like this example. (Read the docs here.)
--all includes all of the refs, tags, and branches in the logs (including remote branches). You might not want everything so adjust this as you see fit. (Read the docs here.)
Read the documentation for git-log to learn how to make your git logs better.
Understanding a particular commit
You want to know what happened in a particular commit. You can use the git show command to get an overview of the changes in the commit and also to see the changes made to individual files.
View the summary of a commit
git show <commit> --stat
The --stat
flag will show you a summary of the commit, the files that were changed, and how they were changed.
View specific file changes for a commit
If you want to see what changes were made to a particular file, use git show with the file's location.
git show <commit> -- <filepath>
This will show you which lines in your file have been changed, as well as three lines before and after the changed lines to give you an idea of where the changes are in the file.
Making changes
You created a separate version of the project and added some changes to it. However, while you were working on it, another engineer also made changes to the same files. If you are using GitHub, it will tell you if there are any conflicts between your changes and the other engineer's changes when you try to merge them back into the main version.😱
Git will ask you to fix any problems that occur when you try to add your changes to the main
version. This is important because you don't want to ruin the work that other people have done. To get started resolving this locally you will usually take one of two paths: merge
or rebase
.
git merge vs git rebase
If there are updates to the main branch that you want to add to your own branch, you can either combine the changes together or start your branch from a different point.
merge
combines the changes from one branch and adds them to another branch in a single commit.
git merge origin/main your-branch
rebase
changes the point where a branch was created from the main branch, making it start from a different point.
git rebase origin/main your-branch
Generally, you will use rebase to include changes from the main branch in your own branch. You will use merge to put changes from your branch back into the main branch.
However, using
rebase
carries a lot of potential risks so I would advise you not to use it if you don't understand it. I will have a detailed article about this issue in the future.
Mình hy vọng bạn thích bài viết này và học thêm được điều gì đó mới.
Donate mình một ly cafe hoặc 1 cây bút bi để mình có thêm động lực cho ra nhiều bài viết hay và chất lượng hơn trong tương lai nhé. À mà nếu bạn có bất kỳ câu hỏi nào thì đừng ngại comment hoặc liên hệ mình qua: Zalo - 0374226770 hoặc Facebook. Mình xin cảm ơn.
Momo: NGUYỄN ANH TUẤN - 0374226770
TPBank: NGUYỄN ANH TUẤN - 0374226770 (hoặc 01681423001)
All rights reserved