Git user manual

Time:2021-2-21

Git user manual

Set the password to be entered only once

git config --global credential.helper cache
git config --global credential.helper store

Git configuration

Weight warehouse > Global > system of GIT configuration file

Pro /. Git / config ᦇ warehouse configuration

Core configuration

[core]
	excludesfile = /Users/roach/.gitignore_ Global # includes a separate configuration file

collocation method

Delete a section

Section in brackets

Format: git config [-- local | -- global | -- system] - remove section
Example: git config -- local -- remove section dog

View value type

You can view four types: - bool, - int, - bool or int, - path

Format: git config [-- local | -- global | -- system] [-- bool | -- int | -- bool or int | -- path] section.key
Example: git config -- local -- bool dog.name

Operate special sections

[remote "origin"]
	url = https://xxxx.git
Example: git config -- local remote.origin.url   value

Rename section

Parameter: -- rename section

Format: git config [-- local | -- global | -- system] - rename-section Section1 section2

Example: for example, let's change the section named dog to dog1, GIT config -- local rename section dog dog1

Replace, get, and delete multiple properties

Parameters: - replace all, - get all, - unset all
Example: git config -- local -- replace all dog. A 333

Using regularity

Parameters: - replace all, - get regexp
Example: 
	For example, let's get the values of all the key values under the core in the configuration
	Use the command: get config -- local -- get regexp core

Common configuration

Alias: [alias]

Color: [color]

[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "status"]
added = yellow
changed = green
untracked = cyan
[color "diff"]
meta = yellow
frag = magenta bold
commit = yellow bold
old = red bold
new = green bold
whitespace = red reverse
[color "diff-highlight"]
oldNormal = red bold
oldHighlight = red bold 52
newNormal = green bold
newHighlight = green bold 22

Core: [core]

[core]
	editor = vim
	excludesfile = ~/.gitignore
	pager = diff-so-fancy | less --tabs=4 -RFX
	autocrlf = input

Certificate: [credential]

[credential]
	helper = cache --timeout=28800
echo "http://$username:[email protected]$remotehost" >> ~/.git-credentials
git config --global credential.helper store

Push: [push]

[push]
	default = current

Git structure chart

img

  • Workspace: workspace
  • Index / stage: staging area
  • Repository: production area (local warehouse)
  • Remote: remote warehouse

Git help view

git [xxx] -h

Git common commands

git init

New code base

Git init # create a new git code base in the current directory
Git init # create a new directory and initialize it as git code base
Git clone # downloads a project and its entire code history
Usage: git init [- Q | -- quiet] [-- bar] [-- template) = [-- shared [=]] []

    --template 
                          The template directory will be used
    --Bar create a pure warehouse
    --Shared [=] specifies that the GIT repository is shared among multiple users
    -q. -- quiet mode
    --separate-git-dir 
                          Separation of GIT directory and workspace

git clone

Usage: git clone [[--] []

    -v. -- more detailed
    -q. -- quiet is quieter
    --Progress force display progress report
    -n. -- no checkout does not create a checkout
    --Bar create a pure warehouse
    --Mirror create a mirror warehouse (also a pure warehouse)
    -l. -- local clone from local warehouse
    --No hardlinks does not use local hard links and always copies
    -s. -- set shared as shared warehouse
    --recurse-submodules[=]
                          Initializing submodules at clone time
    -j. -- the number of sub modules that jobs concurrently cloned
    --template 
                          The template directory will be used
    --Reference repository
    --reference-if-able 
                          Reference warehouse
    --Dissociate only refers to the local repository pointed by reference when cloning
    -o. -- origin uses instead of 'origin' to track upstream
    -b. -- branch detection instead of remote head
    -u, --upload-pack 
                          Remote git upload pack path
    --Depth creates a shallow clone of the specified depth
    --shallow-since 
                          Create a shallow clone from a specific time
    --shallow-exclude 
                          Deepen the history of shallow cloning, except for specific versions
    --Single branch clones only one branch, head or -- branch
    --No tags do not clone any tags, and subsequent fetching operations do not download them
    --The shallow sub modules will be cloned in shallow download mode
    --separate-git-dir 
                          Separation of GIT directory and workspace
    -c, --config 
                          Setting configuration information in new warehouse
    -4, - IPv4 only uses IPv4 address
    -6, - IPv6 uses only IPv6 addresses
    --Filter object filtering

git config

Configure Git

Git config - L # displays all configurations
Git config -- local - L # displays the current git configuration
Git config -- local - e # open configuration file
	xxx/.git/config

Git config -- Global - L # view user configuration
	/home/user/.gitconfig

Git config -- System - L ᦇ view system configuration
	/usr/lcal/etc/gitconfig
Git config -- global # global configuration Git
git config  user.email  " [email protected] "# configure the profile of the current project
git config  user.name  "XXX" # configure the user name of the current project
Usage: git config []

Profile location
    --Global uses global profiles
    --System uses system level configuration files
    --Local uses warehouse level configuration files
    -f. -- file uses the specified configuration file
    --The blob reads the configuration from the given data object

operation
    --Get value: name [value regex]
    --Get all: key [value regex]
    --Get regexp gets the value according to the regular expression: name regex [value regex]
    --Get urlmatch: Section [. Var] URL
    --Replace all the matching variables: name value [value]_ regex]
    --Add adds a new variable: name value
    --Unset delete a variable: name [value regex]
    --Unset all delete all matches: name [value regex]
    --Rename section: old name new name
    --Remove section delete a section: name
    -l. -- list all
    -e. -- edit open an editor
    --Get color to get the configured color: configuration [default]
    --Get colorbool get color settings: configure [stdout is TTY]

