|
As we all know, Git distributed version control is already a leader in the field, around Git formed a complete ecosystem. Learn Git, first of all learn the basic workflow with Git. A powerful tool, compared to other traditional SVN version control systems, Git is designed for distributed version control and students. Frequently used commands when using Git has pull, commit, push, etc., seemingly very simple. Sometimes, however, the situation you will encounter merge conflicts, Git will then flagged conflict, you need to manually resolve. Sometimes you accidentally code submitted to the wrong branch, and then pushed to the remote repository. Other times, you need to switch to a different branch, but Git will not let you do that, because there are unsaved changes. If you need to submit to another branch of the code patch how to do it? This article will introduce 12 advanced Git command, the rational use of these commands can greatly enhance the efficiency of use Git.
1. Use rebase instead of merge to pull upstream modification
Branch merger will be recorded as a merge commit, this approach makes sense. For example, it can in this way to identify a new feature has been incorporated into the release branch. However, when employees work in multiple teams to a project and using conventional git pull to synchronize branch, will be submitted to the timeline to submit the merger unnecessary pollution. Better approach is to use a feature branch git rebase to rebase to master branch:
$ Git checkout feature
$ Git rebase master
Doing so will move the entire feature branch to start master branch, it will merge all new commits on the branch master. However, compared to the use of merge commit, the change by creating a new group will be submitted to rewrite the history of the project in the original branch for each submission. The main change for the better group that you will get a much cleaner project history. In addition, there are variations on the base of the trap of some discussion.
2. After the execution of git rebase to resolve merge conflicts
As the power comes greater responsibility for the same. When executed git rebase, you may encounter merge conflicts. Merge Conflicts represents two to commit changes to the same row of the same file, Git does not know which one to modify the application. This will result in an error message such as the following:
Git will provide you with three options to fix the conflicting submitted (fa39187):
You can run git rebase -abort completely cancel rebase. Doing so will cancel rebase modify, and branch back into the state of implementation of git rebase before.
You can run git rebase -skip completely ignore the submission. Thus, a problem to submit changes introduced will not be added to the history.
You can use the same criteria merge conflicts steps to resolve the conflict.
3. Save the modified temporary
At work, some things will always be in a messy state. If you then need to switch to a different branch of the how to do it? Git does not allow you to do so, because there are unsaved changes. Frank to say that you do not want to submit semi-finished products go up, come back to modify. Way to solve this problem is to use git stash command. Stash receives the current state of the working directory (for example, to modify the track file changes and staging areas, etc.), and save it to the stack of unfinished changes, so you can then later be modified at any time. You can scratch your work with the following command:
$ Git stash
Saved working directory and index state WIP on feature: 3fc175f fix race condition
HEAD is now at 3fc175f fix race condition
Now, the working directory is clean:
$ Git status
# On branch feature
nothing to commit, working directory clean
Then you can safely switch the branch do something else. But do not worry, this is still in the staging of submission:
$ Git stash list
stash @ {0}: WIP on feature: 3fc175f fix race condition
Later, after returning to feature branch, you can retrieve all the staged changes:
$ Git stash pop
On branch feature
Changes not staged for commit:
(Use "git add ..." to update what will be committed)
modified: index.html
Dropped refs / stash @ {0} (ac2321cc3a33ba712b8e50c99a99d3c20da9d6b8)
About scratch, there are several other options are available as follows:
$ Git stash save "describe it" # give the stash a name
$ Git stash clear # delete a stashed commit
$ Git stash save --keep-index # stash only unstaged files
4. clone a particular remote branch
If you want to clone a particular branch from a remote repository in how to do it? Normally you would use git clone, but to do so all the other branches will be cloned down. A convenient way is to use the git remote add:
$ Git init
$ Git remote add -t -f origin
$ Git checkout
5. Submit to cherry-pick the remote into your branch
Moreover, if you just want to submit a specific remote repository into their own branch in how to do it? You can use git cherry-pick to select a given SHA values submitted, and then merged into the current branch:
$ Git cherry-pick
6. Application from unrelated local patch repository
If you need another unrelated local repository submit patches to the current Warehouse how to do it? The answer is the following command:
. $ Git --git-dir = / git format-patch -k -1 --stdout | git am -3 -k
7. Ignore the track file changes
If you and your colleagues manipulated the same branch, it is likely to require frequent execution git merge or git rebase. However, doing so may be reset a number of related and environmental profile, so after each merger need to be modified. In contrast, you can tell Git permanently by following commands do not control a local file:
$ Git update-index --assume-unchanged
8. run once every X seconds git pull
Often, the reason is that you merge conflicts are working local repository no longer reflects the current state of the remote repository. This is why every morning to first perform a git pull sake. In addition, you can also script (or use GNU Screen) called once every X seconds git pull in the background:
$ Screen
$ For ((i = 1; i <= 10000; i + = 1)); do sleep X && git pull; done
9. subdirectory separated into new warehouse
Sometimes you may need to Git repository in a particular directory into a new warehouse. This can be achieved git filter-branch:
$ Git filter-branch --prune-empty --subdirectory-filter master
# Filter the master branch to your directory and remove empty commits
Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (89/89)
Ref 'refs / heads / master' was rewritten
Now, the warehouse will contain all the files in the specified subdirectory. All files will be deleted before though, but they still exist in the Git history. Now a new local repository can be pushed to the remote.
10. Cleanup
Sometimes, Git will prompt "untracked working tree files" will be "overwritten by checkout". The reasons for this are many. But generally speaking, we can use the following command to keep the tree clean and work to prevent this from happening:
$ Git clean -f # remove untracked files
$ Git clean -fd # remove untracked files / directories
$ Git clean -nfd # list all files / directories that would be removed
11. Project files into tar package, and excludes .git directory
Sometimes, you need to provide a copy of the project to GitHub repository was unable to access external members. The easiest way is to use tar or zip package to all project files. However, if you are not careful, the hidden .git directory will be included in the tar file, which causes the file size larger; at the same time, if the documents inside and receivers Git repository confused, it would be more headache a. Easy rule of thumb is do automatically excluded from tar file out .git directory:
$ Tar cJf .tar.xz / --exclude-vcs
12. Find Modifier
Finally, if the chaotic situation, you will want to find out who is responsible. If the production server goes down, then find the culprit is relatively easy to do: just follow git blame. This command will display each line of the file author, submit hash on the line will be modified to identify, but also to see the submission time stamp:
$ Git blame
Of course, Git command is very much, in addition to the 12 most important commands introduced above, I believe you InfoQ readers in the daily course of their work have their own preference and useful commands, it may be in the form of comments to share with other readers. |
|
|
|