Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ 25 Git Usage Tips     - Java proxy mode (Programming)

- PostgreSQL log classification and management (Database)

- 7 JavaScript interview questions (Programming)

- Getting Started with Linux system to learn: how to check in a package is installed on Ubuntu (Linux)

- Java MVC CRUD examples (Programming)

- RHEL / CentOS / Fedora Install Nagios 4.0.1 (Linux)

- CentOS 6.5 installation and configuration Cobbler (Server)

- Oracle Migration partition table (Database)

- How to set up HTTPS policies for older browsers (Server)

- C / C ++ various data types Conversion Summary (Programming)

- Linux system security configuration (Linux)

- Linux Telnet access Windows 7 configuration procedure (Linux)

- Spring AOP (Programming)

- Ubuntu Series Installation Docker (Linux)

- Java concurrent programming combat (using synchronized synchronization method) (Programming)

- Source Analysis: Java object memory allocation (Programming)

- Python Dir find a folder several files (Programming)

- Customize the 404 error page Nginx (Server)

- Construction LVM-based raw device Oracle10g Database on Oracle Linux 5.11 (Database)

- Java is simple to read and write HDFS Demo (Programming)

 
         
  25 Git Usage Tips
     
  Add Date : 2017-08-31      
         
       
         
  Andy Jeffries to Git intermediate users to share 25 tips summary. You do not need to do a lot of searching, perhaps these tips helpful to you.

I started using git has now been almost 18 months, thought he had a really understand git. Until I saw on github Scott Chacon in LVS, a supplier / developer of teaching betting / gaming software on the first day benefit.

As a people who enjoyed git, I want to share practical experience learned from a variety of community, so that we do not need to spend too much effort will be able to find the answer.

Basic skills

1. The first step after installation

After installing git, the first thing you need to set your name and e-mail, because this information is required for each submission.

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

2. The pointer is based

