“Code Tailor “provides technology related information and a series of basic articles for front-end developers. Follow the wechat public account” Rookie of Xiaohe Mountain “to get the latest articles.
What is a Git
Before we dive right into Git commands, let’s give you a brief overview of what Git is.
Simply put, Git is a distributed version control system.
If you are not familiar with version control, check Git for information about version control
Pull the project from the remote end, check out the branch locally, add the file to the cache locally, commit and attach the commit, and finally confirm and upload the changes to the remote end (push). This is what Git does for us.
Explanation of the nouns in the picture:
- -Jenny: Well, I’m working in a Workspace.
- Index/Stage: temporary storage area
- A Repository is a Repository.
- Remote: Remote repository
1. Create a code base
Create a new Git 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]
Copy the code
2. The configuration
Git’s Settings file is.gitconfig, which can be in the user home directory (global configuration) or in the project directory (project configuration).
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]"
Copy the code
3. Add or delete files
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] ...
# 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
Branch of 5.
# 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]
# delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
Copy the code
6. Label
# 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
# 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 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
Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Git Your attention and praise will be the strongest motivation for us to move forward! Thank you!