Pro Git study notes

  Front end, git, github

Pro Git study notes

Document address:Pro Git
Original address:PRO GIT study notes

1. Git Starts

Configuration before first running Git

User information

git config --global user.name "your user name"
git config --global user.email "your email address"

Text editor

To set the default text editor:git config --global core.editor emacs

View configuration information

git config --list

2. Git foundation

Create Git warehouse

Initialize a new warehouse in the working directory

git init

Clone existing warehouse

git clone url

urlpointssshAndhttpsTwo are recommended.ssh.

Check current file status

git status

Track the latest files

git add 文件名或*.js/css/html...或.

Ignore unwanted files

cat .gitignore

View staged and unscheduled updates

git diff
git diff --cached

Submit updates

git commit -m "提交备注信息"

Skip using staging area

git commit -a "提交备注信息"

Use when submittinggit commit -aThe tracked temporary files will be submitted together and skippedgit addStep, that is, the two commands are merged.

Remove file

git rm

Remove and delete the specified files in the working directory from the list of tracked files, first usegit statusCheck the list of trace files before using them.git rmFor precise removal.

Force removal for usegit rm -fHowever, it is not recommended.

To delete a file from a remote repository, use:

git rm --cached 文件名/*.文件后缀/文件夹...

Move file

To rename or move a file, you can use:

git mv file_from file_to

View submission history

git log
git log -p -2

-pOptions expand to show content differences for each submission, using-2Displays the last two updates.

For word-level comparison, use:

git log -p -U1 --word-diff

This command is less used in code checking and more used in graphic editing.

To display summary information, use:

git log --stat

Other useful commands:

--prettyThe option can specify that the submission history be presented in a way completely different from the default format, usingonelinePutting each submission on one line is useful when the number of submissions is large:

git log --pretty=online

formatYou can customize the record format to be displayed:

git log --pretty=format:"%h - %an, %ar : %s"

The Writing of Common Format Placeholder and Its Significance

Options explain
%H Submit object (commitThe full hash string of the
%h Submit a short hash string for the object
%T Tree object (treeThe full hash string of the
%t A short hash string for the tree object
%P Parent object (parentThe full hash string of the
%p A short hash string for the parent object
%an Author (author) name
%ae The author’s e-mail address
%ad The author revised the date (can be used-date=Option customization format)
%ar The author’s revision date is displayed in the way how long ago
%cn Submitted by (committer) name
%ce The e-mail address of the submitter
%cd Date of submission
%cr Date of submission, how long ago
%s Submit instructions

Add simple graphics represented by ASCII strings

git log --pretty=format:"%h %s" --graph

Options supported by git log command

Options explain
-p Displays the difference between each update in patch format.
--word-diff Pressword diffFormat display difference.
--stat Displays the file modification statistics for each update.
--shortstat Show only--statAdd and remove statistics for the last row number modification in.
--name-only The list of modified files is displayed only after the information is submitted.
--name-status Displays the list of documents added, modified and deleted.
--abbrev-commit Show onlySHA-1The first few characters of, not all of40Characters.
--relative-date Use a shorter relative time display (for example, “2 weeks ago”)。
--graph displayASCIIThe branch merging history of the graphical representation.
--pretty Use other formats to display historical submission information. Available options includeoneline,short,full,fullerAndformat(followed by the specified format).
--oneline --pretty=oneline --abbrev-commitThe simplified usage of.

Limit output length

A time-bound order--sinceAnd--until

git log --since=2.weeks

Search criteria

  • --authorDisplays submissions by the specified author
  • --grepSearch for keywords in submission instructions
  • --all-matchSubmit search criteria that satisfy both options at the same time

Other common similar options

Options explain
-(n) Show only the most recentnArticle submission
--since,--after Only submissions after the specified time are displayed.
--until,--before Only submissions before the specified time are displayed.
--author Only submissions related to the specified author are displayed.
--committer Only submissions related to the specified submitter are displayed.

Specific examples:

git log --pretty="%h - %s"  --author=gitster --since="2018-10-01" \ --before="2008-11-01" --no-merges -- t/

Undo operation

Modify Last Submission

git commit --amend

Cancels files that have been temporarily saved

git reset HEAD 文件名

Cancel changes to files

This command is used cautiously.

git checkout -- 文件名

Use of remote warehouses

View the current remote library

git remote

Displays the corresponding clone address

git remote -v

Add remote warehouse

git remote add [shortname] url

Capture warehouse information

git fetch [shortname]

Grabbing Data from Remote Warehouse

This command retrieves data from the remote warehouse to the local

git fetch [remote-name]

Capture the updated data of the cloned remote warehouse

git fetch origin

fetchThe command only captures the data from the remote warehouse locally and will not automatically merge into the current working branch.

Recommended command to pull remote warehouse data and merge data

git pull

Push data to remote warehouse

git push origin master

View remote warehouse information

git remote show [remote-name]

Delete and Rename of Remote Warehouse

Rename remote warehouse

git remote rename
git remote rename vue react

Remove remote warehouse

git remote rm vue

Label

Show existing labels

git tag

Set criteria to search

git tag -l "v1.4.2.*"

New label

Lightweight label

git tag

Labels with Notes

git tag -a
git tag -a v1.4 -m "my version 1.4"

View the version information of the corresponding label

git show v1.4

Sign a label

git tag -s
git tag -s v1.5 -m "my signed 1.5 tag"

Verification label

git tag -v [tag-name]
git tag -v v1.4.2.1

Post-labeling

Forget to label, just keep up with the checksums of the corresponding submitted objects when labeling.

git tag -a v1.2 9fceb02

Share tags

git push origin v1.5

Push all locally added labels at once

git push origin --tags

Tips and Tricks

Automatic completion

windowsPress continuously under the systemTabKey

Git command alias

git config --global alias.ci commit
git config --global alias.st status

Git branch

New testing branch

git branch testing

Switch to testing branch

git checkout testing

New and Merged Branches

The above two commands are combined

git checkout -b testing

Git will restore the contents of the working directory to a snapshot of the committed object it points to when a branch is checked out. It automatically adds, deletes, and modifies files to ensure that the contents of the directory are exactly the same as when it was submitted.

Merge submissions


git merge
git checkout master
git merge hotfix

Delete working branch

git branch -d hotfix

Merger of branches

View conflicts git status

Calling Visual Merge Tool to Resolve Conflicts

git mergetool

Branch management

View information about the last submitted object for each branch

git branch -v

See which branches have been merged into the current branch

git branch --merged

View branches that have not been merged

git branch --no-merged

Work flow for branch development

Long-term branch

Characteristic branch

Remote branch

Synchronize data on remote servers to local

git fetch origin 

Push local branch

git push origin master

A new branch is formed on the remote branch:

git checkout -b serverfix origin/serverfix

Tracking remote branches

git checkout -b sf origin/serverfix

Delete remote branch

Derivation of Branches

Integrated branching method

git merge
git rebase

The history of branching out from one feature branch to another.

git rebase --onto master server client

git checkout master
git merge server

Derivative risks

Once the submitted objects in the branch are published to the public warehouse, never perform derivation operation on the branch.

Git on server

Protocol

Git can carry out data transmission by four main transmission protocols: local protocol, SSH protocol, Git protocol and HTTP protocol.

Deploy Git on server

git clone --bare my_project my_project.git

Move bare warehouse to server

To be continued …