Git on all things are stored in the file when you create a commit, it creates a file that contains your submitted information and relevant data (name, email, date / time, last submission, etc.) and connect a tree file, this file contains the list of objects tree or other trees. This is the actual content of the above objects or blob file that this submission (you may think this is a document, although not stored in the object, but stored in the tree). All files are in the file name through SHA-1 is calculated (Translator's Note: The number after the SHA-1 computation after that git version number) is stored in it.

As can be seen from here, branches and tags are contained sha-1 number (version number) simply a pointer to the file submitted, so the use of reference will become faster and more flexible, create a new branch is like Create a file is as simple, SHA - 1 number (version number) will quote you submit this branch. Of course, if you are using GIT command line tool (or GUI) that you will not reach them. But really very simple.

You may have heard the HEAD reference, which is a pointer to the current contents of your submission of SHA-1 number (version number) pointer. If you merge conflicts are resolved, you will not use HEAD particular branch of any change will only point to your current branch.

All branches pointers are stored in .git / refs / heads, HEAD pointer stored in .git / HEAD, the label is stored in the .git / refs / tags, you have time to go see it.

3. Two of the parent (Parent), of course!

When we look in the log file merge commit message, you will see two of the parent, the parent is going first branch, the second is that you want to merge branches.

4. Merge Conflicts

Now, I found that there were merge conflicts and solve it, it is a when we edit the file normal. After <<<<, ====, >>>> remove these tags, and save the code you want to save. Some time before the code is a direct replacement, you can see the conflict is a very good thing. For example, before the change of two conflicting branches you can use this command mode:

$ Git diff --merge
diff --cc dummy.rb
index 5175dde, 0c65895..4a00477
--- A / dummy.rb
+++ B / dummy.rb
@@@ @@@ 1,5 1,5 +1,5
  class MyFoo
    def say
- Puts "Bonjour"
 - Puts "Hello world"
++ Puts "Annyong Haseyo"
    end
  end

If the file is binary, diffing files is not so easy ... What you'll normally want to do is to try each version of the binary file and decide which one to use (or manually copy portions over in the binary file's editor). To pull a copy of the file from a particular branch (say you're merging master and feature132):
If it is a binary file (binary), these files are not easy to distinguish. Usually you will see the version of each binary file, and then decide which (or manually copy the binary file editor), and push it to the particular branch. (For example, you want to merge master and feature132)

$ Git checkout master flash / foo.fla # or ...
$ Git checkout feature132 flash / foo.fla
$ # Then ...
$ Git add flash / foo.fla

Another way is to cat the file from git - you can do this to another filename then copy the correct file over (when you've decided which it is) to the normal filename:
Another method is to git the cat file, you can name another file name, and then you decide that file to the correct file name:

$ Git show master: flash / foo.fla> master-foo.fla
$ Git show feature132: flash / foo.fla> feature132-foo.fla
$ # Check out master-foo.fla and feature132-foo.fla
$ # Let's say we decide that feature132's is correct
$ Rm flash / foo.fla
$ Mv feature132-foo.fla flash / foo.fla
$ Rm master-foo.fla
$ Git add flash / foo.fla
Update: Thanks for reminding me carls original blog comments, you can use "git checkout -ours flash / foo.fla" and "git checkout -theirs flash / foo.fla" in considering you do not need to merge the branch to check the specified version, the personally, I prefer a more explicit way, but it is also a choice ...

Remember, after you resolve merge conflicts To add a file. (I had just committed such a mistake)

Service, branch and label

5. Remote Services

Git has a very powerful feature is the ability to have multiple remote server (and you are running a local repository). You do not always visit, you can have more than one server, and from one (the integration exercise) to read and then write another. Add a remote server is very simple:

$ Git remote add john git@github.com: johnsomeone / someproject.git

If you want to see information about your remote servers you can do:
If you want to view the remote server information you can:

# Shows URLs of each remote server
$ Git remote -v

# Gives more details about each
$ Git remote show name

You can always see the differences between a local branch and a remote branch:
You can always see the local branch and remote branch different places:

$ Git diff master..john / master

You can also see the changes on HEAD that are not on that remote branch:
You can also see on a remote branch HEAD pointer no changes:

$ Git log remote / branch ..
# Note: no final refspec after ..
6. Tagging tag

There are two types of label in Git: Lightweight and annotation type label.

The second is a pointer to remember basic Git, the difference between the two is very simple, lightweight label is simply named submitted pointer, you can submit it to point to another. Note type annotation is an information point and history and marked the name of the object pointer, it has its own information, if desired, may be GPG mark.

Create two types of labels is very simple (one command line with changes)

$ Git tag to-be-tested
$ Git tag -a v1.1.0 # Prompts for a tag message
7. Creating Branches create a branch

Create a branch in git is a very simple thing (very fast and takes less than 100byte file size). Create a new branch and switching to the branch, usually below this:

$ Git branch feature132
$ Git checkout feature132
Of course, if you want to switch to the branch, the most direct way is to use a command like:

$ Git checkout -b feature132
If you want to rename local branch, it is also very simple:

$ Git checkout -b twitter-experiment feature132
$ Git branch -d feature132
Update: Or you (Brian Palmer in the original blog comments noted) can use the -m switch to "git branch" (as Mike pointed out, if you only need one particular branch, you can rename the current branch)

$ Git branch -m twitter-experiment
$ Git branch -m feature132 twitter-experiment
8. Merge Branches

You may recall later merge your changes, there are two ways to do this:

$ Git checkout master
$ Git merge feature83 # Or ...
$ Git rebase feature83
Distinction is rebase and merge, merge will attempt to resolve the changes and create a new submission to fuse them. rebase sucked from the last time you incorporate changes from another branch after separation, and directly follows the other branch of the head pointer. However, before you go on a remote branch server push, do not use rebase. This will make you confused.

If you are not sure which branch (which need to be merged, which need to be removed). There are two ways to switch git branches to help you:

# Shows branches that are all merged in to your current branch
$ Git branch --merged

# Shows branches that are not merged in to your current branch
$ Git branch --no-merged
9. Remote Branch

If you want to place the local branch of the remote server, you can use this command to push:

$ Git push origin twitter-experiment: refs / heads / twitter-experiment
# Where origin is our server name and twitter-experiment is the branch
If you want to remove the branch from the server:

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

$ Git remote show origin
This will list those that once existed and now there is no remote branches, which will help you easily remove your excess local branch.

$ Git remote prune
Finally, if the local track remote branches, common ways are:

$ Git branch --track myfeature origin / myfeature
$ Git checkout myfeature
Despite this, the new version of Git will automatically start tracking, if you use -b to checkout:

$ Git checkout -b myfeature origin / myfeature
Storing Content in Stashes, Index and File System in the stash storage content, indexing and file system

10. Stashing

In Git you can save the contents of the current work area to Git stack and read the relevant content from the most recent submission. The following is a simple example:

$ Git stash
# Do something ...
$ Git stash pop
Many people recommend using git stash apply instead of pop. stash content like this after recovery does not delete saved while 'pop' to restore the contents of the stash store also deleted, you can use git stash apply remove any stack the latest content.

< Code data-language = "javascript"> $ git stash drop
< / Code>
git can be created automatically based on information submitted by the current instruction, if you prefer to use generic information (equivalent to the previous submission does not make any changes)

< Code data-language = "javascript"> $ git stash save "My stash message"
< / Code>
If you want to use a stash (not necessarily the last one), you can do it and then use the list displayed:

< Code data-language = "javascript"> $ git stash list
  stash @ {0}: On master: Changed to German
  stash @ {1}: On master: Language is now Italian
$ Git stash apply stash @ {1}
< / Code>
11. Add Interactivity

In svn, after which, if you have a file changes, and commits all the changed files in the Git in order to better submit specific file or a patch, you need to select the submission of documents submitted in 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 & gt;
This is a menu-based interactive prompt. You can use the digital command before entering or highlighted letter (if you have highlighted input) mode. Common form, the digital input you wish to perform before the operation. (You can be like 1 or 1--4 or 2,4,7 format to execute the command).

If you want to enter Patch mode (p input in interactive mode or 5), also you can do this:

$ 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 @@
 class MyFoo
   def say
- Puts "Annyong Haseyo"
+ Puts "Guten Tag"
   end
 end
Stage this hunk [y, n, q, a, d, /, e ,?]?
As you can see, you will get some options choose to add changes to the bottom portion of the file. In addition, using the "?" Will explain this option.

12. The file system storage / retrieval

Some items (such as Git own project) needs to be added directly in the Git file system does not want the extra file to be examined.

Let us start saving random file in Git

$ Echo "Foo" | git hash-object -w --stdin
51fc03a9bb365fae74fd2bf66517b30bf48020cb
For example, objects in the database, if you do not want some of the objects are garbage collected, the easiest way is to add it to the label:

$ Git tag myfile 51fc03a9bb365fae74fd2bf66517b30bf48020cb
Here we set myfile label, when we need to retrieve the file can be:

$ Git cat-file blob myfile
This developer may need but do not want to check every useful documents (password, gpg key, etc.) is very useful (especially in the production process).

13. View Log

Without using "git log" in case you can not view your recent submission of long-term, however, there are still some more convenient method you use, for example, so you can view the contents of a single submission changes:

$ Git log -p
Or you look at a summary of file changes:

$ Git log --stat
This is like an alias allows you to simplify the submission at a command line, and show good graphic branch.

$ Git config --global alias.lol "log --pretty = oneline --abbrev-commit --graph --decorate"
$ Git lol
* 4d2409a (master) Oops, meant that to be in Korean
* 169b845 Hello world
14. Find in the log

If you want to find the specified author:

$ Git log --author = Andy
Update: Thanks Johannes comments relieved some of my confusion,

Or you can search for the information you submit content:

$ Git log --grep = "Something in the message"
These instructions are called pickaxe powerful instruction to check to be removed or added specific piece of content (for example, when they first appeared or been removed), add the contents of any line will tell you (but does not include the line content just been changed)

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

$ Git log lib / foo.rb
If you have a feature / 132 and ferature / 145 these two branches, and want to see the contents of these branches are not on the master. (Caret is meant non)

$ Git log feature / 132 feature / 145 ^ master
You can also use ActiveSupport style date to shorten the time frame:

$ Git log --since = 2.months.ago --until = 1.day.ago
The default query uses the OR to merge, but you can also use AND (if you have more than one condition)

$ Git log --since = 2.months.ago --until = 1.day.ago --author = andy -S "something" --all-match
15. Select attempt / alteration of the previous version.

You know that according to the information, in the following way to find the previous version:

$ Git show 12a86bc38 # By revision
$ Git show v1.0.1 # By tag
$ Git show feature132 # By branch name
$ Git show 12a86bc38 ^ # Parent of a commit
$ Git show 12a86bc38 ~ 2 # Grandparent of a commit
$ Git show feature132 @ {yesterday} # Time relative
$ Git show feature132@{2.hours.ago} # Time relative
Note: Unlike the previous section mentioned, the parent class at the end of the caret means submission, in front of the caret does not mean that this branch.

16. Choose a mode

The easiest way:

$ Git log origin / master..new
# [Old] .. [new] - everything you have not pushed yet
You can also omit the [new], this will default to the current HEAD pointer.

Rewinding Time & Fixing Mistakes rollback and repair errors

17. Reset Change

If you do not submit you can simply undo changes:

$ Git reset HEAD lib / foo.rb
Usually we use an alias like "unstage" instead:

$ Git config --global alias.unstage "reset HEAD"
$ Git unstage lib / foo.rb
If you have already submitted, there are two cases: When is the last time you just need to submit amend:

$ Git commit --amend
This will not be the last commit, restore your original content, submit information to the default information for your next presentation.

If you have submitted more than once before and wanted to return to that record completely, you can reset the branch back to the specified time.

$ Git checkout feature132
$ Git reset --hard HEAD ~ 2
If you want to but you want to roll back the branch number (version number) SHA1 is not the same (maybe you can branch HEAD points to another branch, or after submission), you can adopt the following ways:

$ Git checkout FOO
$ Git reset --hard SHA
In fact there is a faster way (this does not change the contents of your file copying, and return to the first FOO state and point SHA)

$ Git update-ref refs / heads / FOO SHA
18. Submit to the wrong branch

Well, assuming that you submit to the master, but you would like to submit a topic branch is called experimental, if you want to remove this change, you can create a pointer to the current branch and the head rolled back and then check the new branch

$ Git branch experimental # Creates a pointer to the current master state
$ Git reset --hard master ~ 3 # Moves the master branch pointer back to 3 revisions ago
$ Git checkout experimental
If your branch branch branches were changes will be very troublesome, then you need to do is change the rebase branch elsewhere

$ Git branch newtopic STARTPOINT
$ Git rebase oldtopic --onto newtopic
19. rebase of interaction

This is a very nice feature, but I have seen the demo has been not really get to know, now I know, is very simple. If you made three commit, but you want to re-edit them (or a combination of them).

$ Git rebase -i master ~ 3
Then you get your editor to open some instructions, you need to do is modify the instruction to select / squash / edit (or delete) / submission and save / exit, after editing by using git rebase -continue to your every command.

If you choose to edit a will leaving your submission status, so you need to use git commit -amend to edit it.

Note: Do not rebase when submitted - can only be added after the re-use -continue, -skip or -abort.

20. Clear

If you submitted something (perhaps some of the old SVN resource file) in the branch and want to be completely removed from history, it can be:

$ Git filter-branch --tree-filter 'rm -f * .class' HEAD
If you have to push to the origin, and submitted a number of spam, you can also push the local system before you can do this:

$ Git filter-branch --tree-filter 'rm -f * .class' origin / master..HEAD
Miscellaneous Tips wide variety of skills

21. Have you seen the front reference

If you know before you see the SHA-1 number (version number), and requires some reset / rollback, you can use the command to query reflog recently viewed sha - 1 number (version number):

$ Git reflog
$ Git log -g # Same as above, but shows in 'log' format
22. branch named

An interesting tips, do not forget the name of the branch is not limited to a-z, and 0-9, in the use of the name and / pseudo namespace for naming and version control, is also a good idea, for example:

$ # Generate a changelog of Release 132
$ Git shortlog release / 132 ^ release / 131
$ # Tag this as v1.0.1
$ Git tag v1.0.1 release / 132
23. Found Dunnit

Find out who change a single line of code in a file, a simple command is:

$ Git blame FILE
Sometimes the change occurs on a file (if you merge two files, or if you have moved to a function), so you can use:

$ # Shows which file names the content came from
$ Git blame -C FILE
Sometimes need to track back and forth by clicking on the change, there is a nice built-gui:

$ Git gui blame FILE
24. Database Maintenance

Git normally do not require excessive maintenance, it can almost get their own, though you can also view the use of the database statistics:

$ Git count-objects -v
If the value is too high you can choose your clone garbage collection. This will not affect the content or other people push you, but it can make your command to run faster and use less space:

$ Git gc
It can also be consistency check at runtime:

$ Git fsck --full
You can add -auto parameter (if you run the scheduled task at the server) in the back, when this statistical data is a must.

When the result of the check is "dangling" or "unreachable" this is normal, this is usually the result of rollback and rebase. Get "missing" or "sha1 mismatch" This result is not good ... you need to get professional help!

25. Recover lost branch

If you accidentally delete a branch, you can re-create it:

$ Git branch experimental SHA1_OF_HASH
Reflog you can view your most recently visited SHA1 number (version number) to use git

Another way is to use the git fsck -lost-found, floating objects (dangling commit) is the loss HEAD pointer is submitted, (deleted branches just lost HEAD pointer becomes dangling objects)

Done!
     
         
       
         
  More:      
 
- How to fix Not Enough Free Disk Space On / boot on Ubuntu (Linux)
- Network Security Basics Linux command (Linux)
- CentOS 6.5 set under Oracle 12c at startup (Database)
- Linux port scanning (Linux)
- Spark local development environment to build (Server)
- Ubuntu install Tonido private cloud services (Server)
- Python data types summary (Programming)
- FreeRadius installation process record (Linux)
- How to protect your eyes automatically adjust the screen brightness on Linux (Linux)
- Android Scroller call mechanism and the relationship of computeScroll (Programming)
- CentOS 5.5 kernel upgrade installation iftop (Linux)
- MySQL function: group_concat () function (Database)
- Fatal NI connect error 12170 error in Alert Log (Database)
- Linux foundation tutorial: how to modify the host name on CentOS or RHEL 7 (Linux)
- LaTeX Getting Started Tutorial (Linux)
- MySQL server after an unexpected power outage can not start (Database)
- Puppet 3.x installed on Debian 7 (Server)
- Linux RAID Set RAID 10 or 0 + 1 (Linux)
- C language - Traverse pci device (Programming)
- Install Java JDK 8 in CentOS 7 / 6.5 / 6.4 (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.