Want to write some detailed things, it is best to let the new people simple to use, can daily use, understand a general; It may not be an official term, but the goal is to make it easier for beginners to get on the road.

Basic concept

What is a Git

Git is the most advanced distributed version control system in the world.

Let’s start with version control. For example, if you take a Word file, modify it and save it, modify it and save it, modify it and save it until you are satisfied. Version control is the ability to view and restore any previously saved file at any time.

There are two common version control systems, one is SVN and one is Git. So what’s the difference between them? The difference is that Git is a distributed version control system. Take another example, there is an account of the ledger Mr. Zhou, each wife go out to go shopping after the need to go to the ledger Mr. Zhou there to register, usually good, a few wives will not meet in the ledger, after all, who also do not want to let others say their spending is big. However, when festivals come, such as the Mid-Autumn Festival or the Dragon Boat Festival, the wives will inevitably meet and have conflicts. This is SVN, centralized, accounting that account lost, there is no way to statistics.

So what is Git? It is this example of accountant zhou likewise, the difference has a small account there with each wife, after going out to play every time can be recorded in his small account first, wait for need to go to accountant Zhou to register there again, calculate accountant that account lost so, also need not too much with worry.

Figure:

SVN Flow Chart

Git Workflow

What is a Git – Flow

Git-flow is the specification of Git commit. After all, multiple engineers develop the same project. If they do not agree on a commit specification, they will basically not know what to commit and need to read the source code of commit changes bit by bit, which is very inconvenient.

Basic operation

Basic Git operations

Example Generate an SSH secret key

SSH // run the following command to check whether the directory exists: CD ~/. SSH // Check whether the directory exists. If yes, the SSH key has been generatedCopy the code

Copy the public key and add it to the code hosting platform

Cat id_rsa.pub // Run the 'cat' command to view the contents of file 'id_rsa.pub' and copy itCopy the code

For example, on Github, go to Settings -> SSH and GPG Keys and paste in the copy. Click Add SSH Key. This step means that you are hosting your public key on Github.

Configure the user name and mailbox globally

Git config --global user.name "git config --global user.email" git config --global user.name "git config --global user.email"Copy the code

git init

Initialize an empty repository

git init

Copy the code

git add

Adds a file to the staging area

Git add XXX # Add all files that are currently changed to the staging area. git add .Copy the code

git commit

Commit the changes

Git commit # Commit the staged changes, and the new editor is opened for editing. Git commit -m "you message" # = git add. && git commit -m git commit -am # = git add git commit --amendCopy the code

git pull

Push the code to the remote repository

# Pull code from remote repository and merge it locally Git pull --rebase < remote host name > < Remote branch name >:< Local branch name >Copy the code

git fetch

Unlike Git pull, git FETCH only pulls remote changes and does not merge automatically. Has no effect on your current code

< branch name > < branch name > < branch nameCopy the code

git branch

Branch related Operations

# Create local branch Git branch -r # check local and remote branches git branch -a # Check local and remote branches git branch -r # Check local and remote branches git branch -a # Check local and remote branches git branch Git branch -m <old-branch-name> <new-branch-name>Copy the code

Git-flow Basic operations

git flow init

Initialize the

Initialized to empty Git repository/Users/jouzeyu/test /. Git/No branches exist yet. The Base branches must be created now. The Branch name for Production releases: [master] main // Select Branch releases from 'next Release'; [Develop] How to name your supporting branch prefixes? Feature branches? [feature/] Release branches? [release/] Hotfix branches? [hotfix/] Support branches? [support/] Version tag prefix? []Copy the code

Some higher git versions already have git-flow integrated by default. If your version does not support git-flow, check out the installation instructions at Github -> GitFlow

git flow feature

Develop a new feature

git flow feature start your-branch-name

Copy the code

Start is usually followed by the name of your new function. It will create a new branch based on the Develop branch. When the function is developed, you can run:

 git flow feature finish your-branch-name
 
Copy the code

This will merge your new feature into the Develop branch and remove it, then switch back to the Develop branch. Of course, you can use the git log command to view the operation history.

git flow release

Release a new version

Git Flow Release start v0.0.1Copy the code

To release the branch once the new version is tested, you can run:

Git Flow Release finish V0.0.1Copy the code

Notice that the name of this release is usually v plus 3 digits because it’s a new project and there’s no tag and the tag starts at 0 and that’s v0.0.1 depending on the company rules

git flow hotfix

When we have a bug that needs fixing and we don’t want to affect the release branch, we can create a new HotFix branch to fix the bug. This is based on the new HotFix branch that the master branch created for us:

Git Flow Hotfix start v0.0.1.1Copy the code

When we are finished:

Git Flow Hotfix finish v0.0.1.1Copy the code

He will help us merge the branch into the master and then tag and delete the branch. You can also use the git log command to view the operation history.

Note the named version number: We usually use v+4 bits to name the version number and we need to look up the latest release branch because the release is 3 bits and we need to add one bit to that because the release we just finished up there is v0.0.1 so we’re going to create a new hotfix The name of the branch is v0.0.1.1

If the latest release of git tag is v1.2.3, then our hotfix branch can be named v1.2.3.1 and so on

Git problem solving scenarios at work

Switch the remote warehouse address

For example, if github hosted code is switched to GitLab for company code security, then the remote repository address must be changed. You can do this:

Git remote add origin url = git remote add origin url = git remote add origin url = git remoteCopy the code

Use git Stash to stash files

It is very common to have a bug in your production environment that needs to be fixed while you are working on a new feature. You can use the Git stash command to save the changed files in your workspace and then switch to the HotFix branch to fix the bug. When you have fixed the bug, switch back to the Feature branch. Recovers from the stack what has just been saved.

Git stash Save "message" Adds notes when storing them for easy retrieval. Git stash pop // Applies the last cached change and removes the cached record. Git stash apply // applies a store but does not remove it from the list of stores. The default is to use the first store, stash@{0}. Git stash apply stash@{$num}. Git stash list // Check what stash stores are available. Git stash Clear // Deletes all cached stash storesCopy the code

Use Git Alias to reduce operations

You can alias some commands with git alias, such as git commit=>git cm. You can do this by using the following command: git config –global alias

Other to be added

reference

How do I use Git at work

Summary of Daily use of GitFlow