"Pro Git" reading notes 1

"Pro Git" reading notes 1

git config

1. The config file has the following types

Config file location
    --global use global config file
    --system use system config file
    --local use repository config file
    -f, --file <file> use given config file
    --blob <blob-id> read config from given blob object

2. There are the following actions (action) instructions, cooperate Config file location, used to add, delete, modify, and check different configuration files

Action
    --get get value: name [value-regex]
    --get-all get all values: key [value-regex]
    --get-regexp get values ​​for regexp: name-regex [value-regex]
    --get-urlmatch get value specific for the URL: section[.var] URL
    --replace-all replace all matching variables: name value [value_regex]
    --add add a new variable: name value
    --unset remove a variable: name [value-regex]
    --unset-all remove all matches: name [value-regex]
    --rename-section rename section: old-name new-name
    --remove-section remove a section: name
    -l, --list list all
    -e, --edit open an editor
    --get-color find the color configured: slot [default]
    --get-colorbool find the color setting: slot [stdout-is-tty]
git --global --edit//Open the editor to modify the global configuration file
git --global --list//List all configuration information in the configuration file

3. The way to set the key value --addcan be omitted. If no Config file locationconfiguration item is added , the default is to save to--local

git config --add user.age 25
git config user.xxx "hehehe"
git config --global user.name "zhangfengqi"

4. Set aliases for commonly used commandsalias

$ git config --global alias.co checkout
$ git config --global alias.last'log -1 HEAD'

$ git config --global alias.unstage'reset HEAD --'
//The following two are equivalent
$ git unstage fileA
$ git reset HEAD - fileA

If you want to execute an external command, not a Git subcommand, you need to add a !symbol before the command .

$ git config --global alias.visual'!gitk'

ignore

Ignore files with some suffixes, just modify .gitignoreand add them

vim .gitignore
*.swp
*.a
!lib.a//Ignore all .a suffixes, except lib.a!

The format specification of the file .gitignore is as follows:

  1. All blank lines or lines beginning with # will be ignored by Git.
  2. You can use standard glob pattern matching.
  3. The matching pattern can start with (/) to prevent recursion.
  4. The matching pattern can end with (/) to specify a directory.
  5. To ignore files or directories outside the specified mode, you can add an exclamation point (!) before the mode to negate it.

The so-called glob pattern refers to the simplified regular expression used by the shell. The asterisk ( ) matches zero or more arbitrary characters; [abc] matches any character listed in square brackets (this example matches either an a, a b, or a c); the question mark (?) only Match an arbitrary character; if you use a dash to separate two characters in square brackets, it means that all characters within the range of these two characters can be matched (for example, [0-9] means to match all digits from 0 to 9). Use two asterisks ( ) to match any intermediate directory, for example, a/**/z can match a/z, a/b/z or a/b/c/z, etc.

# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/directory
doc/**/*.pdf

A collection of .gitignore templates

level 11 12 git rm

http://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#Removing-Files

  1. Remove the file from the temporary storage area and delete it from the working directory of the hard disk
  2. If you only use rm to delete the file from the hard disk, there will be an un-temporarily recorded record in git. At this time, you need to perform git rm xxxthe operation of recording this deleted file. The next time you submit, the file will no longer be included in version management.
  3. If you delete a modification before and have been placed in the temporary storage area, you must use the forced delete option -f (annotation: the first letter of force). This is a security feature to prevent accidental deletion of data that has not been added to the snapshot. Such data cannot be restored by Git.
  4. I just want to delete the file from the staging area (meaning no longer let git track it), but keep it on the hard drive without deleting it. Usage scenario: This is especially useful when you forget to add a .gitignore file and accidentally add a large log file or a bunch of compiled files like .a to the temporary storage area.
git rm --cached deleteme.rb//Save the working tree file, only remove it from the stage, and no longer track it

level 13 14 git mv

1. Rename git mv oldfile.txt newfile.txt

In fact, running git mv is equivalent to running the following three commands:

$ mv README.md README
$ git rm README.md
$ git add README

2. Cut files

//Make a new folder named src, and move all of the .html files into this folder
git mv *.html src/

level 18 git push

https://git-scm.com/docs/git-push

  • git push --tags
//git push origin HEAD
A handy way to push the current branch to the same name on the remote.

git commit --amend modify the last commit

1. Modify the last submission information

