Some commands help work more effectively with Git

Git is a powerful version control system. Normally, you only use the basic commands of git while there are many interesting and convenient commands that you may not know to use it. This article will share some commands that can help you do work more effectively with git.

1. Git aliases

The easiest way is to create an alias for the commands we often use in git like checkout, commit, branch …

Once you have created the alias, you must instead use the git checkout master command to return to the master branch as usual, instead you just need to type git co master.

In addition, you can edit or directly add the aliases you want through editing the ~ / .gitconfig file.

2. Stash uncommitted changes

Suppose in case you are working in a new branch but for some reason you need to checkout for another branch to edit the code immediately and you don't want to commit what you did in this current branch. because your code in this branch is incomplete. A solution in this situation is that you will temporarily remove changes in the current branch with the command:

This git stash command will save your changes and refresh the directory you are working on, you can now checkout another branch to make the necessary changes. When you have finished editing that branch and want to go back to the previous branch that is being unloaded, you just need to checkout for that branch and use the command:

This command helps you restore previous changes.

Alternatively, you can use the command below to completely remove the previous stash stack.

3. Compare commits

An easy and quick way to compare the differences between commits or different versions of the same file is to use the command

If you want to compare the differences of the same file on different commits, you can use the following command:

And to compare changes between two commits, you can use the command:

These commands will open diff view in the terminal, but if you want to use a tool to see the difference more visually, consider using Meld. To configure Meld to use the command

You can now use the following commands:

4. Reset files

Sometimes you are editing the code and realize that your changes do not bring the desired results and you want to reset those changes, instead of using the undo command to get back everything you've changed. then you can use the following command:

Or if you want to reset only one file:

And if you've committed the changes, you can completely revert using the command:

5. Use git blame

Git blame is a great tool that allows you to determine who made the change. To use blame effectively, you can use different flags depending on what you want to show.

6. Use parameters with git log

We should use the git log command with the parameters depending on the target, for example:

—Author = "Alex" – Show only commits of a specified author

-Name-only – Only show the changed file names

–Graph – Show graph of all commits

–Reverse – Show shows commits in reverse order (Old commits are displayed first)

[Conclusion] The above are some commands that I think will help you more or less in the process of working with git.

ITZone via Viblo

Share the news now