Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ 25 Git Advanced Skills     - About Linux backdoor (Linux)

- Apple Mac computer to install Windows 10 Concise Guide (Linux)

- In Debian 4.3 compiler under Linux-2.6.28 kernel Summary (Programming)

- Linux configuration startup mount: fstab file (Linux)

- shell script: a key optimization CentOS system (Linux)

- Teach you how to ensure password security under the Linux operating system (Linux)

- Ubuntu and derived versions of the user how to install G Mic (Linux)

- Linux operating system Study Notes (Linux)

- shell script error dirname: invalid option - b (Database)

- To install JDK1.7 and compiler Hadoop-2.7.1 under CentOS7 (Server)

- Shutdown - an advanced shutdown artifact (Linux)

- Linux virtual machines to solve end MySQL database can not remote access (Database)

- Python several standard types of built-in functions (Programming)

- Linux add a new hard disk (Linux)

- [SHELL] MySQL primary recovery solution from + Keepalived online (Server)

- Linux, see picture not resolve the problem (Linux)

- Learning C ++ Standard Template Library and data structures (Programming)

- To install PostgreSQL 9.4 (Database)

- Linux System Getting Started Tutorial: mounted directly in Linux LVM partition (Linux)

- Linux / BSD firewall M0n0wall Profile (Linux)

  25 Git Advanced Skills
  Add Date : 2017-01-08      
  I've been using git almost 18 months, I felt it should have been very understanding. Then from Scott Chacon of GitHub LVS over to do the training, and I was on the first day learned a lot.

As one pair of git feel good, I feel to share some valuable information from the community to grasp, perhaps someone can help solve the problem without having to do too much in-depth study.

Basic skills

1. The first step after installation

After installing git, the first thing you should do is to set your name and e-mail, because each submission must use this information:

$ Git config --global user.name "Some One"
$ Git config --global user.email "someone@gmail.com"

2. Git is a pointer-based

Everything is stored in a file in git. When you create a commit when you will create a submission containing information and related data (name, e-mail address, date / time, before a submission, etc.) file, and link it to a file tree. This tree file contains a list of objects or other tree. Object (or binary large objects) is mentioned here and submit this actual content (which is also a file, in addition, even though the file name is not included in the object, but is stored in the tree). All these documents are subject to use SHA-1 hash value as the file name.

In this manner, branches and tags is a simple file (basically like), contain SHA-1 hash value points that submission. Using these indexes will bring great flexibility and speed, such as creating a new branch is simply to create a file with the name and branch of the divided submit that the SHA-1 index. Of course, you do not need to do this, but just use Git command-line tool (or GUI), but the fact is that simple.

You may have heard HEAD index had called. This is simply a file that contains the SHA-1 is currently pointing to your index that submission. If you merge conflicts being resolved once and then saw HEAD, this is not a particular branch of a particular position on a branch or necessary, just indicate your current location.

All branches pointers are stored in .git / refs / heads inside, HEAD in .git / HEAD years, and the label is stored in .git / refs / tags inside - they can just go in and see.

3. Two father (parent) - you're not wrong!

See information submitted by a merger in history, you will see two parent node (different from the conventional case submitted on the working copy). The first parent is the branch you are, you are merged with the second branch.

4. Merge Conflicts

At present, I believe you met merge conflict and solved. Usually edit the file, remove <<<<, ====, >>>> flag, reservations need to leave code. You can sometimes see these two modifications before the code will be very good, for example, until now these two branches of the conflict changes. Here's one way:

$ Git diff --merge
diff --cc dummy.rb
index 5175dde, 0c65895..4a00477
--- A / dummy.rb
+++ B / dummy.rb
@@@ - 1,5-1,5 + 1,5 @@@
def say
- Puts "Bonjour"
- Puts "Hello world"
++ Puts "Annyong Haseyo"
If it is a binary file, the difference is not so simple ... usually you have to do is to test both versions of the binary file to determine which retention (or manually copy the conflict in part binary file editor). Get a copy of the file (for example, you merge two branches in the master and feature123) from a particular branch:

