When I first graduated, I used SVN for a very short time, and I have been using Git for code version control since then. For about 4 years, I made some records and experiences in the process of using Git. Share it with you here, you may be able to absorb some useful things from it.
Whether it is github, gitlab, or other code hosting platforms, code management is done with git. git can be said to be a must-have skill for a programmer, and it is very helpful for work and interviews.
- Git common commands
- Some practices for using Git gracefully
- oh-my-zsh common commands
Git common commands
- git clone the remote branch repository:
git clone -b branch name remote address
When git clones a remote repository project without specifying a branch, only the contents of the default branch will be cloned. - View git username and password
git config user.name
git config user.email
- Branch related
git branch (view current branch)
git branch -a (see all branches)
git checkout branch name (switching to the corresponding branch) will automatically update the code to the branch code
git branch branch name (create a branch)
git branch -d branch name (delete a branch)
git branch -D branch name (force delete an unmerged branch)
git checkout -b branch name [based on branch name or commit value] (switch branch and switch directly past)
- View git history
history
- Search git history by keyword
history | grep push
- View commit history
git log
git log --summary
- Set up a git account
git config --global user.name "Frankkai"
git config --global user.email "gaokai20100801@gmail.com"
git config user.name "Frankkai"
git config user.email "gaokai20100801@gmail.com"
- View git account
git config --global --list
git config --local --list
- Only view the configuration of a certain item
git config --local user.name
- rollback this change
git reset HEAD static/lib/js/constantsUrl.js
git checkout -- static/lib/js/constantsUrl.js
- View the modified code
git diff
git diff HEAD
git diff --staged
- After submitting, I found that several files were lost and not submitted
Found that the modification record was lost and re-addedgit add "*.html"
Resubmit, end up with only one commitgit commit --amend
- Cache files with a certain suffix
git add "*.js"
- Clear files in cache
git reset octofamily/octodog.txt
- Completely delete files with a certain suffix
git rm "*.txt"
- merge branch to master
git merge branch name
- add . to uncommit some files before
git checkout -- <filename>
- Hide code to dirty directory (applicable to other members who have modified the same branch code, but don't want to commit)
git stash
- Free dirty directory code
git stash pop
- Free the specified dirty directory code
git stash pop stash@{0}
- Delete the remote branch (this branch must be a non-default branch)
git push origin --delete branchname
- Has been committed, forced to roll back to the old version
git log
//find the commit hash valuegit reset --hard hash value
- View the stash directory
git stash list
- delete a stash
git stash drop stash@{0}
- Set the remote warehouse address
git remote set-url origin git@foo.bar.com:baz/helloworld.git
- A new branch was created locally, but orgin did not, before the push code
git push --set-upstream origin preproduction
- Specify tag to remote
git push origin <tag_name>
- Hit all tags to remote
git push --tags
- View current tags
git tag --list
- Just deleting the index does not delete the .idea file on the working tree
git rm --cached -r .idea
// --cached only deletes the index, -r (recursive) recursively deletes all files in the .idea directory - Git actively tracks files, controls files, and prepares for submission
git add <file(s)>/.
- git unstage files, release files, selective control
git reset HEAD <file(s)>/.
- How staging area files overwrite working directory files
git reset HEAD <file(s)>/. && git checkout -- <file(s)>/.
- Submit all files that have been managed by git and are modified in red
git add -u
- Reset all files in the workspace and staging area to their original state
git reset --hard
- Compare the difference between the current branch and a commit
git diff HEAD [commit hash fragment]
- delete a branch
git branch -D [branch name]
After synchronizing to remote, merge multiple commits into one
git rebase -i HEAD~2/hash pick && squash :wq!
- View the address of the origin representative of the project
git remote -v
- pick quits by mistake
git rebase --abort
- Not synced to remote, resubmit
reset soft
gitflow release releases a new version
git flow release start v0.5.0 npm version minor git flow release finish -n git push git checkout master git push
gitflow hotfix to fix a bug on master
git flow hotfix start foo npm version patch // 注意:一定要在修复bug代码之前新增版本号 git add . git commit -m "version change message" git flow hotfix finish -n git push git checkout master git push
Compare the logs of 2 branches
git log develop..master
Merge master code into feature
git checkout feature git merge master
git merge master feature
What to do when git rebase conflicts
resolve conficts git add . git rebase --continue
How to squash multiple commits into one?
Suppose merge feature to master.git checkout master git merge --squash feature git commit -m "这是一次squash commit" git push
View the parent branch of the current branch
git reflog show <childBranch> 32c3956 (HEAD -> currentBranch, origin/fatherBranch, fatherBranch, list) childBranch@{0}: branch: Created from fatherBranch
childBranch is your new branch.
fatherBranch is its parent branch, which is the source branch.Undo remote branch error commit
...reset git push --force
Actually use the commits from the local branch instead of the remote branch.
How to restore the local branch of the leading remote by mistake?
git reflog // 找出最新的commit sha1值,HEAD@{1}比HEAD@{2}新 git branch branchName <sha1>
Find a commit through git reflog, and then cherry-pick it.
Remove tags added by npm version patch/minor/major by mistake
git tag | grep v1.1.38 git tag -d v1.1.38 git push origin :refs/tags/v1.1.38
The difference between git fetch and git pull
git fetch 更新origin/*下的所有分支,在发布git flow feature前很有用,用于更新remote分支。 git pull 主要用来更新多人合作的当前分支,多用于更新local分支。
https://www.git-tower.com/learn/git/faq/difference-between-git-fetch-git-pull
The remote branch is deleted, and the local git fetch cannot be updated to the latest branch
git fetch --prune
--prune
Before fetching, remove any remote-tracking references that no longer exist on the remote.Check the status of all local branches in time
git remote show origin
What should I do if this branch no longer exists on the remote and git fetch --prune cannot update the status?
git push origin --delete feature/fix-chat-unread-msg-async
Delete a single detached remote branch
git remote prune <name>
- Cancel a merge (merge)
git merge --abort
- Roll back a commit
git revert Head/[commit hash]
Some practices for using Git gracefully
- gitbash under windows supports Chinese input:
1) Click the left mouse button on the git logo in the upper left corner
2) Find options and switch to the text directory, set the Character set to UTF-8 - generate ssh-key
ssh-keygen -t rsa -C "gaokai20100801@qq.com"
- windows view ssh-key
/c/Users/frank/.ssh/id_rsa.pub
- mac/linux view ssh-key
cd ~/.ssh
ls
cat id_rsa.pub
- git flow
https://danielkummer.github.io/git-flow-cheatsheet/index.html I deleted stash by mistake, what should I do?
git fsck --unreachable | grep commit | cut -d\ -f3 | xargs git log --merges --no-walk --grep=WIP
Find the corresponding commit hash value
git stash apply 1f55da93d26cd51f15f9e93351dae6b75e25e36f
.idea modification will always remind, .gitignore does not take effect
.idea/
git rm -r --cached .idea
- What is origin in Git?
origin is a variable representing a git repository address. You can usegit remote -v
view the address represented by origin. What do Git's system, global and local parameters stand for?
--system可以输出很多git的系统设置,其中最有用的是alias,例如git s代表了git status,系统所有登录用户有用。 --global输出了git的全局设置,主要包括全局的user.name和user.email,优先级低于单个仓库中设置的user.name和user.email,当前用户所有仓库有用。 --local输出了git的项目设置,主要包括remote.origin.url以及gitflow的很多配置,只对某个仓库有用。
Difference between Git workspace and staging area?
add commit 工作目录---->暂存区---->版本历史
Staging area: git has obtained management rights to the file, and the files in the staging area have status: new file, deleted, modified, etc.
Version history: git log to view each commit record.
Windfall:
If you want to control the commit record in a very fine-grained manner, you can use git add to specify the file, separate multiple commits, submit a fine-grained function change file set for each commit, and go through the process of the file directory staging area version history multiple times.
How does Git rename files?
git mv README.md readme.md
- What does Git's working tree and index mean?
Index refers to the git index, which can be understood as a copy of the file in git, and only deleting the index will delete only the files that exist in git.
The working tree refers to the working tree of the operating system, that is, the files stored on the disk of the operating system.
Here are two common examples: - Only delete files in git index, IDE hidden work tree files such as .idea cannot be deleted: --cached
git rm --cached -r .idea // **--cached only deletes index**, -r (recursive) recursively deletes all files in the .idea directory
- Delete the files on the index and working tree
git rm
delete the files on the index and working tree,
Just deleting the working tree without deleting the index does not exist.
- nothing to commit and working tree clean?
The staging area has nothing to commit to version history.
The work area is also clean. How to distinguish the working area and the staging area at a glance?
- Your branch is ahead of 'origin/master' by 1 commit Version History
- Changes to be committed staging area
- Untracked files Workspace
// 版本历史 Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) // 暂存区 Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: readme.md -> README.md new file: helloman // 工作区 Untracked files: (use "git add <file>..." to include in what will be committed) hi
- How to view logs more elegantly?
git log --oneline
Concise commit recordgit log -n2 --oneline
The last 2 concise commit recordsgit log --all
Historical version information of all branchesgit log --graph
Graphical view version evolution historygit log --oneline --all -n4 --graph
combination view log - How to quickly locate the command documentation of git?
git help --web log
browser to view the usage ofgit log
- What about the graphical interface that comes with git?
gitk
not need to install third-party plug-ins, it is available in pure command line without third-party software. - Is author and committer different in git?
The author is the generator of the code, for copyright protection. - The mysterious .git directory
HEAD
working branch refs/heads/fooconfig
repo configuration informationrefs
heads, branches; tags, tags or milestones
What is stored inrefs/heads/master
, the latest commit
What is stored inrefs/tags/js01
, the latest tag contains an objectobjects
folder, 2 characters and loose pack folder, store tree, there are blob files under tree
You can modify HEAD, config and other information directly through vim, which is the same as the command. How to tell the type of a git file?
git cat-file -t/-p [hash fragment]
// -t type, -p content
As long as the file content of any file is the same, in the eyes of git, it is the only one blob.commit tree // 位于objects目录下 blob // 位于objects目录的二级目录下,具体的文件
- The difference between tree, commit, blob?
commit: A commit will definitely correspond to a tree, which contains the information of a commit object such as the root tree, author, committer, parent, etc.
tree: Take out a commit and store a snapshot. This snapshot corresponds to the snapshots of all the folders and files of the current project. It is a state of the entire warehouse at a specific time. There can be blobs or trees in the tree. Because trees are folders; the root tree is the largest tree.
blob: It has nothing to do with whether the file name is the same or not, as long as the content is the same, it is the only blob.
What does a commit contain?
git cat-file -p [commit hash fragment]
Contains tree, parent, author and commiter.
tree f06f7f36af17cb9098031c66d22a7910c0fa1bac parent 92a55c8a5b1d38d224232ad84b9b728ae77189cb parent eda632a1f2a3ea049c5f5268f6b2f064b71898ce author FrankKai <gaokai20100801@gmail.com> 1548139120 +0800 committer FrankKai <gaokai20100801@gmail.com> 1548139120 +0800 Merge branch 'feature/chatBreakChange' into prerelease # Conflicts: # src/api/chat.js
What does a tree contain?
git cat-file -p [tree hash fragment]
Contains tree, blob.
- blob 015aaf344153ed7822069b2a98898b7d7a215b0d .babelrc
- blob 9d08a1a828a3bd2d60de3952744df29f9add27fa .editorconfig
- blob 080140b833db5b758b1eb869a269f4bbb068a19e .eslintignore
- blob 8f777c376c0314e480f9bbba273d4902810bcb11 .eslintrc.js
- blob 895e844218637929546ed2295ae90f991ceb5d38 .gitignore
- blob db7b635d23657349dbe4c33cc353ef4efd8ca960 .npmrc
- blob 797e871f4b8c0a3071e8b6ab2cc40b804cd2971c .postcssrc.js
- blob d3983c1d6a5525aae58b823448723434ca83ceed .prettierrc
- blob 93cc7473ab066204f3329221111a945e2dc83576 BUS.md
- blob defc3d9914d1af08e6670b96995261bfe1fb61a6 CHANGELOG.md
- blob bfd46fd4008cbe7103181fc5cd64392a74426e96 MQTT.md
- blob abdb55935d833dd4f4b79475aa7d63ffcb0cc9cd README.md
- tree f1f80f844bb80389826198a15ec0f224a53525f8 build
- blob 2aefa3130f4ff753b5c3e538db53b9b186f12540 index.html
- blob 967b8f243420a9a8a07b8f429f0a7ba874a834ad package-lock.json
- blob 35d9fa46f569395b25a87daef4820de42d071831 package.json
- tree f6bdc675a8f9af805867b5a19c263e5bbfe4c26c src
- tree 09e231414b91779326447a0c8d5b3421aa2308c2 static
- tree ad94369cfdd2038a552e44fc0abbd1738113b5e6 test
blob 0b96f21c27a3759cecde02fba1e050d86a8e9a54 yarn.lock
What does a blob contain?
git cat-file -p [tree hash fragment]
is a specific file.
- What is detached HEAD?
Detach the head pointer.git checkout [commit hash fragment]
, switch to the detached head pointer state, not associated with any branch or tag, git will consider it unimportant and clean it up as garbage.
Disadvantage: After switching branches, you need to create a new branch with git branch [branch name] [commit hash fragment]
, otherwise the original message will be lost.
Advantages: You can cut a branch based on a commit, and then create a new commit, which will quickly back to the desired version.
- What can HEAD point to?
It's at .git/HEAD.
It can point to a branch or commit, , but in fact, the branch still points to commit in the final analysis.
git log View the branch name pointed to by the HEAD pointer: (HEAD->foo, bar, master)
Can do a quick diff, git diff HEAD [commit hash fragment]
.
Father's father diff: git diff HEAD HEAD~2
, git diff HEAD HEAD^^
.
- How to modify the message of the latest commit?
git commit --amend
Note: Such changes cannot be made on the team's integration branch, only locally. How to modify the message of an old commit?
git rebase -i [父 commit hash fragment] reward 添加修改后的commit message
Note: Such changes cannot be made on the team's integration branch, only locally.
- What else can git stash pop stash@{n} do?
When the local code of the current branch is not submitted, the leading remote branch code is pulled, and the remote code will overwrite the local code.
Git is smarter, it will not completely throw away the local code, even if there is no manual generation of a commit record, it will automatically generate a record for us under stash, so as to avoid major code loss.
- How to standardize version release in gitflow mode?
Version number upgrade | gitflow corresponding |
---|---|
bug -> patch | hotfix |
feature->minor | release |
System Refactoring->major | release |
However, in the case of scrum, the iteration is very fast. If all features are upgraded to the minor, the minor number will be very large. How to deal with this situation?
When only minor is upgraded, in the commit information, add the following information:
Types of | Submit Information |
---|---|
bug patch | [bug patch] |
feature patch | [feature patch |
Create a new project and upload to git
git init git ac git remote add origin remote repository URL
What is the git parameter --decorate?
- There are short, full, auto, no values, --decorate=short
- Print out the ref name of the commit.
- When short, the ref name prefix refs/head, refs/tags/ and refs/remotes will not be printed
- When full, the full prefix will be printed
- When auto, if the output is a terminal, it will be printed as short; non-terminal will display all
- When no, refs information such as HEAD and tag will be hidden
- The default value is short
- What is cherry pick?
How does understand git cherry pick? Clear all local git caches
git rm -r --cache .
- error Command "husky-run" not found
rm -rf .git/hooks/ A complete rebase process
- git checkout feature
- git rebase master
- resolve conflicts
- git add .
git rebase --continue
- The difference between git revert and git reset
The way git revert generates a new commit rolls back to the previous or specified commit version of the code, the principle is that Head continues to move forward. When the commit branch code that has been rolled back is merged, the code is merged normally
git reset deletes the code after a commit, the principle is that the Head goes backwards. When there is a commit branch code that has been rolled back, the reset code will still be merged
oh-my-zsh common commands
Abbreviation and full writing comparison table
abbreviation | write all |
---|---|
gst | git status |
gaa | git add . |
gcmsg "" | git commit -m "" |
gp | git push |
glog | git log --oneline --decorate --graph |
gl | git pull |
gf | git fetch |
gfa | git fetch --all --prune |
Use little tricks
- How to modify the parameters of the default command. For example, the decorate of glog is short by default. I want to specify the decorate of glog to be no. What should I do?
glog --decorate=no
Looking forward to communicating with you and making progress together:
- WeChat public account: Dada big front end / excellent_developers
- Front-end Q&A mutual aid planet: t.zsxq.com/yBA2Biq
Strive to be an excellent front-end engineer!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。