I used SVN for a very short time when I just graduated, and since then I have been using Git to do the version control of the code. In the past four years, I have made some records and experiences in the process of using Git. I would like to share with you here, and you may learn some useful things from it.

Whether it’s Github, GitLab, or any other code hosting platform, git is used for code management, and git is a must have skill for a programmer, which is very helpful for the job and the interview.

  • Git Common Commands
  • Some practices for using Git gracefully
  • Oh-my-zsh Common command

Git Common Commands

  • Git clone remote repository

Git Clone -b Branch name Remote address

Git will clone only the default branch if you do not specify a branch.

  • Check your Git username and email address
git config user.name
git config user.email
Copy the code
  • Branch related
Git branch name (switch to the corresponding branch) is automatically updated to the branch code git branch name (create a branch) git branch -d Git branch -d branch name (force to delete an unmerged branch) git checkout -b branch name [branch name based on or commit value] (switch branch and switch to it directly)Copy the code
  • Checking Git History

history

  • Search git history by keyword

history | grep push

  • Viewing the COMMIT History
git log
git log --summary
Copy the code
  • Setting your Git Account
git config --global user.name "foo"
git config --global user.email [email protected]"
git config user.name "foo"
git config user.email "[email protected]"
Copy the code
  • Check your Git account
git config --global --list
git config --local --list
Copy the code
  • You can view only the configuration of one item

git config --local user.name

  • Roll back the modification
git reset HEAD foo.js
git checkout -- foo.js
Copy the code
  • View the modified code
git diff
git diff HEAD
git diff --staged
Copy the code
  • After submitting, several files were found missing and not submitted
Add git add "*.html" and resubmit. Finally, only one git commit --amendCopy the code
  • Cache files with certain suffixes

git add "*.js"

  • Clear files in the cache

git reset octofamily/octodog.txt

  • Delete files with certain suffixes completely

git rm "*.txt"

  • Merge branches to master

Git merge branch name

  • Add. Uncommit some files before

git checkout -- <filename>

  • Hide code in a dirty directory (for other members who modify the same branch code but don’t want to commit)

git stash

  • Release dirty directory code

git stash pop

  • Releases the specified dirty directory code

git stash pop stash@{0}

  • Delete remote branch (this branch must not be the default branch)

git push origin --delete branchname

  • The rollback to the earlier version is forced because the version is committed
Git reset --hard hash valueCopy the code
  • Querying the stash directory

git stash list

  • Delete a stash

git stash drop stash@{0}

  • Set the remote warehouse address

git remote set-url origin [email protected]:baz/helloworld.git

  • We’ve created a new branch locally, but orgin doesn’t have a push code

git push --set-upstream origin preproduction

  • Specify tag to remote

git push origin <tag_name>

  • Call all tags to remote

git push --tags

  • View current tags

git tag --list

  • Delete the index file without deleting the. Idea file on the working tree

Git rm –cached -r. idea // –cached only removes 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 do staging files overwrite working directory files

git reset HEAD <file(s)>/. && git checkout -- <file(s)>/.

  • Commit all files that have been managed by Git and modified in red

git add -u

  • Resets all files in the workspace and staging area to their original state

git reset --hard

  • Compare the current branch to a commit

git diff HEAD [commit hash fragment]

  • Deleting a branch

git branch -D [branch name]

  • After synchronization to remote, merge multiple COMMITS into one
git rebase -i HEAD~2/hash
pick && squash
:wq!
Copy the code
  • View the address of the origin representative for the project

git remote -v

  • Pick up by mistake

git rebase --abort

  • If the data is not synchronized to remote, submit it again

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 pushCopy the code
  • Gitflow HotFix fixes a bug on the master
Git flow hotfix start foo NPM version patch Git add. Git commit -m "version change message" Git flow hotfix finish -n git push Git checkout master git pushCopy the code
  • Compare the logs of the two branches
git log develop.. masterCopy the code
  • Merge master code into feature
git checkout feature
git merge master
Copy the code
git merge master feature
Copy the code
  • What to do when Git Rebase conflicts
resolve conficts
git add .
git rebase --continue
Copy the code
  • How to strike multiple commits?