type
    -t, --type <>         取值为该type
    --Bool value is "true" or "false"
    --The int value is a decimal number
    --The bool or int value is -- bool or -- int
    --The path value is a path (file or directory name)
    --The expiration date value is an expiration date

other
    -z. -- null the termination value is nul bytes
    --Name only displays only the variable name
    --When querying includes, refer to the include instruction to search recursively
    --Show origin displays the source of the configuration (file, standard input, data object, or command line)
    --The -- get parameter is used for default, and the default value is used when the setting is missing

git add

Add / delete files

Git add [file1] [File2] #
Git add [dir] #
Git add. Adds all files in the current directory to the temporary storage
Usage: git add [] [--]

    -n. -- dry run exercise
    -v. -- verbose output

    -i. -- interactive picking
    -p. -- patch selects data blocks interactively
    -e. -- edit edit the current difference and apply it
    -f. -- force allows you to add ignored files
    -u. -- update updates the tracked file
    --Renormalize returns the tracked file (implied - U) to a new line character
    -N. -- intent to add only records, the path will be added later
    -A. -- all add all changed tracked and untracked files
    --Ignore remove ignores the path removed from the workspace (same as -- no all)
    --Refresh does not add, only refreshes the index
    --Ignore errors skips files that cannot be added due to errors
    --Ignore missing checks whether the file (even if it does not exist) is ignored in drill mode
    --Chmod (+ | -) x covers the executable bits of the files in the list

git stash

Temporarily saving the development progress to the stack will save the changes of the scratch area and workspace