Enter directly git commit --amend, you will be brought into the editor to modify the submission information

2. One submission has been completed, but there is a file that I forgot to mention

Then run by modifying the file git addor git rma file has been tracked, and then run git commit --amendaway the current staging area and make it as a snapshot of the new submission.

Note the top because the amendment will change the submitted SHA-1 checksum. It is similar to a small rebase-if the last commit has already been pushed, don't fix it.

git status [-s | –short]

$ git status -s
 M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt

The newly added untracked files are marked with??, the newly added files in the temporary storage area are marked with A, and the modified files are marked with M. You may have noticed that M can appear in two positions. The M on the right indicates that the file has been modified but has not been placed in the temporary storage area. The M on the left indicates that the file has been modified and placed in the temporary storage area. Deposit area. For example, the above status report shows: The README file has been modified in the workspace but the modified file has not been put into the temporary storage area, the lib/simplegit.rb file has been modified and the modified file has been put into the temporary storage Area. Rakefile is modified in the working area after being modified and submitted to the temporary storage area, so there is a record of the file being modified in both the temporary storage area and the working area.

git diff & git difftool

  1. To see which parts of the files that have not been staged have been updated, directly enter git diff without adding parameters. Please note that git diff itself only displays the changes that have not been staged, not all the changes made since the last submission. So sometimes you temporarily store all the updated files at once, but there is nothing after running git diff, that's why.
  2. To view the content that has been temporarily stored and will be added to the next commit, you can use the git diff -cached command. (Git 1.6.1 and later versions also allow the use of git diff --staged, the effect is the same, but it is better to remember.)
  3. The difference between git diff to see each area (staging area workspace repository) git diffcomparison is the difference between the work area and the staging area of git diff –cachedcomparison is the difference between the staging area and the repository git diff HEADcan see the work area and the difference between each repository After the second commit, git diff –cachedthere is no content, because the content in the temporary storage area has been updated to the version library, so there is no difference between the content in the temporary storage area and the version library
  4. The plug-in version of git diff git difftool --tool-help//See which Git Diff plug-ins your system supports git config --global diff.tool vimdiff git difftool --edit vimdiff

git log view commit history

1. The default condition (without any parameters)

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gee-mail.com>
Date: Sat Mar 15 10:31:28 2008 -0700

    first commit

All updates are listed by submission time, with the most recent update at the top. By adding parameters, -pyou can see the differences in the content of each submission

2. Simple custom output content --pretty=oneline | short | full | fuller

$ git log --pretty=oneline//Only the commit number and comment
4df268a7256494563ff21feeb245a5fb52e42709 m_school bug fixed1
a1f8f9f33ffa4a39d99a0325e92acd73a48e11fa agent phone

$ git log --pretty=short//There is no submission time, there will be a brief prompt for merge submission
commit 7e65491f3f742c3dd3befccd01f74d60d1000c19
Author: zuoerdong

    redian index, brief content trim space and substr to length 100

commit de7042ce4004d8207d3aa4f768d076402ccc972c
Merge: 4f0513c 448fdf7
Author: Wang Chunwei

git log --pretty=full//Both the author and committer will be listed, but there is still no commit time
commit 4df268a7256494563ff21feeb245a5fb52e42709
Author: Zhang Fengqi <zhangfengqi@xxx.com>
Commit: Zhang Fengqi <zhangfengqi@xxx.com>

    m_school bug fixed1

$ git log --pretty=fuller//too much time
commit 4df268a7256494563ff21feeb245a5fb52e42709
Author: Zhang Fengqi <zhangfengqi@xxx.com>
AuthorDate: Fri Jan 15 18:28:23 2016 +0800
Commit: Zhang Fengqi <zhangfengqi@xxx.com>
CommitDate: Fri Jan 15 18:28:23 2016 +0800

    m_school bug fixed1

3. Highly customized output content --pretty=format:"xxx"

alias.hist=log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short

Options

Description

%h

The short hash of commit, the uppercase H is the full hash

%t

short hash of tree, same as above

%an

Author name

%ae

anthor email

%ad

Author revision date (you can use the -date= option to customize the format)

%ar

relative date is displayed as how long ago

%s

Submit instructions

%arIs equivalent to --date=relativeor--relative-date

4. Limit the output length and content

  • When you want to get the union of multiple restrictions, you need to add --all-matchoptions, otherwise the submissions that meet any one of the conditions will be matched