$ Git checkout master flash / foo.fla # or ...
$ Git checkout feature132 flash / foo.fla
$ # Then ...
$ Git add flash / foo.fla
Another way is through git output file - you can output to another file name, and then when you decide which to use later, and then selected the correct file copied as normal file name:

$ Git show master: flash / foo.fla> master-foo.fla
$ Git show feature132: flash / foo.fla> feature132-foo.fla
$ # Detected master-foo.fla and feature132-foo.fla
$ # If you say that we have decided to file from feature132 is correct
$ Rm flash / foo.fla
$ Mv feature132-foo.fla flash / foo.fla
$ Rm master-foo.fla
$ Git add flash / foo.fla
Update: Thanks Carl in the comments on the original blog post reminder, you can actually use "git checkout -ours flash / foo.fla" and "git checkout -theirs flash / foo.fla" to check out a specific version of a file, and you do not remember the name of the branch in the merger. I personally like it a little more accurate, but it is also a way to ...

After remember to resolve conflict To add files submitted (like what I have done).

Server, branches and tags

5. The remote server

A super large functional git is that you can have more than one remote server (in fact, you have been working on a local repository). You do not necessarily have to have write access to these servers, you can have multiple servers that can be read (to merge their work) and then written to another warehouse. Add a new remote server is very simple:

$ Git remote add john git@github.com: johnsomeone / someproject.git
If you want to view information about the remote server can do this:

# Display URL each remote server
$ Git remote -v
# For more details
$ Git remote show name

You can always see the difference between the local branch and remote branch:

$ Git diff master..john / master
You can also see not on a remote branch HEAD changes:

$ Git log remote / branch ..
# Note: .. not followed by specific reference to the end

6. Label

In git, there are two types of tags - lightweight tags and annotated tags. Remember Tip 2've said git is a pointer-based, the difference between the two is simple. Lightweight tag is simply a pointer to a point with a name submitted. You can always submit it to point to another. Annotated tag is a pointer to an object with a name tag pointer, with its own information and history. Because their information, it may need to be signed in accordance with GPG.

The establishment of these two types of labels is very simple (only a command-line switch difference)

$ Git tag to-be-tested
$ Git tag -a v1.1.0 # will be prompted to enter the tag information

7. Establishment of a branch

Git branch established in a very simple (and as fast as lightning, because it only needs to create a file smaller than 100 bytes). Create a new branch and switch over conventional manner:

$ Git branch feature132
$ Git checkout feature132
Of course, if you are sure you switch directly to the newly created branch, you can use a command to achieve:

$ Git checkout -b feature132
If you want to rename a local branch is also very simple (you can show what happened a long way):

