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 …
1 2 3 | $ git config --global alias.co checkout $ git config --global alias.ci commit $ git config --global alias.br 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.
1 2 3 4 | [alias] co = checkout ci = commit br = branch |
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:
1 | $ git stash |
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:
1 | $ git stash pop |
This command helps you restore previous changes.
Alternatively, you can use the command below to completely remove the previous stash stack.
1 | $ git stash drop |
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
1 | $ git diff |
If you want to compare the differences of the same file on different commits, you can use the following command:
1 | $ git diff $ start_commit .. $ end_commit - path / to / file |
And to compare changes between two commits, you can use the command:
1 | $ git diff $ start_commit .. $ end_commit |
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
1 | $ git config --global diff.tool git-meld |
You can now use the following commands:
1 2 3 | $ git difftool $ start_commit .. $ end_commit - path / to / file # or $ git difftool $ start_commit .. $ end_commit |
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:
1 | $ git reset --hard HEAD |
Or if you want to reset only one file:
1 | $ git checkout HEAD - path / to / file |
And if you've committed the changes, you can completely revert using the command:
1 | $ git reset --soft HEAD ~ 1 |
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.
1 2 3 | $ git blame -w # ignores white space $ git blame -M # ignores moving text $ git blame -C # ignores moving text into other files |
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.