Git Help
The approximate method used by Git
When using Git for the first time, you need to configure your username and email address correctly
git config --global user.name "My Name"
git config --global user.email "[email protected]"
About Git, GitHub
Git is a free and open source distributed version control system that can process projects quickly and efficiently.
GitHub is a remote repository that can create and manage programmers, and it can also facilitate project collaboration.
Git basic operation process
Initialize a git repository, store all files in the directory, and finally upload the snapshot.
git init
git add .
git commit -m 'initial commit'
Create and switch to a branch called featureA, edit and store some files in it, and finally submit a new snapshot.
git branch featureA
git checkout featureA
(edit files)
git add (files)
git commit -m 'add feature A'
Switch back to the master branch, restore the changes made in featureA before, edit some files and put them on the master, and submit the snapshot.
git checkout master
(edit files)
gir commit -a -m 'change files'
Merge the changes of featureA into the master, and then delete the featureA branch.
git merge featureA
git branch -d featureA
Use github as a remote repository
Add remote warehouse
git remote add [shortname] [url]
Generate SSH Key
ssh-keygen -t rsa -C "[email protected]"
Back [email protected] changed your mailbox registered on Github, you'll be asked to confirm the path and enter the password, use the default way to enter the line.
If successful , a .ssh folder will be generated under ~/ , go in, open id_rsa.pub , and copy the key inside .
# 大概是下面这个样子
$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/tianqixin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): # 直接回车
Enter same passphrase again: # 直接回车
Your identification has been saved in /Users/tianqixin/.ssh/id_rsa.
Your public key has been saved in /Users/tianqixin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:MDKVidPTDXIQoJwoqUmI4LBAsg5XByBlrOEzkxrwARI [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|E*+.+=**oo |
|%Oo+oo=o. . |
|%**.o.o. |
|OO. o o |
|+o+ S |
|. |
| |
| |
| |
+----[SHA256]-----+
Back on github, go to Account => Settings (account configuration).
Select SSH and GPG keys on the left , then click the New SSH key button, title set the title, you can fill in and paste the key generated on your computer.
Enter the following code to verify success
$ ssh -T [email protected]
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # 输入 yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access. # 成功信息
**Click "New repository" after logging in **
Then fill in runoob-git-test (remote warehouse name) in Repository name, keep the default settings for others, click the "Create repository" button to successfully create a remote repository
noun | Translated name | concept |
---|---|---|
Workspace | Work area | Is the directory you can see on your computer |
Index / Stage | storage cache | It is generally stored in the index file (.git/index) under the .git directory, so we sometimes call the temporary storage area an index. |
Repository | Warehouse area | The warehouse is divided into remote warehouse and local warehouse, which is the place for storage |
tag | label | If you reach an important stage and want to always remember that particular commit snapshot, submit a label |
New code base
Create a new Git code repository in the current directory
git init
Create a new directory and initialize it as a Git code base
git init [project-name]
Download a project and its entire code history
git clone [url]
Configuration
Show current Git configuration
git config --list
Edit Git configuration file
git config -e [--global]
Set user information when submitting code
git config [--global] user.name "[name]"
git config [--global] user.email "[email address]"
Add and delete files
Add the specified file to the staging area
git add [file1] [file2] ...
Add the specified directory to the temporary storage area, including subdirectories
git add [dir]
Add all files in the current directory to the temporary storage area
git add .
Before adding each change, it will ask for confirmation
For multiple changes of the same file, it can be submitted in stages
git add -p
Delete the work area file, and put this deletion into the temporary storage area
git rm [file1] [file2] ...
Stop tracking the specified file, but the file will remain in the workspace
git rm --cached [file]
Rename the file and put this rename into the temporary storage area
git mv [file-original] [file-renamed]
Code submission
Submit temporary storage area to warehouse area
git commit -m [message]
Submit the designated files in the temporary storage area to the warehouse area
git commit [file1] [file2] ... -m [message]
Submit the changes in the work area since the last commit, directly to the warehouse area
git commit -a
Display all diff information when submitting
git commit -v
Use a new commit to replace the previous commit
If there is no new change in the code, it is used to rewrite the submission information of the last commit
git commit --amend -m [message]
Redo the last commit and include the new changes in the specified file
git commit --amend [file1] [file2] ...
Branch
List all local branches
git branch
List all remote branches
git branch -r
List all local branches and remote branches
git branch -a
Create a new branch, but still stay in the current branch
git branch [branch-name]
Create a new branch and switch to that branch
git checkout -b [branch]
Create a new branch and point to the specified commit
git branch [branch] [commit]
Create a new branch and establish a tracking relationship with the specified remote branch
git branch --track [branch] [remote-branch]
Switch to the specified branch and update the workspace
git checkout [branch-name]
Switch to the previous branch
git checkout -
Establish a tracking relationship between the existing branch and the designated remote branch
git branch --set-upstream [branch] [remote-branch]
Merge the specified branch to the current branch
git merge [branch]
Choose a commit and merge into the current branch
git cherry-pick [commit]
Delete branch
git branch -d [branch-name]
Delete remote branch
git push origin --delete [branch-name]
git branch -dr [remote/branch]
label
List all tags
git tag
Create a new tag in the current commit
git tag [tag]
Create a new tag at the specified commit
git tag [tag] [commit]
Delete local tag
git tag -d [tag]
Delete remote tag
git push origin :refs/tags/[tagName]
View tag information
git show [tag]
Submit the specified tag
git push [remote] [tag]
Submit all tags
git push [remote] --tags
Create a new branch, pointing to a tag
git checkout -b [branch] [tag]
View information
Show changed files
git status
Display the version history of the current branch
git log
Show the commit history and the files that have changed each time
git log --stat
Search submission history, according to keywords
git log -S [keyword]
Show all changes after a certain commit, each commit occupies one line
git log [tag] HEAD --pretty=format:%s
Show all changes after a commit, and its "submission description" must meet the search criteria
git log [tag] HEAD --grep feature
Display the version history of a file, including file rename
git log --follow [file]
git whatchanged [file]
Show every diff related to the specified file
git log -p [file]
Show the last 5 commits
git log -5 --pretty --oneline
Show all submitted users, sorted by the number of submissions
git shortlog -sn
Show who modified the specified file and when
git blame [file]
Show the difference between staging area and work area
git diff
Show the difference between the staging area and the previous commit
git diff --cached [file]
Show the difference between the workspace and the latest commit of the current branch
git diff HEAD
Show the difference between two commits
git diff [first-branch]...[second-branch]
Show how many lines of code you wrote today
git diff --shortstat "@{0 day ago}"
Show metadata and content changes of a submission
git show [commit]
Show files that have changed in a commit
git show --name-only [commit]
Display the contents of a file at the time of a submission
git show [commit]:[filename]
Show the last few commits of the current branch
git reflog
Remote sync
Download all changes in the remote warehouse
git fetch [remote]
Show all remote warehouses
git remote -v
Display information about a remote warehouse
git remote show [remote]
Add a new remote warehouse and name it
git remote add [shortname] [url]
Retrieve changes in the remote warehouse and merge with the local branch
git pull [remote] [branch]
Upload the local designated branch to the remote warehouse
git push [remote] [branch]
Forcibly push the current branch to the remote warehouse, even if there is a conflict
git push [remote] --force
Push all branches to the remote warehouse
git push [remote] --all
Revoke
Restore the specified files in the temporary storage area to the work area
git checkout [file]
Restore the specified file of a commit to the temporary storage area and work area
git checkout [commit] [file]
Restore all files in the temporary storage area to the work area
git checkout .
Reset the specified file in the temporary storage area to be consistent with the last commit, but the work area remains unchanged
git reset [file]
Reset the temporary storage area and work area to be consistent with the last commit
git reset --hard
Reset the pointer of the current branch to the specified commit, and reset the temporary storage area, but the work area remains unchanged
git reset [commit]
Reset the HEAD of the current branch to the specified commit, and reset the temporary area and work area at the same time, consistent with the specified commit
git reset --hard [commit]
Reset the current HEAD to the specified commit, but keep the temporary storage area and work area unchanged
git reset --keep [commit]
Create a new commit to cancel the specified commit.
All changes in the latter will be offset by the former and applied to the current branch
git revert [commit]
Temporarily remove uncommitted changes and move in later
git stash
git stash pop
other
Generate a compressed package for publishing
git archive
This document refers to the rookie tutorial