Git cheat guide for the front end

Regardless of github, GitLab, or other code hosting platforms, code management is done with Git. Git can be said to be a programmer’s essential skill, which is very helpful for work and interview.

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

Git Git

  • Git clone remote branch repository:

Git clone-b Branch name Remote address

If you do not specify a branch when Git clones a remote repository project, only the contents of the default branch are cloned.

  • Check git username and mailbox

    git config user.name git config user.email

  • Branch related

    Git branch -d git branch -d git branch -d git branch -d Git checkout -b branch name [based on the branch name or commit value] (switch branches and switch directly over)

  • Git History

history

  • Search Git history by keyword

history | grep push

  • Viewing Commit History

    git log git log –summary

  • Setting a 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]

  • Checking git Accounts

    git config –global –list git config –local –list

  • View the configuration of only one item

git config --local user.name

  • Roll back the modification

    git reset HEAD foo.js git checkout — foo.js

  • View the modified code

    git diff git diff HEAD git diff –staged

  • After submission, I found that several files were lost and not submitted

    “*.html” to commit again. Finally, only one commit git commit –amend

  • Caches files with a suffix

git add "*.js"

  • Clear files in the cache

git reset octofamily/octodog.txt

  • Completely delete a file with a suffix

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 (if other members have modified the same branch code but do not want to commit it)

git stash

  • Release dirty directory code

git stash pop

  • Releases 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

  • The system has committed, forcing a rollback to an earlier version

    Git reset –hard hash

  • Look at the Stash directory

git stash list

  • Delete one stash

git stash drop stash@{0}

  • Set the remote warehouse address

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

  • A new branch is created locally, but orgin does not, before the 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

  • Index does not delete. Idea files on working Tree

Git rm — cached-r. idea // –cached only deletes index, -r (recursive) deletes all files in the.idea directory

  • Git active track files, control files, ready to submit

git add <file(s)>/.

  • Git unstage files, free 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 managed by Git that have modified in red

git add -u

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

git reset --hard

  • Compares the difference between the current branch and a commit

git diff HEAD [commit hash fragment]

  • Delete a branch

git branch -D [branch name]

  • After remote synchronization, merge multiple commits into one

    git rebase -i HEAD~2/hash pick && squash :wq!

  • View the address where origin represents the project

git remote -v

  • The pick terminates by mistake

git rebase --abort

  • Not synchronized to remote, resubmit

reset soft

  • Gitflow release new release

    Git flow release start v0.5.0 NPM version Minor Git flow release finish -n git push git checkout master Git push

  • 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 push

  • Compare logs for the two branches

    git log develop.. master

  • Merge master code into feature

    git checkout feature git merge master

    git merge master feature

  • What if git rebase conflicts

    resolve conficts git add . git rebase –continue

  • Squash: How should multiple commits be

Assume merge feature to master.

Git checkout master git merge --squash feature git commit -m "Squash commit" git pushCopy the code
  • View the parent branch of the current branch

    git reflog show 32c3956 (HEAD -> currentBranch, origin/fatherBranch, fatherBranch, list) childBranch@{0}: branch: Created from fatherBranch

ChildBranch is your new branch. FatherBranch is its parent branch, or source branch.

  • Undo remote branch error commit

    . reset git push –force

Instead of a remote branch, commit from the local branch is used.

  • How to recover a local branch that mistakenly deleted leading remote?

    Git branch branchName git branch branchName git branch branchName git branch branchName

