As a programmer, you must have spent most of your life on Github, which has been dubbed the world’s largest gay dating site. According to? Programmers need a version control system (VCS) to manage their code. This allows programmers to track the details of a project’s changes, trace selected files back to a previous state, and code contributions from individual members on a multi-player project. This paper is a preliminary exploration of the version control software Git, which can fully support the daily collaborative development of multiple people.

Introduction to the

Version control system

A Version Control System is a System that records changes to the contents of one or more files for future reference of revisions to a particular Version. It can be used not only for software source code management, but also for the majority of files (binary files do not support incremental commit) version control. Its development has gone through three stages, namely localization, centralization and distribution. The three phases are constantly optimizing the weaknesses of the original, which was simply copying the entire project to save different versions, to make version control better and better.

  • Localized version control system

A localized version control system uses some kind of simple database to keep track of the changes in the file. It solves the problems such as too large backup files, inconvenient comparison and difficult management in the original rough version system. The typical example is called RCS, which works by keeping a set of patches on hard disk (patches refer to changes made to files before and after revision, also known as differential coding). By applying all the patches, you can recalculate the contents of each version of the file.

TIPS Delta encoding, also known as Delta encoding, is the way data is stored or sent as data differences between sequential data (as opposed to the way data is stored and sent as a complete file). This method is widely used, such as viewing historical change records of files (version control systems: RCS, SVN, Git), remote differential compression in Windows, online backup, Mysql master-slave synchronization, Redis AOF persistence, etc.

  • Centralized version control system

The Centralized Version Control System uses a single, centrally managed server to hold revisions of all files, and all the people working together connect to this server through clients to retrieve the latest files or submit updates. It solves the problem of multi-player collaboration. Typical examples are SVN, CVS, Perforce, etc.

  • Distributed version control system

Distributed Version Control System (Distributed Version Control System) maintains complete historical Version information on the server, and maintains complete historical record of the project on any client. It addresses the single point of failure of the server or disk corruption of the central database. Typical examples are Git, Mercurial, etc.

Introduction of Git

Git is a distributed version control software originally created by Linus Torvalds and released under the GPL in 2005. The original purpose was to better manage Linux kernel development. The advantage is its speed and excellent merge tracking capability. There’s a little story about its birth.

Between 2002 and 2005, Linux kernel projects used a proprietary distributed version control system called BitKeeper for free to manage and maintain code. Because a programmer named Andrew Tritchiu wrote a simple program that could connect to BitKeeper’s repository, the copyright owner of BitKeeper was upset and withdrew the right of the Linux kernel community to use BitKeeper for free. After negotiations failed, Linus Torvalds wrote the first version of Git in ten days to manage and maintain the Linux kernel source code instead of BitKeeper.

When Git manages a project, it has three local work areas: Git workspace, staging, and local repository. As shown below:.

  • Working Directory

    A workspace is all the files in the project directory except the.git folder. Files in this workspace have only two states: traced files and untraced files. Track files are subdivided into committed, Modified, and staged states. The file status is as follows:
  • Stage/Index Area Staging Area is a transition Area from a workspace to a local repository. The corresponding.git/index file.
  • A Local Repository holds submitted data, which is pushed to a remote Repository

Making introduction

GitHu is the largest repository for Git releases and a hub for thousands of developers and projects to collaborate with. Most Git repositories are hosted on GitHub, and many open source projects use GitHub for Git hosting, problem tracking, code reviews, and more. To put it bluntly, GitHub is used as a remote repository and implements quasi-real-time synchronization between local and remote repositories. In this way, even if an exception occurs in the local warehouse, it can be recovered through the remote warehouse.

Actual project version control with Git:

In the figure, workspace represents the current workspace, index represents the staging area (also known as index), Repository represents the local Repository, and Remote represents the Repository on the Remote server, which generally refers to GitHub/Gitlab/Gitee servers.

Git file structure

  • Folders:

    • branches
    • hooksClient-side or server-side hook scripts
    • infoSaved a global copy of ignored patterns that you do not want to manage in a.gitignore file
    • logsThe folder where logs are stored
    • objectsStoring Git Objects
    • refsStores a pointer (sha-1 identifier) file to each branch
  • file

    • HEADPoint to current branch
    • configContains project-specific configuration options
    • descriptionFor use by GitWeb programs only
    • indexThe staging area information is saved