Suppose merge feature to master.

Git checkout master git merge --squash feature git commit -mCopy the code
  • 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
Copy the code

ChildBranch is your new branch. The fatherBranch is its parent branch, the source branch.

  • Undo remote branch error commit
. reset git push --forceCopy the code

Use the local branch’s commit instead of the remote branch.

  • How to restore the local branch that mistakenly deleted the leading remote?
Git branch branchName <sha1>Copy the code

Git reflog to find a commit and cherry-pick can also be used.

  • The tag added by NPM version patch/ Minor/Major was deleted
Git tag | grep v1.1.38 git tag - d v1.1.38 git push origin: refs/tags/v1.1.38Copy the code
  • The difference between git fetch and Git pull
Git Fetch updates all branches under Origin /*. This is useful for updating the Remote branch before publishing the Git Flow feature. Git pull is used to update the current branch of a multi-party collaboration. It is mostly used to update local branches. // https://www.git-tower.com/learn/git/faq/difference-between-git-fetch-git-pullCopy the code
  • The local git fetch cannot be updated to the latest branch when the remote branch is deleted
Git fetch --prune --prune Before fetching, remove any remote-tracking references that no longer exist on the remote.Copy the code
  • Check the status of all local branches in a timely manner
git remote show origin
Copy the code
  • What if the remote branch no longer exists and git fetch –prune cannot update the status?
git push origin --delete feature/fix-chat-unread-msg-async
Copy the code
  • Delete a single detached remote branch
git remote prune <name>
Copy the code
  • Cancel a merge

git merge --abort

  • Roll back the commit

git revert Head/[commit hash]

Some practices for using Git gracefully

  • Windows Gitbash supports Chinese input:
1) Click the Git logo in the upper left corner with the left mouse button. 2) Find Options and switch to the Text directory and set the Character set to UTF-8Copy the code
  • Generate the SSH key

ssh-keygen -t rsa -C "[email protected]"

  • Windows to see the SSH key

/c/Users/frank/.ssh/id_rsa.pub

  • MAC/Linux view the SSH key

cd ~/.ssh ls cat id_rsa.pub

  • git flow

Danielkummer. Making. IO/git – flow – ch…

  • What should I do if I mistakenly delete Stash?
git fsck --unreachable |
grep commit | cut -d\  -f3 |
xargs git log --merges --no-walk --grep=WIP
Copy the code

Find the corresponding commit hash value

git stash apply 1f55da93d26cd51f15f9e93351dae6b75e25e36f
Copy the code
  • .idea changes are always notified,.gitignore does not take effect

.idea/

git rm -r --cached .idea
Copy the code
  • What is Origin in Git?

Origin is a variable that represents a Git repository address. You can use git remote-v to see where origin stands for.

  • What are the Git system, global, and local parameters?
The most useful is the alias. For example, git s stands for Git status, which is useful for all logins in the system. Name and user.email have a lower priority than those set in a single repository. This is useful for all repositories of the current user. --local output git project Settings, including remote. Origin. Url and gitflow.Copy the code
  • What’s the difference between a Git workspace and a staging area?
Add COMMIT Working directory ----> staging area ----> Version HistoryCopy the code
Staging area: Git has been granted permission to manage files. Staging area files have statuses: new file, deleted, modified, etc. Version history: Git log to view every commit record. Bonus: If you want very fine-grained control over the commit record, you can use Git Add to specify files, commit multiple times, commit a fine-grained set of changes to each commit, and walk through the version history of the file directory multiple times.Copy the code
  • How does Git rename files?
git mv README.md readme.md
Copy the code
  • Git Working tree and Index
If you delete the index, you will only delete the files that exist in Git. Working tree refers to the operating system's working tree, that is, the files stored on the operating system's disk. 1. Delete only files in git index, and you can't delete files in.idea. **--cached** git rm --cached -r. idea // **--cached only deletes index**, -r (recursive) deletes all files in idea directory 2. Git RM does not delete the files on the index and working tree and only delete the working tree without deleting the index.Copy the code
  • Nothing to Commit and working tree Clean?
The staging area has nothing to commit to the version history. The workspace is also clean.Copy the code
  • How to distinguish work area and staging area at a glance?
    • Your branch is ahead of ‘origin/master’ by 1 commit
    • Changes to be committed staging area
    • Untracked Files Workspace
// Version history 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>..." Renamed: readme.md -> readme.md new file: helloman // Work area Untracked files: (use "git add <file>..." to include in what will be committed) hiCopy the code
  • How to view logs more elegantly?
Git log - oneline concise git commit record log - n2 -- oneline recently two simple git commit record log - all the history of all branches version information git log - graph Graphical view of version evolution history Git log --oneline -- all-n4 --graph Combination view logsCopy the code
  • How to quickly locate git command files?

Git help — Web log browser to see how to use git log

  • What about git’s own graphical interface?

Gitk does not need to install third-party plug-ins. It is available only on the COMMAND-LINE interface (CLI) and no third-party software is available.

  • Are authors and committers different in Git?

The author is the code generator for copyright protection.

  • The mysterious.git directory
HEAD Work branch refs/heads/foo Config Repo configuration information refs heads, branch; What tags, tags, or milestone refs/heads/master stores, what the latest commit refs/tags/ JS01 stores, the latest tag, contains an Object Objects folder, 2 characters and loose pack folders, The bloB file is stored in the treeCopy the code

You can use vim to modify the HEAD, config, and other information.

  • How to determine the type of Git file?

Git cat-file -t/-p [hash fragment] // -p [hash fragment] // git cat-file -t/-p [hash fragment]

Commit tree // in the objects directory blob // in the objects directory level 2, specific fileCopy the code
  • What’s the difference between tree, commit and blob?
Commit: A commit must correspond to a tree, containing the root tree, author, committer, parent, and so on of a commit object. Tree: Takes a commit and stores a snapshot. This snapshot is a snapshot of all the folders and files in the current project. It is a snapshot of the state of the entire repository at a specific time. You can have blobs in a tree, you can have trees, because trees are folders; The root tree is the largest tree. Blob: The bloB is the only bloB as long as the contents are the same regardless of whether the file name is the same.Copy the code
  • What does a COMMIT contain?
git cat-file -p [commit hash fragment]
Copy the code

Contains tree, parent, author, and commiter.

tree f06f7f36af17cb9098031c66d22a7910c0fa1bac
parent 92a55c8a5b1d38d224232ad84b9b728ae77189cb
parent eda632a1f2a3ea049c5f5268f6b2f064b71898ce
author FrankKai <[email protected]> 1548139120 +0800
committer FrankKai <[email protected]> 1548139120 +0800

Merge branch 'feature/chatBreakChange' into prerelease

# Conflicts:
#       src/api/chat.js
Copy the code
  • What does a tree contain?
git cat-file -p [tree hash fragment]
Copy the code

Including tree, blob.

100644 blob 015aaf344153ed7822069b2a98898b7d7a215b0d .babelrc 100644 blob 9d08a1a828a3bd2d60de3952744df29f9add27fa .editorconfig 100644 blob 080140b833db5b758b1eb869a269f4bbb068a19e .eslintignore 100644 blob 8f777c376c0314e480f9bbba273d4902810bcb11 .eslintrc.js 100644 blob 895e844218637929546ed2295ae90f991ceb5d38 .gitignore 100644 blob db7b635d23657349dbe4c33cc353ef4efd8ca960 .npmrc 100644 blob 797e871f4b8c0a3071e8b6ab2cc40b804cd2971c .postcssrc.js 100644 blob d3983c1d6a5525aae58b823448723434ca83ceed .prettierrc 100644 blob 93cc7473ab066204f3329221111a945e2dc83576 BUS.md 100644 blob defc3d9914d1af08e6670b96995261bfe1fb61a6 CHANGELOG.md 100644  blob bfd46fd4008cbe7103181fc5cd64392a74426e96 MQTT.md 100644 blob abdb55935d833dd4f4b79475aa7d63ffcb0cc9cd README.md 040000 tree f1f80f844bb80389826198a15ec0f224a53525f8 build 100644 blob 2aefa3130f4ff753b5c3e538db53b9b186f12540 index.html 100644 blob 967b8f243420a9a8a07b8f429f0a7ba874a834ad package-lock.json 100644 blob 35d9fa46f569395b25a87daef4820de42d071831 package.json 040000 tree f6bdc675a8f9af805867b5a19c263e5bbfe4c26c src 040000 tree 09e231414b91779326447a0c8d5b3421aa2308c2 static 040000 tree ad94369cfdd2038a552e44fc0abbd1738113b5e6 test 100644 blob 0b96f21c27a3759cecde02fba1e050d86a8e9a54 yarn.lockCopy the code
  • What does a BLOB contain?
Git cat-file -p [tree hash fragment]Copy the code
  • Detached HEAD: What is detached HEAD?
Separate the head pointer. 'Git checkout [commit hash fragment]', switch to the separate head pointer state, not associated with any branch or tag, git will consider this is not important, as garbage. Disadvantages: After branch switch, you need to use 'git branch [branch name] [Commit hash fragment]' to create a branch. Otherwise, the original message will be lost. Advantages: You can switch branches based on a commit and create a new commit, quickly falling back to the desired version.Copy the code
  • What can HEAD point to?
It's located in.git/HEAD. It could be a branch or it could be a COMMIT, ** but the branch is still a commit**. Git log: '(HEAD->foo, bar, master)' can fast diff, 'git diff HEAD [commit hash fragment]'. Diff: 'git diff HEAD HEAD~2', 'git diff HEAD HEAD^^'.Copy the code
  • How do I modify the latest COMMIT message?

Git commit –amend Note: this change cannot be made on the integration branch of the team, only locally.

  • How do I modify an old commit message?
Git rebase -i [Parent Commit hash fragment] reward Add a modified commit message note: This change cannot be made on the integration branch of the team.Copy the code
  • Git Stash pop Stash @{n} What else can I do?
Pull the leading remote branch code when the local code for the current branch is not committed. The remote code overwrites the local code. Git is smart. It will not throw away the local code completely. Even if it does not manually make a commit record, it will automatically make a stash record for us to avoid significant code loss.Copy the code
  • How to regulate version release in GitFlow mode?
Version Upgrade Gitflow corresponding
bug -> patch hotfix
feature->minor release
System reconfiguration -> Major release

However, in the case of Scrum, the iteration is very fast, and if all features upgrade minor, the number of minor will be large. How to deal with this situation?

If only minor is upgraded, add the following information to the commit information:

type Submit information
bug patch [bug patch]
feature patch [feature patch
  • Create a new project and upload it to Git
git init
git ac
git remote add origin remote repository URL
Copy the code
  • Git parameter — class is what?

    • There are short, full, auto, and no values, — class =short
    • Print the ref name for COMMIT.
    • When short, the refs/head,refs/tags/ and refs/remotes prefixes refs/ name are not printed
    • When full, the full prefix is printed
    • When auto, if the output is a terminal, it will be printed as short. Non-terminals will display all
    • When no, refS information such as HEAD and tag is hidden
    • The default is short
  • What’s cherry pick?

How to understand Git Cherry pick?

  • Clear all local Git caches
git rm -r --cache .
Copy the code
  • error Command “husky-run” not found

rm -rf .git/hooks/

  • A complete rebase process
1. git checkout feature
2. git rebase master
3. resolve conflicts
4. git add .
5. git rebase --continue
Copy the code
  • The difference between Git Revert and Git Reset
Git Revert creates a new commit that rolls back to the previous commit or the commit version specified by git Revert. Git reset Deletes code after a commit, and the Head moves backwards. When a rollback of the commit branch is merged, the reset branch is still mergedCopy the code

Oh-my-zsh Common command

Abbreviations are written in the cross reference table

abbreviations All written
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 a trick

  • For example, glog classpiles default to short, and I want to set the class to NO. What do I do?

glog --decorate=no

Looking forward to communicating with you and making progress together:

  • Wechat official account: Greatly large front/excellent_developers
  • Github: A personal blog while you’re still young
  • SegmentFault: Be a good front end engineer while you’re still young

Strive to become an excellent front-end engineer!