What is Git
1. Basic concepts of Git
A diagram shows you how Git works
Workspace: that is, the project file directory created by us can be visually displayed in the editor; Local Repository: a workspace has a hidden directory, git, which is not a workspace, but a git version repository. The local code can be submitted to this office first when the network is not available; Cache (stage/index): stage, or index. Git /index file. Git /index file. Remote Repository: the server where our code is stored, such as Github, GitLab, code Cloud, etc
2. How Git works
The essence of Git version management is to keep the change history in the process of file modification, which can easily undo the previous changes to the file. Think of it as managing three different boxes, commit History, Index, and Working Directory.
commit history:
The index index:
working directory:
git init
Git will mark every subsequent operation (except for gitignore files). You can use git add to commit to the staging area, and then use git commit to store a snapshot of your files in your git repository.
Git’s basic workflow is to repeat three steps over and over:
❝
Workspace change – add-> staging – commit-> Git repository,
❞
Eventually, the Git repository directory forms a snapshot stack, and each time a new version is created, HEAD points to the latest version.
Git common commands
1. Create a code base
$git init // Create a new git code base in the current directory
$git init [project-name] // Create a new directory and initialize it as a Git code base
$ git clone[url] // Download a project and its entire code history
Copy the code
2. Configure information
Display current Git configuration
$ git config --list
Edit Git configuration files
$ git config -e [--global]
Set user information when submitting code
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
# generate key
$ cd~/.ssh // Check the SSH key of the local host
Ssh-keygen -t rsa -c [email protected] // Enter your email address
Login to Github, Account Settings -->SSH Public keys --> add another Public keys, add the generated Public key;
$SSH -t [email protected] // Connect
Copy the code
3. Add/delete files (folder)
Add the specified file to the staging area
$ git add [file1] [file2] ...
Add the specified directory to the staging area, including subdirectories
$ git add [dir]
Add all files from the current directory to the staging area
$ git add .
Before adding each change, ask for confirmation
For multiple changes to the same file, we can implement multiple commits
$ git add -p
Delete the workspace file and place the deletion in the staging area
$ git rm [file1] [file2] ...
Delete the workspace folder and place the deletion in the staging area
$git rm SRC / -r // -r
# stop tracking the specified file, but it will remain in the workspace
$ git rm --cached [file]
Rename the file and place the rename in the staging area
$ git mv [file-original] [file-renamed]
Copy the code
4. Code submission
Submit the staging area to the warehouse area
$ git commit -m [message]
Submit the file designated for the staging area to the warehouse area
$ git commit [file1] [file2] ... -m [message]
Commit workspace changes since the last commit, directly to the warehouse
$ git commit -a
Display all diff information when submitting
$ git commit -v
Use a new COMMIT instead of the previous commit
If there are no new changes to the code, it is used to overwrite the commit information from the last commit
$ git commit --amend -m [message]
Rework the last commit and include new changes to the specified file
$ git commit --amend [file1] [file2] ...
Copy the code
5. Branch management
# list all local branches
$ git branch
List all remote branches
$ git branch -r
List all local and remote branches
$ git branch -a
Create a new branch, but remain 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 that points to commit
$ git branch [branch] [commit]
Create a new branch and set up a trace 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 trace between an existing branch and a specified remote branch
$ git branch --set-upstream [branch] [remote-branch]
Merge the specified branch into the current branch
$ git merge [branch]
Select a COMMIT and merge it into the current branch
$ git cherry-pick [commit]
# delete branch
$ git branch -d [branch-name]
Recommand the local branch
$ git branch -m [old-branch-name] [new-branch-name]
Rename the remote branch
$ git branch -m [old-branch-name] [new-branch-name]
$git push origin :[old-branch-name]
$git push origin [new-branch name]:[new-branch name] $git push origin [new-branch name]:[new-branch name]
$ git branch --set-upstream [new-branch-name] origin/[new-branch-name]
# delete remote branch
$git push origin --delete [branch-name
$ git branch -dr [remote/branch]
Copy the code
6. Label management
# list all tags
$ git tag
Create a new tag for the current commit
$ git tag [tag]
# Create a new tag before specifying commit
$ git tag [tag] [commit]
# delete the local tag
$ git tag -d [tag]
# delete remote tag
$ git push origin :refs/tags/[tagName]
# check the tag information
$ git show [tag]
# submit the specified tag
$ git push [remote] [tag]
# submit all tags
$ git push [remote] --tags
Create a new branch that points to a tag
$ git checkout -b [branch] [tag]
Copy the code
7. Check information
# show the changed file
$ git status
Display the version history of the current branch
$ git log
Display the commit history and the files that changed each commit
$ git log --stat
# Search submission history by keyword
$ git log -S [keyword]
Display all changes after a commit, with each commit occupying one row
$ git log [tag] HEAD --pretty=format:%s
All changes made after a commit must match the search criteria
$ git log [tag] HEAD --grep feature
Display the version history of a file, including file renaming
$ git log --follow [file]
$ git whatchanged [file]
# display every diff associated with the specified file
$ git log -p [file]
# show the last 5 commits
$ git log -5 --pretty=oneline
Displays the historical version of the current branch
$ git log--pretty=oneline --pretty=oneline
# display all submitted users, sorted by number of submitted users
$ git shortlog -sn
# show when and by whom the specified file was modified
$ git blame [file]
Display the difference between the staging area and the workspace
$ git diff
Show the difference between the staging area and the last commit
$ git diff --cached [file]
Show the difference between the workspace and the latest COMMIT of the current branch
$ git diff HEAD
Display 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}"
Display metadata and content changes for a commit
$ git show [commit]
Display the files that have changed during a commit
$ git show --name-only [commit]
Display the contents of a file at the time of a commit
$ git show [commit]:[filename]
Displays the most recent commits for the current branch
$git refloglogGit reflog: git reflog: git reflog: Git reflog: Git reflog: Git reflog: Git reflog: git reflog
Copy the code
8. Remote synchronization
Download all changes to the remote repository
$ git fetch [remote]
Display all remote repositories
$ git remote -v
Display information about a remote repository
$ git remote show [remote]
Add a new remote repository and name it
$ git remote add [shortname] [url]
Fetch the changes from the remote repository and merge them with the local branch
$ git pull [remote] [branch]
Upload the local branch to the remote repository
$ git push [remote] [branch]
Push current branch to remote repository forcibly, even if there is conflict
$ git push [remote] --force
Push all branches to the remote repository
$ git push [remote] --all
Copy the code
9. Cancel
Restore the specified file from the staging area to the workspace
$ git checkout [file]
Restore the specified files of a COMMIT to the staging area and workspace
$ git checkout [commit] [file]
Restore all files from the staging area to the workspace
$ git checkout .
Reset the specified file in the staging area, same as the last commit, but with the same workspace
$ git reset [file]
Reset the staging area and workspace as the last COMMIT
$ git reset --hard
Reset the pointer to the current branch to commit, and reset the staging area, but the workspace remains the same
$ git reset [commit]
Reset the HEAD of the current branch to the specified COMMIT, and reset the staging area and workspace to the same as the specified COMMIT
$ git reset --hard [commit]
Reset the current HEAD to specify commit, but leave the staging area and workspace unchanged
$ git reset --keep [commit]
Create a commit to revoke the specified commit
All changes of the latter are cancelled out by the former and applied to the current branch
$ git revert [commit]
Uncommitted changes are temporarily removed and moved in later
$ git stash
$ git stash pop
Copy the code
Other 10.
Generate a zip package ready for distribution
$ git archive
Copy the code