8 Tips to help you work better with Git原文 https://sweetcode.io/8-tips-to-help-you-work-better-with-git 2016-12-16 05:07:38 ℃ 0 评论
Git is a very powerful version control system. It can be a little bit daunting to try to learn everything around it, so most people just use the basic commands. We want to give you here some help with some tips you may or may not have heard. Either way, these tips can make your workflow a little easier.
One of the best ways to ease your daily workflow with Git is to create aliases for common commands you use every day. This can save you some time in the terminal.
You can use the following commands to create aliases for the most used Git commands, checkout, commit and branch.
gitconfig --global alias.cocheckout gitconfig --global alias.cicommit gitconfig --global alias.brbranch
This way, instead of typing git checkout master you only need to type git co master.
You could also edit them or add more by modifying the ~/.gitconfig file directly:
co = checkout
ci = commit
br = branch
Stashing uncommitted changes
Let’s say we are working on a new feature, but there is an emergency and we need to fix to our project immediately. We don’t want to commit an unfinished feature, and we also don’t want to lose our current changes.
The solution is to temporarily remove these changes with the git stash command:
The git stash command hides these changes, giving us a clean working directory. We’re now able to switch to a new branch to make our important updates, without having to commit a meaningless snapshot just to save our current state.
Once you are done working on the fix and want to show your previous changes again, all you need to is run:
And your changes will be recovered. If you no longer need those changes and want to clear the stash stack you can do so with:
Compare commits from the command line
An easy and quick way to compare the differences between commits, or versions of the same file is to use the command line. For this you can use the git diff command.
If you want to compare the same file between different commits, you do the following:
$ gitdiff $start_commit..$end_commit -- path/to/file
And if you want to compare the changes between two commits:
$ gitdiff $start_commit..$end_commit
These commands will open the diff view inside the terminal, but if you prefer to use a more visual tool to compare your diffs, you can use git difftool. A really great diff viewer/editor is Meld.
To configure Meld:
$ gitconfig --global diff.toolgit-meld
Now to start viewing the diffs:
$ gitdifftool $start_commit..$end_commit -- path/to/file
$ gitdifftool $start_commit..$end_commit
Sometimes when you start modifying your code, you realize that the changes you did are not that good and would like to reset your changes. Instead of clicking undo on everything you edited, you can reset your files to the HEAD of the branch:
$ gitreset --hardHEAD
Or if you want to reset a single file:
$ gitcheckoutHEAD -- path/to/file
Now, if you already committed your changes, but still want to revert back, you can use:
$ gitreset --softHEAD~1
Use Git blame more efficiently
Git blame is a great tool for finding out who changed a line in a file, but there are ways you can use it more efficiently. You can pass different flags, depending on what you want to show.
$ gitblame -w # ignores white space $ gitblame -M # ignores moving text $ gitblame -C # ignores moving text into other files
Now that you know some very useful tips about working with Git, let us give you some other tips on how to best use Git within your workflow.
If you are using the GitLab Workflow, it means that you are working on feature branches. Depending on how long your feature takes to implement, a lot of changes might have been made to the master branch.
In order to avoid major conflicts at the end of the development of your feature, you should pull the changes from the master branch to your branch often. This will allow you to resolve any possible conflicts as soon as possible and it will make merging your branch to master easier.
Commit often, but don’t push every commit
Committing your changes often will keep your changes concise and make them easier to revert, if you were to need that. But it is not necessary to push every single commit to the server, as it will appear in the activity feed and probably spam your colleagues. Work on your changes until you are ready to push.
Push when changes are tested
A nice sign that your changes are ready to push is when they have been tested and the tests are green. This usually also means that this part of your feature is done and you can concentrate on the next part. Push your changes once this has been done and let the CI server test them again.
Do you think you can beat this Sweet post?If so, you may have what it takes to become a Sweetcode contributor...Learn More.
- 2017-06-28 GITLAB email不发送腾讯企业邮箱解决方案
- 2017-06-27 往GitHub上传东西，试了各种方法都不成功，可能是Email设置的问题
- 2017-06-27 通过Git Gui Here上传本地项目到GitHub上
- 2017-06-27 git提交时冲突问题解决
- 2017-06-27 彻底删除/替换git远程仓库 - 非原创，不博客
- 2017-06-27 Ubuntu14.04环境中搭建属于自己的Git服务器
- 2017-06-27 GitLab 的安装
- 2017-06-27 Windows的Git的基本使用
- 2017-06-27 GitHub开源现状：使用者多，贡献者少
- 2017-06-27 The 7 Most Popular DEV Posts from the Past Week
- Java (87048)
- Android (71109)
- Linux (56241)
- C (49906)
- Python (41358)
- 程序员 (32044)
- HTML (27751)
- iOS (26020)
- PHP (25112)
- CSS (22812)
- mysql (20295)
- 数据库 (19153)
- 跨平台 (19146)
- 创业 (17691)
- 算法 (16468)
- iOSDeveloper (15025)
- iOS开发 (14114)
- CC (13544)
- Oracle (12012)
- Windows (11271)
- C语言 (11203)
- Objective-C (10809)
- android开发 (10668)
- Shell (10480)
- Spring (10206)
- 首页投稿 (10196)
- JS (10086)
- LeetCode (9865)
- Ubuntu (9218)
- 数据结构 (9204)
- jQuery (9085)
- 机器学习 (7445)
- acm (7378)
- 设计 (7246)
- SQL (7220)
- android知识 (6881)
- 设计模式 (6699)
- Swift (6624)
- Hadoop (6521)