preface

Recently, I encountered some problems with Git in my spare time at netease, so I took this opportunity to make up the basic knowledge of Git.

Public number front-end UpUp, reply Git, you can get the brain map.

Contact πŸ‘‰TianTianUp, surprise oh ❀️

Brain figure πŸ‘‡

This article is more comb is Git common command, not principle article, need this part of knowledge can look down.


First of all, let’s understand the usual Git operation process. There is a good picture on the Internet πŸ‘‡


The basic concept

Based on the above picture, we have the following concepts πŸ‘‡

  • Repository πŸ‘‰. Git

    • When you use Git to manage files, for examplegit initAt this point, there will be one more.gitFile, we call this file the repository.
    • The git fileAnother effect is that when it is created, it automatically creates the master branch and points the HEAD pointer to the master branch.
  • The workspace

    • Local project location where files are stored
    • You can think of it as the workspace on the graph
  • The staging area (Index/Stage)

    • As the name implies, it is a place to temporarily store files, which are added to the buffer by using the add command
  • Local Repository

    • Normally, we use the commit command to add files from the staging area to the local repository
    • In general, the HEAD pointer points to the master branch
  • Remote Warehouse

    • For example, when we use GitHub to host our projects, it is a remote repository.
    • Usually we use the Clone command to copy the code from the remote repository. When the local code is updated, we push it to the remote repository.

Git file status

  • Usually we need to check the status of a file
git status
Copy the code
  • Changes not staged for commit
    • So it basically says that the workspace has it, but the cache doesn’t, so we need togit add
  • Changes to be committed
    • In general, at this point, the file is in the cache, and we need togit commit
  • nothing to commit, working tree clean
    • At this point, we can push the local code to the remote end

Common command

Git Configuration Commands

  • Listing the current configuration
git config --list	
Copy the code
  • Listing Repository configurations
git config --local --list
Copy the code
  • Listing global configurations
git config --global --list
Copy the code
  • Listing system configurations
git config --system --list
Copy the code

If you do not have user information configured, configure πŸ‘‡

  • Configuring a User Name
git config --global user.name "your name"
Copy the code
  • Configuring user Mailboxes
git config --global user.email "[email protected]"
Copy the code

Branch management

  • Viewing a Local Branch
git branch
Copy the code
  • Viewing remote branches
git branch -r
Copy the code
  • View local and remote branches
git branch -a
Copy the code
  • Switch to another branch from the current branch
Git checkout <branch name> // git checkout feature/tiantianCopy the code
  • Create and switch to the new branch
Git checkout -b <branch-name> // example πŸ‘‡ git checkout -b feature/tiantianCopy the code
  • Delete the branch
Git branch -d feature/tiantianCopy the code
  • The current branch merges with the specified branch
Git merge <branch-name> for example πŸ‘‡ git merge feature/tiantianCopy the code
  • See which branches have been merged into the current branch
git branch --merged
Copy the code
  • See which branches are not merged into the current branch
git branch --no-merged
Copy the code
  • View information about the last committed object for each branch
git branch -v
Copy the code
  • Deleting a remote branch
git push origin -d <branch-name>
Copy the code
  • Renaming a branch
git branch -m <oldbranch-name> <newbranch-name>
Copy the code
  • Pull remote branch and create local branch
Git checkout -b branch name x origin/ branch name x Git fetch origin <branch-name>:<local-branch-nameCopy the code

The fetch instruction

My understanding is to update the content of the remote warehouse to the local, recently with the elder sister development project process, the use of this command.

It is this kind of πŸ‘‡

Fetch is recommended

git fetch origin <branch-name>:<local-branch-name>
Copy the code
  • In general, this origin is the name of the remote host, which defaults to Origin.
  • branch-nameThe branch you want to pull
  • local-branch-nameUsually, you create a new branch locally and download the code from origin to the local branch.

Take πŸ‘‡ for example

Git fetch origin feature/template_excellent: feature/template_layout / / your working directory, Feature /template_layout // If you want to create a new requirement on the branch, you need to upload your branchCopy the code

Fetch other notation

  • Retrieve all updates from a remote host.
Git fetch < remote host name >Copy the code
  • In this case, all branch updates are fetched. If you want to fetch a particular branch, you can specify the branch name πŸ‘‡
Git fetch < host name > < branch name >Copy the code
  • If you want to get the contents of a branch back to a local branch, go to πŸ‘‡
Git fetch origin master:<local-branch-name>Copy the code

Fancy to cancel

  • Undo workspace changes

    • git checkout —
  • Staging file undo (do not overwrite the workspace)

    • git reset HEAD
  • Version back

    • git reset –(soft | mixed | hard ) < HEAD ~(num) > |

    • instruction scope
      –hard Roll back all the files, including the HEAD, index, and working tree
      –mixed The rollback part, including HEAD, index
      –soft Only the back of the HEAD

