Version control – Git in a nutshell

  • Brief introduction to version control
  • 2. Introduction to Git
  • Git basic operations
  • Git branch
  • Git configuration

Version Control Overview

What is the simplest version control

A way to copy the entire project directory with a time suffix

From the local control system – RCS

  • Save patch sets on hard disk (patches are changes to files before and after revision)
  • By applying all the patches, you can recalculate the contents of each version of the file

To Centralized Version Control (SVN)

  • A single centrally managed server that holds revised versions of all files
  • People working together are connected through clients

To Distributed Version Control Systems (Git)

  • Instead of just taking a snapshot of the latest version of the file, the client mirrors the code repository completely
  • You can specify interactions with several different remote code repositories

Differences between Git and other version controls

Many systems on SVN, for example, store information as a set of base files and the cumulative differences between each file over time


Git takes a snapshot of all files at the time of saving and stores the index of the snapshot

Advantages generated

  • Almost all operations are performed locally
    • Fast response time, no waiting
    • Work locally without networking
  • Integrity is guaranteed. It is not possible to change any file contents or directory contents at will

All data is hashed before being stored

  • The operation only adds data


Introduction of git


Three states

  1. Committed ———— Data is safely stored in the local database
  2. ———— modified the file but has not been saved to the database
  3. Passively stored ———— marks the current version of the modified file for inclusion in the next submitted snapshot

These three states also correspond to three working area concepts: Git repository, working directory, and staging area

The Git repository directory is where Git holds your project’s metadata and object database. This is the most important part of Git, and this is where you copy data when you clone repositories from other computers.

A working directory is a separate extract of a version of a project. These files are extracted from a compressed database in a Git repository and placed on disk for you to use or modify.

The staging area is a file that holds a list of files that will be committed next time, typically in a Git repository directory.

Our daily Git workflow

  1. Modify files in the working directory
  2. Temporary file, a snapshot of the file into the temporary storage area
  3. Commit the update, locate the files in the staging area, and store the snapshot permanently to your Git repository directory

Git life cycle


Git Basic Operations


The user related

The first thing you should do after installing Git is set up your user name and email address

git config --global user.name "BugMaker"
git config --global user.email "[email protected]"
Copy the code

This information will be used in every subsequent Git commit, and it will be written to your commit, immutable


The most common file submission operations


Git add. Git rm file git rm file git rm --cached file git add Git commit -m "update file" git commit -a -m Git statusCopy the code

Status Indicates several states

  • new file
  • modified
  • deleted
  • renamed


Common auxiliary operation


Git log -p -p git log -p git log -p git log Forgotten_file) git commit --amend #Copy the code

Git reset –hard loses all current changes!

Remote operation

Git push [remote-name] [branch-name] git push [remote-name]Copy the code
  1. Git fetch will visit a remote repository and pull any data from it that you don’t already have
  2. It does not automatically merge or modify your current work, you need to merge the changes manually

For branches that have been set up for remote tracing, the git pull command automatically grabs and merges the remote branches into the current branch

git pull => git fetch && git merge
Copy the code



Git tag tag operation, if you are interested in git documentation to read




Git branch

What is a Git branch?

Review: What does Git keep?

Snapshot files

What does Git commit?

An object containing three parts:

  1. Snapshot information for each file
  2. A tree object that records the directory structure and index
  3. The submitted object that contains the pointer to the tree object and all the information

After multiple commits, we index the pointer inside

So back to the original question, what is the nature of git branching


Git branches are Pointers to committed objects:

Git’s default branch name is master. After multiple commits, the master branch points to the last committed object

Git branch testingCopy the code

So how does Git know which branch we’re on?

Git uses a special pointer to HEAD to identify which branch you are using

Git checkout TestingCopy the code

Merging of branches

Fast forward to merge

$ git checkout master $ git merge hotfix Updating f42c576.. 3a0874c Fast-forward index.html | 2 ++ 1 file changed, 2 insertions(+)Copy the code

We see fast-forward at merge time, which means that the current master branch is pointing to a commit directly upstream of the current commit (a hotfix commit), so Git simply moves the pointer forward

Three-way merge

More often than not, the changes made by the two branches to merge have resulted in different advances

$ git checkout master Switched to branch 'master' $ git merge iss53 Merge made by the 'recursive' strategy. index.html |  1 + 1 file changed, 1 insertion(+)Copy the code

When this happens, Git does a simple three-way merge using the snapshots indicated at the ends of the two branches (C4 and C5) and the working ancestors of both branches (C2)

After the merge, Git takes a new snapshot of the result of the three-way merge and automatically creates a new commit pointing to it. This is called a merge commit, and it is special in that it has more than one parent commit

Git decides which commit is the best common ancestor to use as the basis for merging

Merge conflicts

Vscode or other IDE operations are recommended…

rebase

Merging different branches In addition to the usual merge, there is also the rebase operation

We can also extract patches and changes introduced in C4 and apply them once over C3. In Git, this operation is called rebase. You can use the rebase command to move all changes committed to one branch to another

$ git checkout experiment
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command
Copy the code

The principle is to first find C2, the most recent common ancestor of the two branches (i.e., the current branch experiment and the target base branch Master of the base-changing operation), and then compare the previous commits of the current branch with the ancestor to extract the corresponding modifications and save them as temporary files. The current branch is then directed to the target base C3, where changes previously saved as temporary files are applied sequentially

$ git checkout master
$ git merge experiment
Copy the code

The rebase command keeps the entire commit history clean by making the parallel work history serial. This is how most open source projects contribute code.


Storage and cleaning

When you’ve been working on one part of a project for a while, everything gets jumbled up and you want to switch to another branch and do something else. The problem is, you don’t want to create a commit for a half-done job just because you come back to this point later. The answer to this problem is the git stash command

This command takes care of the dirty state of the working directory – that is, the modified trace file and the temporary changes – and then saves the unfinished changes on a stack, which you can reapply at any time

Drop the stash and drop the git stash popCopy the code




Git configuration

The git protocol

  1. The HTTP protocol
    • Advantages: Only one URL and authorization information are required. (If no + is required, SSH needs to be configured.)
    • Disadvantages: Storage and input of authorization information
  2. SSH protocol
    • Advantages: Access security, no need to enter credentials
    • Disadvantages: Does not support anonymous access

You are advised to configure SSH to improve overall efficiency.

For details, see gitlab SSH configuration guide


SSH configuration for multiple users


You can set up a config file in the.ssh directory to configure multiple accounts

  • Host: another name. You are advised to keep it the same as hostName. Otherwise, the original command will be changed
  • User: indicates the User name
  • HostName: indicates the real domain name
  • IdentityFile Specifies the path of the private key
Host code.zikuinfo.com User chengyupeng HostName code.zikuinfo.com IdentityFile ~/.ssh/id_rsa Host github.com User SSH -t git@<HostName>.comCopy the code

GUI Tools (recommended)

Finally, git GUI tool sourcetree is recommended

Skip the registration entry method