This article refer to the Git–useful Tips article translation, improper, please understand
The purpose of this article is to provide a useful reminder of the frequent use of git management projects. If you're new to Git, you can read the references section first, and then read this article later. Before you introduce GIT commands, you can start by looking at the aliases provided by On-my-zsh.
Basic commands
git config--global user.name "Your name"
git config--global user.email "[Email protected]"
git config--global core.editor <your favorite editor Here>
Ex:git config--global core.editor vim
Git init: Initializes a repo.
Commit structure
git status (GST): View repo status
Work area:
. Git directory
Staging Area
Working directory
git add <filename> (GA): Add a file to staging area
git Add. (GAA): Add all files to staging area
git add *.js: Add all files with the suffix JS to staging area
git rm--cached <file>: Delete a new file from staging area
Git commit-m "My first Commit" (GCMSG): Create a commit with a message
Git commit-v-A (GCA):
-V is the abbreviation for verbose, which displays the difference information and more meaningful information at the bottom.
-A is similar to git Add. , all files that were modified and deleted are added, but the newly created file is ignored
git help <command>: see a guide to the corresponding command
git log (GLG, Glgg, Glo, Glog): View the project's commit history
Staging Area Management
git reset HEAD <filename> (GRH): Delete a modified file from staging area
git reset HEAD (GRH): Remove all modified files from staging area
git checkout <filename> (GCO): Removes a modified file from staging area and revokes changes to the file
Git commit-m "My first Commit"--amend: Add File/Change last commit in staging area
Git commit-v-a--amend (gca! ): Add File/Change last commit in staging area
. Gitignore: Tell git which files are not being added to version tracking
You can use the git add <filename> F command to add a file that is not tracked by the version
git diff <filename> (GD): View change differences based on the last commit of the current file
git diff (gd): View change differences for the last commit based on all files
git reset head~2--soft: Removes the last two commits from the project submission history, but does not discard the file changes
git reset head~2--hard: Removes the last two commits from the project submission history, but discards file changes and new files created in (last two) submissions
git reset <commit>--soft--hard:
--soft: Trace all changed files back to "pending" status
After--hard:commit, any changes to files that were tracked by git are discarded
Git reflog: Show all commits including revoked
git merge <commit hash>: Resubmit (Restore the commit)
git clean-f: Delete files in your working directory that are not versioned by git
Stashed & Branches
Stash
Git stash (Gsta): Move all staging area files to the "storage area", similar to another type of work area
Git stash list: View storage queue (Stash lists)
Git stash apply: Restore the most recent storage to staging area (you can use any of the storage (stashes) in the queue using a command like git stash apply [email protected]{num} (num starts counting from 0)
Git stash clear: emptying the storage queue
Git stash Save "name of the stash": Name the storage setting
Git stash pop (GSTP): Restores the last storage to staging area and deletes the store from the storage queue
git stash Drop (gstd): Delete The most recent storage from the storage queue ([email protected]{0}) (Git stash drop [email protected]{num} Delete the specified storage from the storage queue)
Branch
git checkout-b Dev (gco): Create a dev branch and switch from the current branch to the Dev branch
Git branch (GB): View all Branches
git checkout Master (GCM): Switch to the main branch
Git merge <branch> (GM): Merging branches
Git rebase master: Merges changes from master to the current branch before adding changes to the current branch. If there is a conflict, after resolving the conflict add--continue parameter to continue merging
Git branch-d <branch>: Delete Branch,-D Force Delete Branch
git merge <branch>--squash: Combine multiple commits into one, with the following flow:
# Go to the ' master ' branch
git checkout Master
# Create A temp branch
Git checkout-b Temp
# Merge The feature/x branch into the temp using--squash
git merge feature/x--squash
# See the new modifications/files in the Staging area
git status
# Create the Unified commit
Git commit-m "Add feature/x"
# Delete The feature/x branch
Git branch-d feature/x
The difference between rebase and merge:
Rebase
Submission history (the presentation) is linear
Cons: Deletes the most recent commit and then creates a new commit
If committed to remote, do not use rebase
Merge
Commit history (the show) is forked
For a merge of two branches, a new commit is created
Remote Warehouse Management
git remote add <name> <url>: Add a repository that will be tracked
Git remote RM <name>: Remove a repository
git push <remote> <remote-branch> (GP, GGP): pushes the local commit of the current branch to the remote repository
git fetch <remote> <remote-branch>: Pulls the latest commit from the remote repository to the current (local) branch (<remote>/<branch>), does not merge
Git pull <remote> <remote-branch> (GL, GGL): Pulls the latest commit from the remote repository to the current (local) branch and automatically merges
Git pull--rebase (gup): merge in rebase way instead of merge
Other useful commands
git tag <name>: Create a tag (for example: v1.3)
git push--tags: Push local tags to the remote repository
git push <tag>: pushes the specified local tag to the remote
What is the posture of using the right git? Comes with my git advice