Git reflog to find a commit and then cherry-pick.

  • Delete the tag mistakenly added by NPM Version Patch /minor/ Major

    Git tag | grep v1.1.38 git tag – d v1.1.38 git push origin: refs/tags/v1.1.38

  • Git fetch differs from Git pull

    Git fetch updates all branches under Origin /*. This is useful before releasing the Git flow feature and is used to update the remote branch. Git pull is mainly used to update the current branch of a multi-party collaboration, mostly the local branch. / / www.git-tower.com/learn/git/f…

  • The local Git fetch cannot be updated to the latest branch

    git fetch –prune

    — What does prune mean? Before fetching, remove any remote-tracking references that no longer exist on the remote.

  • Check the status of all local branches

    git remote show origin

  • What if the remote branch no longer exists and git fetch — Prune does not update its state?

    git push origin –delete feature/fix-chat-unread-msg-async

  • Delete a single detached remote branch

    git remote prune

  • Cancel a merge

git merge --abort

  • Rollback a COMMIT

git revert Head/[commit hash]

Some practices for using Git gracefully

  • Windows gitbash supports Chinese input:

    2) Find options and switch to the Text directory. Set the Character set to UTF-8 and copy 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…

  • Stash deleted 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
Copy the code
  • .idea changes are always reminded that.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 check the origin location.

  • What are the system, global, and local parameters in Git?

    Git s specifies the git status, which is used by all the users in the system. –global outputs git global Settings, including global user.name and user.email, which are lower in priority than those set in a single repository. — Local outputs git project Settings, including remote. Origin. Url and a lot of gitflow configuration, which is only useful for a repository.

  • What is the difference between Git workspace and staging?

       add           commit
    Copy the code

    Working directory —-> Staging area —-> Version history

    Git has been granted permission to manage files in the staging area, and the files in the staging area have states: new file, deleted, Modified, etc. Version history: Git log Displays each commit record. Bonus: If you want very fine-grained control over your commit record, you can use Git add to specify your files and commit them multiple times, each time committing a fine-grained set of functional change files, multiple times through the file directory staging version history process.

  • How does Git rename files?

    git mv README.md readme.md

  • Git working tree index

    Git index is a copy of a file in Git. If you delete index, you will delete only files in Git. Working tree refers to the working tree of the operating system. It refers to the files stored on the operating system disk. Here are two common examples:

    1. Delete only files in git index. IDE hidden working tree files such as.idea cannot be deleted: –cached

    Git rm — cached-r. idea // –cached only deletes index files, leaving directory -r (recursive). Delete files from index and Working Tree

    Git rm deletes files in index and working tree, but does not delete files in working tree.

  • Nothing to commit and working tree clean?

    There is no content to commit to version history in the staging area. The workspace is also clean.

  • How to distinguish work area and temporary storage 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 …” to unstage)

        renamed:    readme.md -> README.md
        new file:   helloman
    Copy the code

    // Workspace Untracked Files: (use “git add… to include in what will be committed)

        hi
    Copy the code
  • How to view logs more gracefully?

    Git log –oneline simple commit Git log -n2 — Oneline two simple commit git log –all historical versions of all branches git log –graph Git log –oneline — all-n4 –graph

  • How to quickly locate git command files?

Git help — Web log Browser to check git log usage

  • What about git’s built-in graphical interface?

There is no need to install third-party plug-ins for gitk. Gitk can be used only on the command line without third-party software.

  • Are authors and committers different in Git?

The author is the creator of the code for copyright protection.

  • The mysterious.git directory

    HEAD work branch refs/heads/foo config repo config info refs heads, branch; Commit Refs/Tags/JS01, the latest tag, contains an Object Objects folder, a 2 character and loose pack folder, A bloB file is stored in a tree

You can directly modify the HEAD, config and other information through vim, and the function of the command is the same.

  • How to determine the type of Git file?

Git cat-file -t/-p [hash fragment] // -t, -p contents If any file has the same contents, it is a unique bloB in Git eyes.

Commit tree // In the objects directory blob // in the objects directory secondary directory, the specific fileCopy the code
  • Tree, commit, blob?

    Commit: A commit must correspond to a tree, including information about the root tree, author, committer, parent, etc. Tree: Retrieves a commit, which stores a snapshot of all folders and files of the current project. This snapshot is a snapshot of the status of the entire repository at a specific time. You can have bloBs in a tree, or you can have trees, because trees are folders; The root tree is the largest tree. Blob: It does not matter whether the file name is the same or not, as long as the content is the same, it is a unique bloB.

  • What does a COMMIT consist of?

    git cat-file -p [commit hash fragment]

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]

Contains tree and 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] is a specific file.

  • Detached HEAD What is a detached HEAD?

    Separate header pointer. Git checkout [commit hash Fragment] : a branch or tag is not associated with git checkout. Disadvantages: After switching branches, run git branch [branch name] [commit Hash Fragment] to create a branch. Otherwise, the original message will be lost. Advantages: Branches can be cut out based on a commit, then a new COMMIT can be created, and quickly back to the desired version.

  • What can HEAD point to?

    It is located at.git/HEAD. You can point to a branch or commit, but the branch really points to a COMMIT. Git diff HEAD [commit Hash fragment] git diff HEAD [commit hash fragment] Git diff HEAD HEAD~2, git diff HEAD HEAD^^

  • How do I modify the message of the last commit?

Git commit — Amend Note: You cannot make such changes on the integration branch of the team, only locally.

  • How do I modify old COMMIT messages?

    Git rebase -i [parent commit Hash Fragment] reward Add the modified commit message note: this change cannot be made on the integration branch of the team.

  • Git stash pop stash@{n} what else can you do?

    Pull the leading remote branch code without committing the local code of the current branch, in which case the remote code overrides the local code. Git is clever because it doesn’t throw away the local code completely. Even if there is no manual commit record, it will automatically generate a record under Stash so as not to cause significant code loss.

  • How to standardize release in GitFlow mode?

Version Upgrade

Gitflow corresponding

bug -> patch

hotfix

feature->minor

release

System Refactoring -> Major

release

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

To upgrade only minor, 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

  • What is the git parameter –decorate?

    • Short, full, auto, no: — class =short
    • Print the ref name for commit.
    • For short, the ref name prefix refs/head,refs/tags/ and refs/remotes are not printed
    • When full, the full prefix is printed
    • Auto prints short if the output is a terminal. Non-terminal will display all
    • When no, HEAD, tag, and other refs information are hidden
    • The default value is short
  • What is cherry pick?

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

    1. git checkout feature
    2. git rebase master
    3. resolve conflicts
    4. git add .
    5. git rebase –continue
  • Git revert differs from Git reset

    Git Revert rolls back to the previous or commit version of the code by creating a new COMMIT, with the Head moving forward. If there are commit branches that have been rolled back, the code will be merged normally

    Git reset deletes the code after a commit by moving the Head back. If there are commit branches that have been rolled back, the reset branch code will still be merged

Oh-my-zsh Common command

Write all abbreviations in the comparison 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