Git operation details

  • Git Clone clone a repository from a remote host

    Copy files from the repository url to a local folder
    git clone< version library url > < local directory name >Use cases support multiple file protocols
    git clone http[s]://example.com/path/to/repo.git/  # the HTTP protocol
    git clone ssh://example.com/path/to/repo.git/      # SSH protocol
    git clone git://example.com/path/to/repo.git/      # the git protocol
    git clone /opt/git/project.git                     # local file
    git clone file:///opt/git/project.git              # local file
    git clone ftp[s]://example.com/path/to/repo.git/   # the FTP protocol
    git clone rsync://example.com/path/to/repo.git/    # rsync protocol
    Copy the code
  • Git remote Manages the remote host name

    # usageGit remote addAdd a remote host nameGit remote rmDelete the remote host nameGit remote rename < git remote rename ># change the host nameGit remote showDisplay host name details
    Copy the code
  • Git fetch fetch (clone from scratch,fetch from something to something)

    Git fetch < host > < branch >Copy the code
  • Git branch/checkout/merge/rebase management branch (git is the biggest characteristic)

    Use to view the branches
    git branch -r    # View remote branches
    git branch -a    Remote branches start with remotes/
    Create a new branch
    git checkout -b newBranch origin/master   Create a new branch based on origin/master
    # use merge branches
    git merge origin/master      # merge origin/ Master branch into local branch
    git rebase origin/master
    Copy the code
  • Git pull fetches updates from a branch of the remote host and merges them with the specified branch of the local host

    Git pull < remote host name > < remote branch name ># is equivalent toGit fetch < remote host > < remote branch >Copy the code
  • Git push pushes local branch updates to remote hosts

    Git push < remote hostname > < local branch name >Copy the code

Practice in Linux (Take Ubuntu as an example)

The installation

  • Install Git

    sudo apt-get install git
    Copy the code
  • Configure SSH Keys

    Save the private key to the github file and the public key to the github
    ssh-keygen -C "[email protected]" -f ~/.ssh/github
    Copy the code
  • Configuration making

Log in to GitHub and click Settings => SSH keys => Add SSH key to Add the public key in GitHub.

  • Authentication is successful

    # execute command
    ssh -T [email protected]
    # prompt
    Hi yuguoliusheng! You've successfully authenticated, but GitHub does not provide shell access.
    Copy the code
  • Configure git personal information

    Set user name
    git config --global user.name "yuguoliusheng"
    Set the email address
    git config --global user.email "[email protected]"
    Copy the code

    TIPS: Configuration level: System/user/project repository

    • System level

      Set the /etc/gitconfig file to be valid for each user and project repository on the machinegit config --system user.name "yuguoliusheng"
    • The user level setting $HOME/.gitConfig file is valid for each project repository of the current usergit config --global user.name "yuguoliusheng"
    • Project repository level sets the.git/config file in the current project, which is only valid for the current project repositorygit config --local user.name "yuguoliusheng"

    The priority level is: Project repository level > User level > System level

use

  • Create the repository for the existing project project

    # Execute the following command at the root of project
    
    Create the readme.md file
    echo "# Git" >> README.md
    Create an empty git repository (create.git folder)
    git init
    Add all files to the staging area
    git add *
    Commit the staging area file to the local repository with the information "initial project"
    git commit -m "initial project"
    # to add to the remote host name origin, it stands for https://github.com/yuguoliusheng/introduce.git
    git remote add origin https://github.com/yuguoliusheng/introduce.git
    # push the local master/main branch to the Master /main branch on the Origin host and set Origin as the default host
    git push -u origin master/main
    Copy the code
  • Clone a repository locally from a remote server

    # clone from https://github.com/yuguoliusheng introduce. The git to introduce local folders
    git clone https://github.com/yuguoliusheng/introduce.git introduce
    Copy the code
  • Ignore certain files or folders

Some files don’t need to be managed by Git, and you don’t want them to be in the list of untracked files. These are usually automatically generated files, such as logs or files created during compilation, etc. You can create.gitignore files to mask these

*. A # Ignore all.a files! /TODO # ignores only TODO files in the project root directory, excluding subdir/TODO build/ # ignores all files in the build/ directory Doc /notes.txt, but not doc/server/arch. TXTCopy the code
  • View submitted updates

    # view the commit log and display the difference between the last two commit updates
    git log -p -2
    Copy the code

    -p Displays the differences between each update in patch format. –stat Displays file modification statistics for each update. –shortstat displays only the last row in –stat. Pretty uses other formats to display historical submission information. Available options include oneline, short, full, fuller and format (followed by a specified format).

  • Store the current workspace

Storage stashing captures the intermediate state of your working directory — that is, the tracked files and pending changes you’ve modified — and stores it in a stack of pending changes, ready to be reapplied.

Bash # store the current workspace, You can switch to another workspace # show the previously stored workspace git Stash list # Re-enable the stored workspace git stash apply stash@2 # stash@2 # Create a branch from the stash stash branch testchanges' 'Copy the code

reference

  1. Git Pro 2nd edition
  2. Baidu Encyclopedia VCS
  3. Wikipedia Git
  4. Local version control system (RCS)
  5. Differential encoding of Wikipedia

‘If the article is helpful to you, or you are interested in technical articles, you can pay attention to the wechat public number: technical tea party, can receive the relevant technical articles in the first time, thank you!

`

This article was automatically published by ArtiPub