01 git getting started

Background analysis

In the process of software project development, the code and documents shared between teams may be accidentally overwritten or lost by others or themselves. I don't know who changed this code for what reason, and there is no way to recover the modifications made a few days ago. Therefore, the version control system (VCS version control system) was born.

With the version control system, we can browse the historical records of all development, master the development progress of the team, and we are no longer afraid of making any changes, because you can easily restore the previous normal version. We can also distribute different versions of software through the functions of branches and labels, such as stable version, maintenance version and developing version. The application trend of version control system in recent years is shown in the figure:

Version Control System (VCS) can be divided into two categories: centralized and distributed. Centralized, as the name suggests, is to use a single server to centrally manage and save all the files of the project. Members of the project team connect to the server through the client to download or submit files. As shown in the figure:

Once the centralized client cannot connect to the server, the version control function will not be available (for example, comparing the differences of historical versions, viewing the contents of a historical version, etc.). The outstanding representative of centralized VCS is SVN

The distributed feature is that each client can connect to a centralized server, the client itself can be a complete version control warehouse, and project team members can version manage files on their own computers. As shown in the figure:

Compared with the centralized version control system, the security of the distributed version control system is much higher, because everyone's computer has a complete version library. It doesn't matter if someone's computer breaks down. Just copy one from others. If there is a problem with the central server of the centralized version control system, everyone can't work. The outstanding representative of distributed VCS is git.

Git introduction

Git is an open source distributed version control system. The author is legendary Linus and the famous author of open source operating system Linux. liuns spent two weeks writing a git in C. The Linux source code is managed by GIT. Git is now the overlord of the Jianghu in the field of VCS.

Git installation (Setting Up)

Download and install

For Windows and Mac systems, you can directly http://git-scm.com/downloads Download and run the installer from the web site. For Windows users, you can use the "Git Bash" command line tool (which comes with Git installation) to use Git, and the MAC system can directly use the Terminal window "Terminal".

Global user information configuration

Configure user and password

$ git config --global user.name "your-name"
$ git config --global user.email "your-email@youremail.com"

Check configuration information

$ git config --list
user.email=xxxxxx@xxxxxx.com
user.name=xxxxxx

Git basic application implementation

Introduction to Git command

Git provides a set of simple, unique and independent commands. The execution of these commands requires starting the GIT terminal of windows or mac system first (Git Bash can be used under window). Its syntax structure is:

$ git <command> <arguments>

Git common commands are:

init, clone, config: Used to start Git Project management.
add, mv, rm: Used to temporarily record or store changes to files.
commit, rebase, reset, tag:
status, log, diff, grep, show: show status
checkout, branch, merge, push, fetch, pull

Git help manual: (you can quickly get help using commands)

$ git help <command>
// or
$ git <command> --help

Git operation manual official document http://git-scm.com/docs.

Git quick start

There are two ways to start a Git management project:

  • Create or start a new project, and then perform git operations.
  • Clone an existing project from a Git host.

Let's start with the new project and learn the basics:

First create a git working directory, named githello, and then create a hello Java, whose code is:

public class Hello {
   public static void main(String[] args) {
      System.out.println("Hello, world from GIT!");
   }
}

Suggestion, in hello Create a readme. Java directory as well MD file, which describes your project. For example, add the following contents to the file:

This is the README file for the Hello-world project.
  • Initialize git warehouse

When implementing project management based on Git, first execute "git init" in the root directory where the project is located (such as githello). The process is as follows:

git init 

// Change directory to the project directory
//$ cd /path-to/hello-git
 
// Initialize Git repo for this project

$ git init
Initialized empty Git repository in /path-to/hello-git/.git/

$ ls -al
drwxr-xr-x    1 xxxxx    xxxxx      4096 Sep 14 14:58 .git
-rw-r--r--    1 xxxxx    xxxxx      426 Sep 14 14:40 Hello.java
-rw-r--r--    1 xxxxx    xxxxx       66 Sep 14 14:33 README.md

After the execution of git init instruction, a ". git" directory will appear in the root directory of git local library (this directory is hidden by default).

Git storage model, as shown in the figure:

When we execute the git init instruction, the local library is actually empty. You need to explicitly store files in the repository.

  • Changes to staging and updating files.
// Add README.md file
$ git add README.md
 
$ git status
On branch master
Initial commit
 
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   README.md
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Hello.java
 
