Hi, I'm Tuan, a Full-stack Web Developer from Tokyo 😊. Follow my blog to not miss out on useful and interesting articles in the future.
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.2
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.
And Finally
As always, I hope you enjoyed this article and learned something new. Thank you and see you in the next articles!
If you liked this article, please give me a like and subscribe to support me. Thank you. 😊