preface
Recently, the students in the project team met some problems with Git. They spent half a day sorting out the common commands for git. They thought they could simply sort them out for easy checking and memorizing.
Git commands are used to record and summarize git commands
Create a warehouse
clone
# clone warehouse
git clone< > warehouse# specify branch, path clone repository
git clone-b < branch > < repository > [< path >]Clone repository (default: origin)
git clone-o <remote name>Copy the code
init
Create an empty Git repository (or reinitialize an existing repository)
git init
Copy the code
Check history and status
log
View the current commit log
git log
View the commit log for the specified branch
git log <branch name>
Display the differences introduced by each commit in patch format
git log -p
Only the last n commits are displayed
git log -<n>
Show differences in some file or directory history commits
git log -p <path>
Display file modification statistics for each commit
git log --stat
View a concise version of history
git log --oneline
Copy the code
status
Check the current version status
git status
Copy the code
show
# View the latest commit
git show
Select commit
git show [<commit>]
View changes to a file or directory in the specified COMMITGit show [<commit>]Copy the code
blame
# View the historical changes of the specified file in a list
git blame <file>
Copy the code
reflog
Check the records of commit, merge, and reset operations
git reflog
Copy the code
Work on the current change
add
Add one or more files (or directories) to the staging areaGit add [< path >]Add all files in the current directory to the staging area
git add .
Copy the code
mv
Move or rename a file, directory, or symbolic link
git mv [file] [newfile]
# -f Forcible operation
git mv -f [file] [newfile]
Copy the code
rm
Delete files from the staging area and workspace
git rm -f <file>
Copy the code
reset
# --mixed: default. Resetting the index without resetting the work tree, workspace file contents remain the same (back to before git Add)
# --soft: Keep the working directory and put the new differences made by the rollback HEAD into staging (back to git add). After)
# --hard: Undo all uncommitted changes in the workspace, return the staging area and workspace to the previous version, and delete all previous information commits
-- Merge: Similar to --hard, except that if you make changes to files before executing reset and do not commit (add or commit), merge keeps the changes. Hard does not. [Note: Merge and Hard delete changes if they are added or committed.]
git reset [--soft | --mixed | --hard | --merge] [<commit>]
Cancel the temporary storage of a file
git reset HEAD <file>
Copy the code
revert
# restore a commit (generates a new commit)
git revert <commit>
When a commit is recovered, the merge continues after the conflict is resolved
git revert --continue <commit>
# Abandon this restore
git revert --abort
Copy the code
Expand, mark, and tune history
branch
# View local branches
git branch
# View remote branches
git branch -r
View all branches, local and remote
git branch -a
Create local branch (current branch unchanged)
git branch <branch name>
Change the local branch name
If the branch name is changed, the reflog record is also changed
# -m: If the new branch name already exists, the change will also be performed
git branch (-m | -M) <oldbranch> <newbranch>
# delete local branch
# -d: If there is an uncommitted commit in the branch, the delete will fail
# -d: Forcible deletion
Git push
git branch (-d | -D) <branch name>
/
: local branch name;)
git branch --set-upstream-to=<remote name>/<branch name> <local branch name>
Copy the code
checkout
# Switch branches
git checkout <branch name>
Create a new branch based on the current branch and switch to the new branch
git checkout -b <branch name>
Discard changes to a file
git checkout -- <file path>
Discard all file modifications
git checkout .
Copy the code
commit
Commit staging area to local warehouse
git commit -m <msg>
Commit the file specified in the staging area to the local repository
git commit [file1] [file2] ... -m <msg>
# skip git add
git commit -am <msg>
< MSG >
# (Don't try to modify a push to a remote commit, and don't use an amend for a public commit)
git commit --amend
< MSG >
git commit --amend --no-edit
Copy the code
diff
Compare files in the staging area and the workspace
git diff [file]
Compare all files
git diff .
# show changes that have been added to the cache but have not yet been committed
git diff --cached
Copy the code
merge
# merge generates a new COMMIT
# merge
into the current branch
git merge <branch name>
If a merge conflict occurs, terminate the merge
git merge --abort
In case of merge conflict, continue merge
git merge --continue
Copy the code
rebase
# rebase does not generate a new COMMIT
# merge
into the current branch
git rebase <branch name>
If a merge conflict occurs, terminate the merge
git rebase --abort
In case of merge conflict, continue merge
git rebase --continue
Copy the code
tag
# see the tag
git tag
# check the tag details
git show <tag name>
# to create the tag
git tag <tag name>
# create tag with remarks
git tag -a <tag name> -m <msg>
Push to remote
git push <remote name> <tag name>
# remove the tag
git tag -d <tag name>
# delete remote tag
git push <remote name> :refs/tags/<tag name>
Copy the code
stash
# temporary save current changes (newly added temporary on top)
git stash
# Add remarks, save current changes temporarily
git stash save <msg>
# view all temporary storage
git stash list
# view temporary storage for a particular time
git stash show -p stash@{n}
# Apply a temporary save (will not be removed from the temporary list)
git stash apply stash@{0}
# retrieve a temporary store (will be removed from the temporary store list)
git stash pop stash@{0}
Delete the first staging in the staging area
git stash drop stash@{0}
Clear the temporary list
git stash clear
Copy the code
synergy
fetch
The latest content of the remote host is pulled locally, and the user checks it to decide whether to merge it into the working local branch
git fetch <remote name>
# merger
git merge <remote name>/<branch name>
FETCH_HEAD (the latest status of a branch on the server)
git fetch <remote name> <branch name>
# Check for updates
git log -p FETCH_HEAD
# merge the latest content into the current branch
git merge FETCH_HEAD
Copy the code
pull
# Merge code from remote to local
git pull <remote name> <remote branch>
# Merge irrelevant history
git pull <remote name> <remote branch> --allow-unrelated-histories
Copy the code
push
# Push local code to remote and merge
git push <remote name> <remote branch>
# delete remote branch
git push <remote name> --delete <branch name>
# push local code to remote merge, associate remote branch
git push [--set-upstream | -u] <remote name> <branch name>
Copy the code
other
config
# Check the configuration
git config [--global | --local | --system] --list
Configure git information
git config [--global | --local | --system] user.name "xxx"
git config [--global | --local | --system] user.email "[email protected]"
# check case sensitivity
git config --get core.ignorecase
Copy the code
remote
# view all remote hosts
git remote
Check the url of the remote host
git remote -v
View details about the remote host
git remote show <remote name>
Add remote version libraryGit remote add <remote name>Delete a remote hostGit remote rmChange the name of the remote host
git remote rename <old remote name> <new remote name>
Copy the code
The last
Scattered sorting out some, there are wrong places hope to help correct, also look forward to everyone to add ~