Options

Description

example

-(n)

Only show the most recent n submissions

--since or --after

Only show submissions after the specified time

--since="2016-01-01" or "1 month 1 day 9 hours ago"

--until or --before

Only show submissions before the specified time

--anthor

Designated author

--commiter

Designated submitter

--grep

Match the keywords in the submission description

--grep=gulp --grep="initial commit"

--S

Only show submissions with a keyword added or removed

--Sfunction_named

5. Other options

  • --decorate Command to view the commit object currently referred to by each branch
$ git log --oneline --decorate
4df268a (HEAD, origin/master, origin/HEAD, master) m_school bug fixed1
a1f8f9f agent phone

Remote warehouse

git tag

Two types of tags: lightweight and annotated

1. Note label

The note tag is a complete object stored in the Git database. They can be verified; they contain the name, e-mail address, date and time of the tagger; it is usually recommended to create a note tag.

Use -a, a for annotated.

$ git tag -a v1.4 -m'my version 1.4'
$ git tag
v0.1
v1.3
v1.4

//View the details of the label
$ git show v1.4
tag v1.4
Tagger: Ben Straub <ben@straub.cc>
Date: Sat May 3 20:19:12 2014 -0700

my version 1.4

commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date: Mon Mar 17 21:52:11 2008 -0700

    changed the version number

2. Lightweight label

No relevant information is saved, just a reference to a specific submission. The lightweight tag essentially stores the submission checksum in a file, and then makes a map correspondence.

//No other parameters are required, and no comment is required
$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5

//Assuming that the commit number of v1.4 is ca82a6, the following two are equivalent, and there will be no additional label information
$ git show v1.4
$ git show ca82a6

3. Tag existing submissions

$ git tag -a v1.2 9fceb02

4. Push tags to remote

By default, the git push command does not send tags to the remote warehouse server. After creating the label, you must explicitly push the label to the shared server.

$ git push origin v1.5
$ git push origin --tags//Push all tags that are not on the remote warehouse

5. Build a branch based on a tag

git checkout -b [branchname] [tagname]

git reset & git checkout

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E7%BD%AE%E6%8F%AD% E5%AF%86

1. 3.districts

| Tree | Purpose | | ————- | ————- | | HEAD | Point to the snapshot of the previous commit, the parent node of the next commit | | Index (staging area) | The expected next commit Snapshot, the area placed after the add operation, what git commitGit looks like when you run | | working Directory | Hard disk local working directory, sandbox |

  • git initAfter execution, only the working directory has the file 1.js (v1)
  • git add 1.jsNow there is 1.js (v1) in the temporary storage area and working directory, but not in the HEAD
  • git commitAfter that, the files in the three areas are now consistent, 1.js (v1)
  • Only when the file collections in the three areas are the same, it git statuswill be displayed as empty

2. git resetThree-step principle

  1. dfd
  2. dsf

Git branch

1. Introduction to Branches

  • Git branch, in fact, is essentially just a variable pointer to the commit object
  • git creates a branch, in fact, it creates a new pointer that can be moved for you
  • Through HEADthis special pointer, GIT knows which branch it is currently on. HEADCan be seen as an alias for the current branch. You can simply think checkoutof where to go, where to HEADmove to
$ git branch dev
  HEAD -> master
             ↓
98ca9 <--- f30ab
             ↑
            dev

$ git checkout -b dev//HEAD will only move when you create a new branch and switch to the dev branch
           master
             ↓
98ca9 <--- f30ab
             ↑
            dev <- HEAD
  • If commit is made on master and dev separately, the history will start to fork
  • Since Git's branch is essentially only a file containing the checksum of the pointed object (a SHA-1 value string with a length of 40), its creation and destruction are extremely efficient. Creating a new branch is like writing 41 bytes (40 characters and 1 newline character) to a file

var gitalk = new Gitalk({ clientID: '82f3fb6574634b43d203', clientSecret: '5790d9fdbfe9340d29810b694b3b2070a9a3b492', repo:'zfengqi.github.io', owner:'zfengqi', admin: ['zfengqi'], id: winMode: true .loc.pathname, }); gitalk.render('gitalk-container');

Reference: https://cloud.tencent.com/developer/article/1563245 "Pro Git" Reading Notes 1-Cloud + Community-Tencent Cloud