The correct way to open Git

What is Git

It is a difference-based version controller, which mainly stores the differences accumulated over time in the base file. Most of its operations are performed locally, generally adding data rather than deleting it. The modification has three states: Committed, temporary, modified, and then saved to the temporary area for one-time commit

Git configuration before the first run

  1. Set the username and email address that will be used on every Git commit, recorded on every commit, and cannot be changed

  2. If you don’t want to use the default text editor, you can use ^1^, for example, to change to Emacs. To use other editors on Windows, you must provide the executable ^2^

     1. $ git config --global core.editor emacs
     2. $ git config --global core.editor "url"
    Copy the code

Git command synthesis manual (manPage)

Use it when you need help, no need to connect to the Internet, super convenient

$ git help <verb>
$ git <verb> --help
$ man git-<verb>
Copy the code

You can also use -h to obtain succinct help. The preceding command provides comprehensive help

2.1 Git repository

Create a new Git repository

$git init // create a.git subdirectory that contains all the required initial Git filesCopy the code

Clone an existing warehouse

Use the git clone URL command to clone nearly all the data in your Git repository

$git clone https://github.com/libgit2/libgit2 / / in the current directory to create a libgit2 directory, and under which initializes a. Git folderCopy the code

2.2 Records updated to the warehouse

Tracked files: files that are under version control and that Git is aware of

Check the current file status

Use the git status command

$git status On branch master Your branch is up-to-date with 'origin/master'. Nothing to commit $echo 'My Project' > README $git status On branch master Your branch is up-to-date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) README nothing added to commit but untracked files present (use "git add" to track)Copy the code

Tracking new files

Start tracking a file with git add

$ git add README
Copy the code

Check the current file status again

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)

new file:   README
Copy the code

Changes to be committed: The following is a transient state

Save the modified file temporarily

Changes not staged for committed: A file whose contents have been tracked down but not put into staged storage is updated. To temporarily store this update, run the gir add command, which is a multi-functional command

Git add: Can be used to track new files, to put traced files into staging, and to mark conflicting files as resolved when merging

At this point the file is provisionally stored, what happens if we modify it again before the file is committed? The answer is that it is in both the staging and non-staging areas, but the commit is still the last version of the commit that ran Git Add, so remember staging after making changes anyway

State tabled Jane

The git status command output is quite detailed, but there is a more concise way to look at it

Git status -s M README MM Rakefile A lib/git.rb M lib/simplegit.rb?? LICENSE.txtCopy the code

Among them

?? Newly added untraced file

A Newly added to temporary storage area

M Modified files

Ignore files

We can create a.gitignore file to list the patterns of files to ignore. In general, a repository has only one.gitignore file in the root directory, which is recursively applied to the entire repository, and subdirectories can have additional files of that file, and subdirectories only apply to their directories

GitHub has a very detailed list of.gitignore files: github.com/github/giti…

The format specification is as follows:

Git ignores all empty lines or lines that start with #.

Standard Glob pattern matching can be used, which is applied recursively throughout the workspace.

Matching patterns can prevent recursion by starting with a (/).

A matching pattern can end with a/to specify a directory.

To ignore files or directories outside the specified schema, precede the schema with an exclamation point (!). Invert.

# ignore all.a files *. A # but keep track of all lib.a, even if you ignored.a files earlier! Lib. A # ignore only TODO files in the current directory, not subdir/TODO /TODO # ignore any folder named build under the directory build/ # ignore doc/notes. Doc /server/arch.txt doc/*.txt # Ignore.pdf files in doc/ and all subdirectoriesCopy the code

View staged and unstaged changes

Git diff lets you know which updates are not provisionated and which are provisionated waiting to be committed.

Compared with Git status, git diff shows updates in the format of file patches, which can let us know more specifically where changes have taken place

To check the status, enter git status

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

    modified:   README

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

    modified:   CONTRIBUTING.md
Copy the code

We know that the README file has been modified to cache and the contribuing.md file has been modified to cache. To see what parts of the contribuing.md file have been updated, use git diff

