• 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 atgitBasic digg friends, no corresponding basis may lead to a poor reading experience, you can first click “like” favorites, fromClick here toLearning.
  • gitEssential 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 viewgitversion
git version/--version
Copy the code
  • updategitversion
    1. Windows
    Git update // 2.17.1 and earlier git update-git-for-windows // after 2.17.1Copy the code
    1. Mac
    • useBrewThe installation,Install the Brew.
    brew install git
    brew link git --overwrite
    Copy the code

gitCommand 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,commitAnd 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 togit logFor 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 onesgit add ./*Outside.
git add [filepath] -u
Copy the code
  • -uAdd remotely tracked change files (already associated with the remote repository),filepathOptional file path, do not write the default project root path.
git add -f [filename] 
Copy the code
  • -fForce add file if in.gitignoreHas been ignored in the file, but want to submit to the warehouse do not want to modify.gitignoreDocuments (not submitted by others).
git add . --no-verify
Copy the code
  • --no-verifybypasshusky pre-commitCode specification verification.
git check-ignore -v [ignore filename/filepath]
Copy the code
  • check.gitignoreAre the rules correct?

undo

  • Don’t usegit add, you can usegit checkout.
    1. Discard all current file modifications:git checkout .(Use with caution, all edited files will be thrown away, save manually).
    2. To discard a file:git checkout -- filepathname(such as:git checkout -- readme.mdAnd don’t forget the middle.”--“, if you don’t write, it becomes a check out branch!! .
  • Has been usedgit add, you can usegit reset.
    1. Discard all caches:git reset HEAD ..
  • Have been usinggit commit, you can usegit reset.
    1. git reset --hard HEAD^Go back to the last timecommitIn the state.
git resetExplanation.
/ / merge | keep not commonly used $git reset [- hard | soft | mixed | merge | keep] [commit | HEAD]Copy the code
  • Workspace: not in usegit add; Temporary storage area: Usedgit add.
  1. --mixed: Only reset the staging area and putHEADPoint 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 resetMode is usedmixedMode.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)
  2. --hard:Reset the staging area and workspace, from<commit>Since any changes in the workspace are discarded, and theHEADPoint 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.)
  3. --soft: The contents of the workspace remain unchanged,HEADPoint to the<commit>Since the<commit>All changes made since are rolled back to the staging area and displayed ingit statusChanges to be committedIn the.Rollback to a version, only rollbackcommitThe information. If you still want to submit, just do itcommitCan.)
  • throughgit logIn thecommit_idWe can go back to any previous version.
git reset --hard commit_id
Copy the code
Viewing the Submission Record
  • usegit resetBack to theDelete .DS_Store.

  1. git lg

2. Look at the abovegit lgPrint, we found missing our last submissionlogRecord.When we executegit resetAfter the version rollback, the latest version cannot be passedgit logIf yes, this parameter is requiredgit reflogCommand to query Git operation records, we can find the previous recordcommit idInformation.

Cache current Changes

  • The previously mentionedgit checkout .All workspace changes will be abandoned, so is there any way to save them?
save
  • You can always executegit stash.
The release of
  • popapply
    • pop: Restores to the workspace and starts fromstashDelete the storage area;
    • apply: Restores to the workspace,stashThe 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

  1. The local branch
Git checkout -b branch-name git checkout -b branch-nameCopy the code
  1. Remote branch
Git checkout -b branch-name origin/branch-name git fetch -b branch-name origin/branch-nameCopy the code

Merging branches

  • Merging branchesmergerebaseThe difference.
    1. git logWhen,mergCommands will not be retainedmergeThe branch ofcommit.
    2. Conflict handling:mergeAfter merging, you only need to resolve the conflicts and add them once.reabseAfter resolving the conflict, executegit add .andgit rebase --continueUntil the conflict is handled, no additionalcommit.
    Git add -u git rebase --continue if(git rebase --abort) break; }Copy the code
  • mergeOr you can keep itcommitRecord, 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

  1. Associated remote
// Do not push git pushCopy the code
  1. Unassociated remote (first push)
  • The branch that we create directly locally is the branch that is not remotely associated when we usegit checkout -b branch-name origin/branch-nameIs the associated branch, provided that the remote already exists.
git push --set-upstream origin [branch name]
Copy the code

Delete the branch

  1. Local deletion
Git branch -d [branch name] git branch -d [branch nameCopy the code
  1. 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.