- This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
preface
- Git website
- This paper is mainly aimed at
git
Basic digg friends, no corresponding basis may lead to a poor reading experience, you can first click “like” favorites, fromClick here toLearning. git
Essential for project development, we often use only a few commands (Clone, Branch, Checkout, Merge, Add, commit, pull, push…), we often find these completely inadequate when we use them. Here are some of the better ways to use these commands, and some of the useful ones you don’t know about.
SSH Viewing and creating
If the git-bash command does not exist, run the ls -al ~/. SSH // command. Ssh-keygen -t rsa -c ["[email protected]"] // copy id_rsa.pub to id_rsa.pub You can use the notepad // Windows command cat id_rsa.pub // copy all the characters in the output // Mac command pbcopy < ~/. SSH /id_rsa.pub // directly copy to the paste board // put the content in the corresponding Add new SSH to GitHub/GitlabCopy the code
Git update
- Install Git on multiple systems
- To view
git
version
git version/--version
Copy the code
- update
git
version- Windows
Git update // 2.17.1 and earlier git update-git-for-windows // after 2.17.1Copy the code
- Mac
- use
Brew
The installation,Install the Brew.
brew install git brew link git --overwrite Copy the code
git
Command document
- Git command official document
Git help/--help [command name] // Windows: D:\ git \mingw64\share\doc\git-docCopy the code
Global configuration
Git config --system --list git config --global --list git config --local --listCopy the code
User name and password
Git config -- -- global user.name "user name" git config -- -- global user.email "user email --unset credential.helperCopy the code
Command alias
- In use, some commands are very long (for example:
branch
,commit
And so on), we can replace it with an alias. In addition, we can configure aliases for specific commands. - Aliases can be configured as you like, not exactly like mine.
git branch -> git bc
git commit -> git cm
git log -> git config --global alias.lg "log --pretty=oneline --graph"
...
Copy the code
- In order to
git log
For example:git log
git config --global alias.lg "log --pretty=oneline --graph"
- This is not greatly save our input command time, improve our development efficiency. Does it make other colleagues feel more advanced?
File operations
add
- Except for the usual ones
git add ./*
Outside.
git add [filepath] -u
Copy the code
-u
Add remotely tracked change files (already associated with the remote repository),filepath
Optional file path, do not write the default project root path.
git add -f [filename]
Copy the code
-f
Force add file if in.gitignore
Has been ignored in the file, but want to submit to the warehouse do not want to modify.gitignore
Documents (not submitted by others).
git add . --no-verify
Copy the code
--no-verify
bypasshusky pre-commit
Code specification verification.
git check-ignore -v [ignore filename/filepath]
Copy the code
- check
.gitignore
Are the rules correct?
undo
- Don’t use
git add
, you can usegit checkout
.- Discard all current file modifications:
git checkout .
(Use with caution, all edited files will be thrown away, save manually). - To discard a file:
git checkout -- filepathname
(such as:git checkout -- readme.md
And don’t forget the middle.”--
“, if you don’t write, it becomes a check out branch!! .
- Discard all current file modifications:
- Has been used
git add
, you can usegit reset
.- Discard all caches:
git reset HEAD .
.
- Discard all caches:
- Have been using
git commit
, you can usegit reset
.git reset --hard HEAD^
Go back to the last timecommit
In the state.
git reset
Explanation.
/ / merge | keep not commonly used $git reset [- hard | soft | mixed | merge | keep] [commit | HEAD]Copy the code
- Workspace: not in use
git add
; Temporary storage area: Usedgit add
.
--mixed
: Only reset the staging area and putHEAD
Point to the<commit>
, but does not reset the workspace, local file changes are not affected.This mode is the default mode, that is, when the notification is not displayedgit reset
Mode is usedmixed
Mode.Changes to files in the workspace are retained, not discarded, but are not marked asChanges to be committed
, but the file is not updated.(Roll back to a version, keep only the source code, roll back the COMMIT and index information)--hard
:Reset the staging area and workspace, from<commit>
Since any changes in the workspace are discarded, and theHEAD
Point to the<commit>
.(If you go back to a version completely, the native source code will also change to the content of the previous version.)--soft
: The contents of the workspace remain unchanged,HEAD
Point to the<commit>
Since the<commit>
All changes made since are rolled back to the staging area and displayed ingit status
的Changes to be committed
In the.Rollback to a version, only rollbackcommit
The information. If you still want to submit, just do itcommit
Can.)
- through
git log
In thecommit_id
We can go back to any previous version.
git reset --hard commit_id
Copy the code
Viewing the Submission Record
- use
git reset
Back to theDelete .DS_Store
.
git lg
2. Look at the abovegit lg
Print, we found missing our last submissionlog
Record.When we executegit reset
After the version rollback, the latest version cannot be passedgit log
If yes, this parameter is requiredgit reflog
Command to query Git operation records, we can find the previous recordcommit id
Information.
Cache current Changes
- The previously mentioned
git checkout .
All workspace changes will be abandoned, so is there any way to save them?
save
- You can always execute
git stash
.
The release of
pop
和apply
pop
: Restores to the workspace and starts fromstash
Delete the storage area;apply
: Restores to the workspace,stash
The storage area is reserved and needs to be manually deleted.
git stash pop
git stash apply stash@{[index]}
Copy the code
delete
Git stash drop stash@{[index]} // Clear all git stash clearancesCopy the code
To view
Git stash show stash@{[index]}Copy the code
Deleting a Remote File
Git rm -rf --cached [filenameCopy the code
- After deleting, submit push again.
Branch management
Create a branch
- The local branch
Git checkout -b branch-name git checkout -b branch-nameCopy the code
- Remote branch
Git checkout -b branch-name origin/branch-name git fetch -b branch-name origin/branch-nameCopy the code
Merging branches
- Merging branches
merge
和rebase
The difference.git log
When,merg
Commands will not be retainedmerge
The branch ofcommit
.- Conflict handling:
merge
After merging, you only need to resolve the conflicts and add them once.reabse
After resolving the conflict, executegit add .
andgit rebase --continue
Until the conflict is handled, no additionalcommit
.
Git add -u git rebase --continue if(git rebase --abort) break; }Copy the code
merge
Or you can keep itcommit
Record, but only in the event of a conflict, after resolving the conflict will automatically create onecommit
. If you want to generate one automatically without conflictscommit
, recording the merge can be usedgit merge --no-ff
.
Push the branch
- Associated remote
// Do not push git pushCopy the code
- Unassociated remote (first push)
- The branch that we create directly locally is the branch that is not remotely associated when we use
git checkout -b branch-name origin/branch-name
Is the associated branch, provided that the remote already exists.
git push --set-upstream origin [branch name]
Copy the code
Delete the branch
- Local deletion
Git branch -d [branch name] git branch -d [branch nameCopy the code
- Remote delete
git push origin --delete [branch name]
Copy the code
The Tag management
create
Git tag [tag version] // Commit git tag -a [tag version] -m [tag description] // Commit git tag [tag version] commit_id // Git log commit ID tag previous commitCopy the code
To view
Git tag show [tag version] git tagCopy the code
push
Git push origin [tag version] // git push originCopy the code
delete
Git tag -d [tag version] git push origin :refs/tags/[tag versionCopy the code
reference
- Git command official document
- Git git
- Git Common Operations Guide
conclusion
- Please feel free to discuss in the comments section. The nuggets will draw 100 nuggets in the comments section after the diggnation project. See the event article for details.
Past wonderful
- Doesn’t the front end know Nginx yet? Come and learn
- VS Code’s guide to improving development efficiency, quality, and coding experience
- Gold nine front-end interview summary!
- Build from 0 Vite + Vue3 + element-plus + VUE-Router + ESLint + husky + Lint-staged
- “Front-end advanced” JavaScript handwriting methods/use tips self-check
- Public account open small program best solution (Vue)
- Axios you probably don’t know how to use
“Likes, favorites and comments”
❤️ follow + like + comment + share ❤️, lingering fragrance in hand, thank 🙏 everyone.