State of the query

  • Check the status
    • git status
  • View historical operation records
    • git reflog
  • See the log
    • git log

The document query

  • Show an outline of Git commands
    • git help (–help)
  • Displays a complete list of Git command Outlines
    • git help -a
  • The command description manual is displayed
    • git help

File store

  • Add changes to stash

    • Git Stash Save – A “message”
  • Remove the temporary

    • git stash drop <stash@{ID}>
  • Viewing the Stash List

    • git stash list
  • Delete all caches

    • git stash clear
  • Restore the changes

    • git stash pop <stash@{ID}>

diff

  • Compare the workspace to the cache
    • git diff
  • Compare the cache with the most recent commit of the local library
    • git diff — cached
  • Compare the workspace with the most recent local commit
    • git diff HEAD
  • Compare the differences between two Commits
    • git diff

Branch name

The master branch

  1. Primary branch: The branch for deploying the production environment to ensure stability.
  2. The Master branch is usually merged with the Develop and Hotfix branches, and in no case can you modify the code directly.

Develop branch

  1. Develop is a development branch, and usually saves the latest completed and bug-fixed code.
  2. When new functionality is developed, the Feature branch is created based on the Develop branch.

Feature branch

  1. To develop new features, create a feature branch based on develop.
  2. Branch naming: feature branches start with feature/. The naming rules are feature/user_module, feature/cart_module.

** I have a deep understanding of this. When I was in netease, mentor taught me this. ** usually set up a feature branch.

The release branch

  1. Release is the pre-online branch. During the release test phase, the release branch code is used as the baseline test.

Hotfix branch

  1. Branch naming: Hotfix/is a fix branch, which is named in a similar way to the feature branch.
  2. When an emergency occurs online and needs to be fixed, create a hotFix branch using the master branch as a baseline. When the fix is complete, merge the hotFix into the Master branch and the Develop branch.

Reference: Uncle Straw


Basic operation

With the above basic understanding, let’s take a look at the overall process.

  • Create the local repository Git init

    git init

  • Link local and remote repositories

    git remote add origin

    By default, origin is the alias of the remote repository. The URL can be created using HTTPS or SSH

  • Checking configuration Information

    • git config –list
  • Git user name and email address

    git config –global user.name “yourname”

    git config –global user.email “your_email”

  • Generating an SSH Key

    Ssh-keygen -t rsa-c “Change your email here”

    CD ~/. SSH there is a file named id_rsa.pub, copy the contents of the git library to my SSHKEYs

  • Often see remote warehouse information

    • git remote -v
  • The remote repository is renamed

    • git remote rename old new
  • Commit to the cache

    • Git add. upload all to the cache
    • Git add specifies the file
  • Commit to your local repository

    • git commit -m ‘some message’
  • Submitting to a remote repository

    • Git push < remote host name > < local branch name >
  • See the branch

    • git branch
  • Creating a new branch

    • git branch
  • Switch branch

    • git checkout
  • Create a branch and switch

    • git checkout -b
  • Delete the branch

    • git branch -d
  • Deleting a remote branch

    • git push -d
  • Switch branch

    • git checkout

Ignore the file.gitignore

The purpose of this file is to ignore things that don’t need to be managed by Git, and we don’t want them on the list of untracked files.

So let’s look at how to configure the file information.

Node_modules # Ignores all files ending in.vscode. Vscode # ignores all files ending in.md *. Md # except readme.md ! Readme.md # ignores doc/something.txt but not doc/images/arch.txt doc/*.txt # Ignores all files in the doc/ directory with the TXT extension doc/**/*.txtCopy the code

reference

  • Git Basic operation, one article is enough!
  • Summary of Git common operations
  • Git Branch development specifications you must know
  • How to use Git gracefully
  • Gracefully Commit your Git Commit Message
  • The difference between git pull and git fetch
  • Ten minutes to arm your code base
  • “Memo” 60+Git common command line

❀️ Thank you all

If you found this helpful:

  1. Like support, let more people can also see this content.
  2. Pay attention to the public number front-end UpUp, regularly push good posts for you.
  3. If you like it, you can also read TianTian’s recent article (thanks to the encouragement and support from friends of Mine 🌹🌹🌹) :
    • “Once and for all” 48 photos to show you the beauty of Flex layout (690+πŸ‘)
    • My 2020 front interview secrets, for your autumn recruit escort (460+πŸ‘)
    • “Noodle” Three rounds of netease Noodle you may need (320+πŸ‘)
    • “Once and for all” WebPack4 configuration (930+πŸ‘)
    • “Fill in the Gaps” to strengthen your HTTP knowledge (1010+πŸ‘)
    • 21 handwritten JavaScript interview questions (620+πŸ‘)
    • 18 browser interview questions (820+πŸ‘)
    • Here are 54 JavaScript interview questions (660+πŸ‘)