// You can use wildcard * in the filename
// Add all Java source files into Git repo
$ git add *.java
 
// You can also include multiple files in the "git add"
// E.g.,
// git add Hello.java README.md
 
$ git status
On branch master
Initial commit
 
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   Hello.java
        new file:   README.md

1) Command "git add < File >.." Accept one or more file names or pathnames that may have wildcard patterns. You can also use "git add." Add all files in the current directory (and all subdirectories).
2) When a new file is "add ed", it will be stored in git's staging area (not commit ted to local library).

Git uses two processes to submit changes to documents:
1) "Git add < File >" stores the file in the staging area.
2)"git commit" submits all changes to the file from temporary access to the local library.

  • Git commit

    If we want to submit the change information of GIT temporary storage file to the ontology library, we need to apply the "git commit" instruction, for example:

$ git commit -m "First commit"
[master (root-commit) 6e49a29] first commit
 2 files changed, 1 insertion(+)
 create mode 100644 Hello.java
 create mode 100644 README.md

Next, you can also view the Git status:

$ git status

Display Git submission log information:

Git records the submitted metadata every time, including log messages, timestamp, author's user name, e-mail, etc.). We can use the "git log" instruction to display the submitted data. You can also use "git log --stat" to view the statistics information of the file.

$ git log
git log --stat
  • . gitignore file

In Git, there is a special file whose full name is gitignore, whose main function is to screen some files based on rules so that these files are not tracked. Naturally, they will not be uploaded to github and other platforms after being pushed. At this time, we only need to create it in the directory where the push operation needs to be performed gitignore file, and then define its rules, for example:

# .gitignore
 
# Java class files
*.class

# Executable files
*.exe

# temp sub-directory (ended with a directory separator)
temp/

Now, execute the "git status" command to check for untracked files.

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
      .gitignore
nothing added to commit but untracked files present (use "git add" to track)

Typically, we also track or submit gitignore file, for example:

$ git add .gitignore
 
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   .gitignore
 
$ git commit -m "Added .gitignore"
[master 711ef4f] Added .gitignore
 1 file changed, 14 insertions(+)
 create mode 100644 .gitignore
  
$ git status
On branch master
nothing to commit, working directory clean

Git remote warehouse settings

When we use Git for project management, we usually submit the project to the remote warehouse. The specific steps are as follows:
Step 1: create a remote warehouse (such as Gitee) - create it yourself.
Step 2: set the remote warehouse name and url mapping.
Via the via "git remote add < remote name > < remote URL >" instruction, set the remote warehouse binding with the name of "origin".
For example:

$ git remote add origin https://gitee.com/JasonCN2008/gittest.git

Render the remote warehouse name as a list:

$ git remote -v

Step 3: push and commit the contents of the library to the remote warehouse.

Push the contents of the local library to the remote library (you need to enter the account and password of the remote warehouse during the execution), and its syntax is git push - U < remote name > < local branch name >, for example:

$ git push origin master

Demonstration of practical process:

Modify hello Java file content, for example:

public class Hello {
   public static void main(String[] args) {
      System.out.println("Hello, world from GIT!");
      System.out.println("Push and Commit !");
   }
}

Perform Git operation

$ git status
// Stage file changes

$ git add *.java

$ git status

// Commit all staged file changes
$ git commit -m "Third commit"
[master 744307e] Third commit
 1 file changed, 1 insertion(+)
 
// Push the commits on local master branch to remote
$ git push origin master

Clone project from remote warehouse

Execute "git clone < remote URL >" to initialize a local library, and copy the resources in the remote library to the working directory of the local library. For example, first switch to git working directory, and then execute:

$ git clone https://gitee.com/JasonCN2008/gittest.git githello-clone

After Clone is finished, switch to git Hello Clone directory and execute ls -a instruction to view the resources in the directory.

Summary

This section mainly analyzes and practices the basic instruction operation of Git. Now we will summarize these orders:

// Edit (Create, Modified, Rename, Delete) files.  
// Stage file changes, which produces "Staged" file changes
$ git add <file>                          // for new and modified files
$ git rm <file>                           // for deleted files
$ git mv <old-file-name> <new-file-name>  // for renamed file

// Commit (ALL staged file changes)
$ git commit -m "message"

// Push
$ git push <remote-name> <local-branch-name>

Tags: git

Posted by AE117 on Sun, 15 May 2022 20:39:31 +0300