Git stash list # view saved progress list
Git stash save 'comment' # save the current work progress
git stash pop [--index] [stash_ After recovery, the progress will be deleted
Git stash pop # restores to the latest progress to the workspace, including changes to the workspace and staging area
Git stash Pop -- index # restore the latest progress workspace and staging area
Git stash pop stash @ {1} restores the specified progress to the workspace
git stash apply [--index] [stash_ ID] # the progress will not be deleted after recovery
git stash drop [stash_ ID] # delete the specified progress
Git stash clear # delete all progress
git stash show [stash_ ID] [- P] # view the difference between the stash saved in the stack and the current directory. - P display details
git stash show [email protected]{1}
Git stash branch - creates a branch from the latest stash
Git stash apply stash @ {1} // restore the original working environment

git commit

Git commit - M "message" ා commit code
Git commit -- amend - M "message" # to override the last commit to prevent too many commits

git branch

Only operate on branches, do not switch branches

Git branch # lists all local branches
Git branch - R # list all remote branches
Git branch - a # lists all remote and local branches
Git Branch [new branch] #
Git Branch [new branch] [commit] #
Git branch -- track [new branch] [remote branch] #
Git branch -- set upstream [branch] [remote branch] #
Git branch - D # delete branch
Git push origin -- delete [branch name] #
Git push - Dr [origin / branch name] #

git checkout

Git checkout - B # create a new branch and switch to it
Git checkout # switch to the specified branch
Git checkout - switch to the previous branch
Git checkout - B [branch] [tag] #
Git checkout - B [branch] [commit] #
git checkout --track orgin/branch_ Name. If there is no such branch locally, a new branch will be created, which is called branch_ Name, and automatically track the remote branch with the same name. (created locally)
Git checkout [commit] [file] # restore the specified file of a commit to the staging area and workspace
Git checkout. # restore all files in the staging area to the workspace (cancel the add operation)

git merge

Merges the specified branch into the current branch

Git merge [branch] # merges the specified branch to the current branch

git log

Git log -- Online - 3 # view the last three submissions of remote warehouse
Git log - s [keyword] # search submission history according to keywords
Git log -- follow [file] # view the historical version of a file
Git log - 5 -- pretty -- online ා shows the last 5 submissions
Git black [file] # shows who changed the file

git cherry-pick

Reduction and merger

Git cherry pick [commit ID] merges the specified commit to the current branch and submits it
Git cherry pick - n # don't submit automatically after merging
Git cherry pick - e # automatically submit after merging, and customize the submission information

git tag

Git tag # list all tags
Git tag [tag] # create a new tag
Git tag - D [tag] # delete local tag
Git show [tag]? View tag information
git tag -a v1.4 -m 'version 1.4'

git push

git push --set-upstream orgin branch_ Name # create a local branch remotely_ Name branch a branch with the same name and track it (created remotely)
Git push origin: refs / tags / [tagName] # Delete remote Tag
Git push [remote] [tag] #
Git push [remote] - Tags # submit all tags
Git push [remote] [branch] # upload local specified branch to remote warehouse
Git push [remote] - force # push to remote branch
Git push [remote] - all # push all branches to remote warehouse
Git push origin XXX # push the local branch to the remote branch XXX (the remote branch does not exist), and bind the association
Git push origin -- delete [branch name] #
Git push - Dr [origin / branch name] #

git pull

Git pull [remote] [branch] #
Git pull [remote] - all # pull all remote branches

git status

Git status # displays the changed files
git

git diff

Git diff # shows the difference between the staging area and the workspace
Git diff head # displays the difference between the workspace and the latest commit of the current branch
Git diff [first branch]... [Second Branch]
Git diff -- shortstat "@ {0 day ago}" shows how many lines of code you wrote today

git show

Git show displays the metadata and content changes of a submission
Git show -- name only [commit] #
Git show [commit]: [file name] # displays the contents of a file when it is submitted

git reflog

Git reflog # displays the latest mentions of the current branch, even after reset

git fetch

Git fetch [remote] # Download all the changes of the remote repository (when colleagues submit the code to the remote, but you can't see the update locally)

git remote

Git remote - V # displays all remote warehouses
Git remote show [remote] displays the information of a remote warehouse
Git remote add [shortname] [url]? Add a new remote warehouse

git reset

Commit commit without history

Git reset [file] # reset the specified file in the temporary storage area, which is consistent with the last commit, but the workspace remains unchanged
Git reset -- hard [commit] # reset the temporary storage area and work area, keep the same as the last commit (use more frequent operation), re specify to the specified commit version, and use it when the "intermediate version" is no longer used
Git push - F # local is older than remote, so it needs to be pushed

git revert

Commit commit that keeps history

git revert [commit_ ID] # create a new commit to cancel the specified commit, and all changes of the latter will be offset by the former and applied to the current branch (if we want to cancel a previous version, but want to keep the later version of the target version (intermediate version), and record the whole version change process, we can use this method. )
git revert -n [commit_ ID] # - n do not submit automatically
Git commit - M "XX" ා submit new content manually
git push

git archive

Generate a package for publishing

It is recommended to package in the root directory of the code base, otherwise you will encounter various problems.

In the next level directory, only the contents under the directory are packaged by default

Git Archive - L # displays the supported compressed packages
git archive --format  tar.gz  --output "./ output.tar.gz "Master # generates a compressed package according to the specified branch
git archive --output "./ output.tar.gz "# compress the specified format according to the generated file name
git archive --format  tar.gz  --output "./ output.tar.gz "5ca16ac0d603603 # package according to the specified commit

git rm

Git RM - R -- cached. # clear cache

git rev-parse

Git Rev parse head # displays the latest git commit ID
Git Rev parse -- verify # check whether the specified branch exists locally

img

Git usage scenarios

Local git user configuration

-Configure ssh-key:
http://10.10.116.61/help/ssh/README
-Global configuration:
git config --global  user.name  Wu Qiang
git config --global user.email "[email protected]"

-Local configuration:
git config  user.name  Wu Qiang
git config user.email "[email protected]"

Create a new local warehouse

git clone [email protected]:root/kubo_qa.git
cd kubo_qa
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Replace remote branch

cd existing_folder
git init
git remote add origin [email protected]:root/kubo_qa.git
git add .
git commit
git push -u origin master

Connect local branch to remote branch (local code already exists)

cd existing_repo
git remote rename origin old-origin
git remote add origin [email protected]:root/kubo_qa.git
git push -u origin --all
git push -u origin --tags

Connect local branch to remote branch (only local directory is created)

cd existing_folder
git init
git remote add origin [email protected]:root/kubo_qa.git
git add .
git commit -m "Initial commit"
Git push - U origin master (if an error is reported: SRC refspec master does not match any)[
	git push --set-upstream origin master
])

