Yufelix

Yufelix

🚀

Git Personal Commands: Tips to Enhance Development Efficiency

As a backend developer, Git is an indispensable tool in our daily work. Today, I want to share some commonly used Git commands that can help you be more proficient during development.

1. Rollback and Force Push to Remote#

Sometimes, we may find that a recent commit has issues and need to roll back to a previous version. At this point, git reset --hard comes in handy.

git reset --hard <commit-hash>

This command will reset the HEAD pointer of the current branch to the specified commit and discard all changes made afterward. Then, we can switch to the target branch and force push to the remote repository:

git checkout <branch-name>
git push origin <branch-name> --force

Note that the --force parameter will overwrite the history of the remote repository, so use it with caution!

2. Rollback a Specific File#

If you only want to roll back changes to a specific file, you can use the git checkout command:

git checkout <commit-hash> -- <file-path>

This command will restore the specified file to the state of the specified commit, which is very useful when fixing bugs.

3. Check Code Lines#

Sometimes, we want to know how much code we have contributed to a project. At this point, the git log command comes in handy:

git log --author="<username>" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

This command will count the lines of code added, deleted, and net gained by the specified author in the project. It's quite fulfilling to see how much code you've written, isn't it?

Summary#

Git is a very powerful version control tool, and mastering these commonly used commands can greatly improve our development efficiency. I hope these tips can help you be more proficient in your daily work. If you have other useful Git commands, feel free to share them in the comments!

Happy coding! 🚀

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.