$ Git checkout -b twitter-experiment feature132
$ Git branch -d feature132
Update: You can also (like Brian Palmer presented in the original blog post's comments Lane) only "git branch" of the -m switch in a single command to achieve (like Mike proposed, if you specify only one parameter branch will rename the current branch):

$ Git branch -m twitter-experiment
$ Git branch -m feature132 twitter-experiment

8. Merge Branches

Maybe sometime in the future, you want to merge changes. There are two ways:

$ Git checkout master
$ Git merge feature83 # or ...
$ Git rebase feature83
Rebase and merge the differences between the merge will attempt to process changes and create a new hybrid of both submissions. rebase will try to put all the changes you from a branch last separated, one is added to the HEAD of the branch. However, the branch has been pushed to the remote server after not to rebase - this will cause a conflict / problem.

If you are not sure on which branch there is a unique work - so you do not know which branch need to be merged and which can be deleted, git branch There are two switches can help you:

# Display all merged into the current branch branch
$ Git branch --merged
# Show no branches merged into the current branch
$ Git branch --no-merged

9. remote branch

If you want to have a branch in the local push to the remote server, you can use the command line push up:

$ Git push origin twitter-experiment: refs / heads / twitter-experiment
# Origin is the name of our server and twitter-experiment is a branch name
Update: Thanks Erlend comment on the original blog post - this fact and git push origin twitter-experiment effect is the same, but use the full syntax, you can use a different branch name in between (so local branch can be add- ssl-support and the distal end is issue-1723).

If you want to delete a branch on a remote server (note the name of the branch in front of the colon):

$ Git push origin: twitter-experiment
If you want to see the status of all remote branch can do this:

$ Git remote show origin
This command may be listed on some previous servers but had now gone branch. If that happens you can use the following command to clean up from your local branch and out:

$ Git remote prune
Finally, if you want to track a remote branch locally, Common way:

$ Git branch --track myfeature origin / myfeature
$ Git checkout myfeature
However, when using the new version of git branch -b mark detection will automatically set the track:

$ Git checkout -b myfeature origin / myfeature
Save the contents in the storage, indexing and file system

10. Storage

In git where you can store the current operating state into a stack, and can be taken out. The simplest case is the following:

$ Git stash
# Do something else ...
$ Git stash pop
Many people recommend using git stash apply instead of pop, but if doing so eventually left a long list of storage. The "pop" will automatically be removed from the stack when fully loaded. If you've used git stash apply, you can also use the following command to remove the last item from the stack:

$ Git stash drop
git automatically created based on current information submitted comments. If you prefer to have custom information (because it may be submitted and before a no contact):

$ Git stash save "My stash message"
If you want to take out a particular storage point from the list (not necessarily the last one) may be listed first and then remove them in the following way:

$ Git stash list
stash @ {0}: On master: Changed to German
stash @ {1}: On master: Languageis now Italian
$ Git stash apply stash @ {1}

11. Adding Interactive

The subversion of the world you can only modify the file and then commit all changes. In git where you have a much more powerful way to submit some documents, or even part of the patch. Part of the file or document submitted in some changes you need to enter an interactive mode:

$ Git add -i
staged unstaged path
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
This will allow you to enter a menu-based interactive prompts. You can use the command number or highlight letters (if you open the highlighted words in the terminal) to enter the corresponding mode. Then just enter the number you wish to file operations (you can use such a format, 1 or 1-4 or 2,4,7).

If you want to enter patch mode (interactive mode Press 'p' or '5'), you can also go directly to:

$ Git add -p
diff --git a / dummy.rb b / dummy.rb
index 4a00477..f856fb0 100644
--- A / dummy.rb
+++ B / dummy.rb
@@ - 1,5 +1,5 @@
def say
- Puts "Annyong Haseyo"
+ Puts "Guten Tag"
Stagethis hunk [y, n, q, a, d, /, e ,?]?
You can see below some of the options will be available to add the changes to the file, all changes to the file, and so on. Use the '?' Command can explain these options.

12. From the file system to save / retrieve changes

Some items (such as Git project itself) in the git filesystem directly save additional documents and did not add them to version control.

Let's git stored in a random file to start:

$ Echo "Foo" | git hash-object-w --stdin
So that the target file has been saved to the database, but if you do not set a pointer to it, then it will be treated as garbage collection. The easiest way is to set a label:

$ Git tag myfile 51fc03a9bb365fae74fd2bf66517b30bf48020cb
Note that we use the label myfile. When we need to use this file when you can do this:

$ Git cat-file blob myfile
This file is useful for some tools, open may be used (password, GPG keys, etc.) but do not want checked out to every hard disk (especially in practical work).

Logs, and what changes?

13. View Log

Prolonged use Git, it does not used 'git log' to see the latest submissions. However, there are some tricks to better use. For example, you can use the following command to view specific changes each commit:

$ Git log -p
Or you could just see which file changes:

$ Git log --stat
There is a very good alias you can try to submit a short name will be displayed and a good map and a branch in one line display of information submitted (a bit like gitk, but in a command line):

$ Git config --globalalias.lol "log --pretty = oneline --abbrev-commit --graph --decorate"
$ Git lol
* 4d2409a (master) Oops, meant that to be inKorean
* 169b845Hello world

14. Search Logs

If you are looking for a particular submitter can do:

$ Git log --author = Andy
Update: Thanks Johannes comment before I have removed some of confusion here.

Or you want to find some information in the relevant fields in the submission:

$ Git log --grep = "Something in the message"
There is also a more powerful command called a pickaxe used to remove or add lookup contains a specific content of the submission (for example, the contents of the first occurrence or deleted). I can tell you when to increase a line (but a character behind a row would not be altered):

$ Git log -S "TODO: Check for admin status"
If you change a particular file, such as lib / foo.rb

$ Git log lib / foo.rb
For example, you have a feature / 132 branches and feature / 145 branch, and then you want to look at these two branches not in the master branch and submitted (note symbol is not the meaning):

$ Git log feature / 132 feature / 145 ^ master
You can also use the date format ActiveSupport be reduced to a range of dates:

$ Git log --since = 2.months.ago --until = 1.day.ago
By default, it will be combined with the OR query, but you can easily change AND (if you have more than one search criteria)

$ Git log --since = 2.months.ago --until = 1.day.ago --author = andy -S "something" - all-match

15. Check / modified version

There are many ways can be used to refer to a version to see what you remember:

$ Git show 12a86bc38 # Depending on the version
$ Git show v1.0.1 # under the label
$ Git show feature132 # according to the branch name
Parent node $ git show 12a86bc38 ^ # commit the
$ Git show 12a86bc38 ~ 2 # submit a grandparent
$ Git show feature132 @ {yesterday} # temporal correlation
$ Git show feature132@{2.hours.ago}# time-dependent
Attention and the previous section is somewhat different, meaning the end of the parent node is submitted - start position meant not in this branch.


16. A range of options

The easiest way:

$ Git log origin / master..new
# [Old] .. [new] - all you do not push submitted
You can also omit the [new], will use the current HEAD.


Time back and regret


17. Reset changes

If you do not submit, then you can use the following command to easily cancel changes:

$ Git reset HEAD lib / foo.rb
Usually use 'unstage' alias, because the above seems somewhat intuitive.

$ Git config --globalalias.unstage "reset HEAD"
$ Git unstage lib / foo.rb
If you have submitted the file, you can do two things - if it is the last time you can also submit corrections:

$ Git commit --amend
This cancels the last commit to work branch fall back all the changes before submitting tagged state, but also ready to submit information can be modified or submitted directly.

If you have submitted several times and hope all rolled back, you can reset the branch to the right place.

$ Git checkout feature132
$ Git reset --hard HEAD ~ 2
(A commit Perhaps you want to replace one branch to another branch HEAD or later) if you actually want to point to a completely different branch of SHA1 you can use the following long way:

$ Git checkout FOO
$ Git reset --hard SHA
In fact there is a fast way (do not need to put on your work branch to switch to the FOO and then proceeds to SHA):

$ Git update-ref refs / heads / FOO SHA

18. Submit to the wrong branch

Well, say you have submitted to the master, but it should create a theme called experimental branch more appropriate. To move those changes, you can create a branch in the current location, and then checked out the new rollback HEAD branch:

$ Git branch experimental # create a link to the current location of the master pointer
$ Git reset --hard master ~ 3 # mobile master branch pointer to the three previous versions
$ Git checkout experimental
If your changes are on a branch branch branch will be more complicated. So you need to do is to switch to another local branch basis:

$ Git branch newtopic STARTPOINT
$ Git rebase oldtopic --onto newtopic

19. Interactive handover base

This is a show I did not really understand before seen have been very functional now feel like it is very simple. Let's say you submitted three times but you want to change the order or edit (or combined):

$ Git rebase -i master ~ 3

Then it will launch your editor and with some instructions. You have to do is modify these instructions to select / insert / edit (or delete) the submission and save / exit. After editing you can then use git rebase --continue command to make each instruction to take effect.

If you have changes, will switch to the state it was in when you commit, then you need to use the command git commit --amend to edit.

Note: Do not rebase when submitted - must first add and use the Parameter --continue, - skip or --abort.


20. Cleanup

If you submit something to your branch (you may be imported from some old SVN repository), then you want to put a file is deleted from the history of all:

$ Git filter-branch --tree-filter 'rm -f * .class' HEAD
If you've been pushed to origin, but then submitted some changes to garbage, you can push to do so before the local system:

$ Git filter-branch --tree-filter 'rm -f * .class' origin / master..HEAD

Other Tips


21. Before you viewed a reference

If you know that you have previously viewed a SHA-1, but then made some reset / rollback operation, you can use the command to list reflog recently viewed SHA-1 record:

$ Git reflog
$ Git log -g # same as above, but use the 'log' format output

22. branch named

A cute little trick - do not forget the name of a branch is not limited to a-z, and 0-9. Names can / will be very convenient and space or used to create a pseudo-name version, for example:

$ # 132 changes generated version history
$ Git shortlog release / 132 ^ release / 131
$ # Label affixed v1.0.1
$ Git tag v1.0.1 release / 132

23. Find out who is the murderer

Usually find out who change a file inside a line of code would be useful. Realization of this function is the simplest command:

$ Git blame FILE
Sometimes these changes come from other files (if you merge two files, or you move a certain function) so you can use the following command:

$ # Display content from which file
$ Git blame -C FILE
Sometimes the change and go back by clicking on the respective long long ago to keep track of the changes will be very good. There is a good built-in GUI command to do this:

$ Git gui blame FILE

24. Data Maintenance

Typically git does not require regular maintenance, which take care of myself well. However, you can view the statistics by the following command:

$ Git count-objects -v
If you take up a lot of space, you can choose your local repository to do garbage collection. This does not affect the push or others, but it can make some of the commands to run faster and reduce the space occupied:

$ Git gc
Often it makes sense to also run an integrity check:

$ Git fsck --full
You can also add --auto parameter at the end (if you are on a server often / daily run this command crontab words), then it will only be executed fsck action when necessary.

When checking in, see "dangling" or "unreachable" is normal, usually as a result of the rollback HEAD or switch basis. And see the "missing" or "sha1 mismatch" is not right ... to find professionals to help it!


25. The recovery lost branch

If you use the -D parameter removes the experimental branch, you can use the following command to re-establish:

$ Git branch experimental SHA1_OF_HASH
If you have recently visited, you can usually use git reflog to find the SHA1 hash.

Another way is to use the git fsck -lost-found. One dangling submitted is missing HEAD (it has just been deleted HEAD branch, which is referenced to the current HEAD HEAD so it is not in a dangling state)
- Docker knowledge base (Server)
- Protection Docker container Notice (Server)
- Linux remote wake the computer original code [C] (Linux)
- LVS + Keepalived the DR mode (Server)
- Use mysqldump MySQL database backup - Linux Shell Scripting (Database)
- Laravel 4.2 Laravel5 comprehensive upgrade Raiders (Server)
- Oracle RAC node on the expulsion of the inspection process on OEL6.3 (Database)
- Why learn Java EE (Programming)
- Bash How to read a file line by line (Programming)
- Through eight skills to let you become a super Linux end-user (Linux)
- MongoDB upgrade to 2.6 (Database)
- MySQL stored procedures and triggers (Database)
- ASP.NET 5 is connected with the Redis server on the Linux platform (Server)
- Analysis RabbitMQ cluster (Server)
- You know the difference between URL, URI and URN among you (Linux)
- LVM basic concepts, management (Linux)
- Java-based data source database access (Programming)
- Oracle PLS-00231 error analysis (Database)
- Learn about EditText little depth (Programming)
- Linux C programming and Shell Programming in the development of practical tools similarities summary (Programming)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.