Create branch

git branch wuqiang_kubo

Create branches and switch branches

git checkout -b [branch_name]
git checkout -b wuqiang_kubo
git checkout -b kcmdb_dev_future

Switch branches

git checkout [branch_name]
git checkout wuqiang_kubo

Commit change, commit rollback

1. Submit changes
	git add *
	git commit -m "information"
	git push -n origin master

	Git push origin // push to the remote branch associated with the current local branch
	Git push origin master // push to the remote branch associated with the current local branch. If the remote branch does not exist, the remote branch will be created automatically

1.1. Push local branch local_ Branch to remote branch_ Branch and establish relationship
	a. Remote_ Branch branch and already associated with local branch local_ Branch and local has been switched to local_ branch
	git push
	
	b. Remote_ Branch but not associated with local branch local_ Branch and local has been switched to local_ branch
	git push -u origin/remote_branch
	
	c. No remote_ Branch and local has been switched to local_ branch
	git push origin local_branch:remote_branch

2. Submit rollback
	Git log // view historical submission
	Git revert sfsdfwefsdfwe // rollback to the specified version

Temporary fix bug

Save the temporary workspace [in the middle of the work, you suddenly need to repair a bug. You need to cut out to a branch to repair the bug. After the repair, you will continue the previous development work in the saved workspace.]

Git stash save "work in progress for foo feature" // save the current workspace to the stack
  git branch another_ Bug // create a bug repair branch
  git checkout another_ Bug // switch to the bug repair branch for bug repair
  
  --Repair complete--
  git add .
  git commit -m "xxxx"
  Git push - U origin / Master // submit the repaired branch to the online branch and establish the association relationship

  --Bug fix finished--
  Git stash list // lists all workspaces saved in the stack
  git checkout xxx_ Dev // switch back to the development branch
  Git stash apply stash @ {1} // restore the original working environment

Delete branch

Git branch - D # delete local branch
Git branch - D branchname # delete local branch (this operation will not be performed when the user is on this branch)
Git branch - D branchname # force deletion of local branch
Git branch - R - D origin / branchname
Git push origin -- delete [branch name] #
Git push - Dr [origin / branch name] #

Branch rename

Git branch - M oldbranch new branch // repeat command
Git branch - M oldbranch newbranch // force rename

Remove files from tracked

Add ignore file
Drop the file cat. Gitignore in the project directory
​```
.gitignore
.idea/
#python
*.py[cod]
*.so
*.egg
*.egg-info
dist
build
​```

git rm -r --cached .idea/
Git add XXX // adding the directory to tracked will invalidate the configuration in. Gitignore file


Git commit - M "delete. Idea directory"
git push origin kcmdb_dev
git rm .idea

Tag

git tag -a v1.4 -m 'version 1.4'

Clone the specified branch or tag

git clone  -b 
Example: git clone http://gitlab.cardinfo.com.cn/cmdb/kubo_ forward.git  -b kforward-0.1.0

Recover deleted files

View workspace status
git status

To view deleted files: git LS files -- deleted
Use the command checkout to recover: git checkout -- file_ name
If you want to recover multiple deleted files, you can use the batch command:
git ls-files -d | xargs git checkout --
If you want to restore the modified file, command: git LS files - M | xargs git checkout--

View the detailed modification record of a file
git log -p files

Revocation of document modification

If you want to discard local file modification, you can use

git reset --hard FETCH_ Head // returns to a commit, but all previous changes will be returned to the staging area [not recommended]
Git revert // generate a new commit to revoke a commit, leaving a history. All commit before this commit will be retained
Git revert head ^ // go back to the previous version
Git reverse head ~ n is the number of layers of rollback
git reset HEAD~n
Git reset - H | git reset -- help // view help

Check whether the specified branch exists locally

git rev-parse --verify

Reference link

gitignore.io

In this paper, the blog group issued a multi article and other operational tools platformOpenWriterelease