$ git diff diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ebb991.. Md +++ b/CONTRIBUTING. Md @@ - 66,7 + 66,8 @@ Branch Directly, things can get messy. Please include a nice description of your changes when you submit your PR; if we have to read the whole diff to figure out why you're contributing in the first place, you're less likely to get feedback and have your change -merged in. +merged in. Also, split your changes into comprehensive chunks if your patch is +longer than a dozen lines. If you are starting to work on  a particular area, feel free to submit a PR that highlights your work in progress (and note in the PR title that it'sCopy the code

Git diff compares the difference between the current file in the working directory and a snapshot of the staging area, that is, unstaged changes

To see what your next commit will be, you can use Git diff –staged or Git diff –cached(synonyms). This will compare your staged file with the last commit

$ git diff --staged diff --git a/README b/README new file mode 100644 index 0000000.. 03902A1 -- /dev/null +++ b/README @@-0,0 +1 @@ +My ProjectCopy the code

Git diff itself, however, only shows unstored changes

Just like status, if a staged file is not committed, modified, and saved again, the file name will appear both under Git diff commands and Git diff –staged commands, and commit will only commit previously staged portions.

Submit the updates

Git commit is a commit command that launches the text editor of your choice to enter the commit instructions

# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # Your branch is up-to-date with 'origin/master'. # # Changes To be committed: # new file: README # modified: CONTRIBUTING. Md # ". Git /COMMIT_EDITMSG" 9L, 283CCopy the code

When you push out the editor,Git discards the comment lines and generates a commit with the commit instructions

You can also use the commit -m command to place the commit information on the same line as the command

$git commit -m "Story 182: Fix benchmarks for speed" [Master 463dc4f] Story 182: Fix benchmarks for speed 2 files changed, 2 insertions(+) create mode 100644 README // It tells you which fork is currently committedCopy the code

Skip using the staging area

Use git commit -a to temporarily save all traced files and commit them, skipping the git add staging step

But it also adds some unnecessary files

Remove the file

If you simply delete files from your working directory, you’ll see it in the unstaging area when you run Git status

You can do this by removing the traced file from staging and then committing it using git rm

Git rm -f to delete a file that has been modified or placed in the staging area

If you forget to use.gitignore and accidentally add a bunch of comments to the staging area, you can use git rm –cached, followed by the name of the file or directory, or glob mode

$git rm log/\*.log $git rm log/\*.logCopy the code

Move files

If a rename occurs in Git, it does not specifically show that it was a rename

If you want to rename Git, you can use Git mv like this

$ git mv file1_from file2_to
Copy the code

It is equivalent to moving file 1 to file 2, then deleting the original file 1 and staging file 2

$ mv README.md README
$ git rm README.md
$ git add README
Copy the code

2.3 Viewing The Submission History

View submission History

Using the git log command, all commits are listed in chronological order

$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <[email protected]>
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <[email protected]>
Date:   Sat Mar 15 10:31:28 2008 -0700

    first commit
Copy the code

Git log -p or Git log-patch can be used to see the differences introduced by each commit (i.e. patches), and we can use some options to limit the number of logs to be displayed, such as -2 showing only the last two commits

Use git log –stat to see brief statistics for each commit, such as all files that have been modified, how many files have been modified, and which files have been removed or added

Use git log — Pretty to display the commit history in other styles, and there are other sub-options inside, such as Oneline to display each commit on a single line

$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
Copy the code

Use git log –format to customize the display format of records. The output format does not change with git updates

$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test
a11bef0 - Scott Chacon, 6 years ago : first commit
Copy the code
  1. %H– The full hash value submitted

  2. %h– The short hash value for the submission

  3. %T- the full hash of the tree

  4. %t- short for tree hash value

  5. %P- the full hash value submitted by the parent

  6. %p- the shorthand hash value submitted by the parent

  7. %an– author name

  8. %ae- email address of the author

  9. % AD — author revision date (you can customize formatting with the –date= option)

  10. %ar– author revision date, in terms of how long ago

  11. %cn– the submitter’s name

  12. %ce– email address of the submitter

  13. % CD — submission date

  14. %cr– Submission date (how long ago)

  15. %s– Submit the description

git logCommon options for

-p

Shows the differences introduced by each commit in patch format.

–stat

Displays file modification statistics for each submission.

–shortstat

Show only the last row number modification in –stat add remove statistics.

–name-only

A list of modified files is displayed only after the information has been submitted.

–name-status

Displays the list of new, modified, and deleted files.

–abbrev-commit

Only the first few characters of all 40 characters in the SHA-1 checksum are displayed.

–relative-date

Display dates in short relative times instead of full formats (such as “2 weeks ago”).

–graph

Displays branch and merge history in ASCII graphics next to the log.

–pretty

Display historical submission information in another format. The options available include oneline, short, Full, Fuller, and Format (to define your own format).

–oneline

–pretty=oneline –abbrev-commit

Limit output length

Like the -2 option above, limit the display of the last two updates

There are also time limit options, such as –since and –until

$git log --since="2 "$git log --since="2" $git log --since="2 years 1 day 3 minutes ago"Copy the code

There are also submissions subject to specified conditions

Use the –author option to display submissions for the specified author, and use the –grep option to search for keywords in the submission instructions. You can specify multiple –author and –grep as search criteria. You can also add –all-match to output all submissions matching –grep mode

-s takes a string argument and displays only those commits that add or remove the string

$git log -s function_nameCopy the code

If you only care about the historical commits of certain files or directories, you can specify their paths after the log option, using — to separate the options from the paths

limitgit logOptions for output (Summary)

Only the last n commits are displayed.

–since, –after

Only commits after the specified time are displayed.

–until, –before

Displays only commits prior to the specified time.

–author

Displays only submissions whose authors match the specified string.

–committer

Displays only submissions whose submitter matches the specified string.

–grep

Displays only commits that contain the specified string in the commit specification.

-S

Only submissions that add or remove content matching the specified string are displayed.

Hide merge commit

No-merges can merge commits that don’t contain much information to avoid messing up the history

2.4 Undo Operation

Cancel the operation