Skip to content
中文
9 min read75

Git Learning Notes

Git Learning Notes

Updated:

阅读中文版

Git

1. Introduction and Installation of Git

1.1 Introduction to Git

After Linux creator Linus created the open-source Linux operating system in 1991, it grew over the years through the collective efforts of enthusiastic volunteers worldwide and has now become the largest server operating system in the world. In the early days, code contributors would send source files to Linus, who would manually merge them. After years of this workflow, the codebase grew so large that manual merging became unsustainable. Linus, who deeply disliked centralized version control systems, chose BitKeeper, a distributed commercial version control system, though the Linux community builders could use it for free. BitKeeper changed Linus's understanding of version control, but he also discovered some shortcomings in BitKeeper. Moreover, a critical issue prevented its widespread use: it was not open-source.

In 2005, the company behind BitKeeper discovered that someone in the Linux community was attempting to crack it, and BitKeeper decided to revoke the free usage rights for the Linux community. After weeks of unsuccessful mediation, Linus searched through all known version control systems of the time and found none satisfactory. In a fit of frustration, he decided to build his own. Linus spent ten days writing an open-source version control system in C, which became the famous Git.

In 2007, three young people in San Francisco thought Git was a great tool and founded a company called GitHub. The following year, they launched the website of the same name, written in Ruby — a free code hosting site based on Git (with paid services available). Over the next decade, the site skyrocketed in popularity, defeating the powerful Google Code to become the world's most popular code hosting platform. In June 2018, GitHub was acquired by the deep-pocketed Microsoft. In January 2019, GitHub announced that users could create private repositories for free. According to GitHub's annual report from October 2018, 31 million developers had created 96 million project repositories, with 2.1 million enterprises onboard.

In one sentence: Git is currently the world's most advanced distributed version control system.

1.2 Centralized Version Control Systems

image-20221018194814392

Advantages:

The SVN repository is centrally stored on a central server. When working, you use your own computer, so you first need to get the latest version from the central server, then do your work, and after finishing, push your completed work back to the central server.

Disadvantages:

You must be connected to the internet to work. If you're on a LAN with sufficient bandwidth and speed, it's fine. But if you're on the internet with a slow connection, it becomes frustrating.

  1. If the version management server crashes or the hard drive fails, how do you recover the code?
  2. The code uploaded to the server by programmers is required to be a complete version, but what if programmers want to manage smaller versions during development?
  3. The system is running in production, bugs need to be fixed from time to time, and adding several new features takes months — how do you manage multiple versions?
  4. How do you manage a large development team spread across the world, where members don't know each other?

1.3 Distributed Version Control Systems

image-20221018200617932

Git is a distributed version control system, meaning it has no central server. Each person's computer is a complete repository, so you don't need to be online to work since all versions are on your own machine. Since everyone has a complete repository on their computer, how do multiple people collaborate? For example, if you modify file A on your computer and someone else also modifies file A on theirs, you simply push your changes to each other, and you can both see each other's modifications.

1.4 How Git Works

image-20221018202104215

Note: All Git commands start with git.

1.5 Installing Git

Download address: https://git-scm.com/download

image-20221018200511831

1.5.1 Review the license agreement, click Next

image-20221018203359801

1.5.2 Choose installation location, click Next

image-20221018203601137

1.5.3 Git Bash needs to be selected; for the rest, just click Next

image-20221018204345638

1.5.4 Default, click Next

image-20221018204535145

1.5.5 Default editor, click Next

image-20221018204652005

1.5.6 Default, click Next

image-20221018204856546

1.5.7 Choose the Git command execution environment

It's recommended to select the first option, which uses Git's own dedicated command line window.

image-20221018205059640

1.5.8 HTTPS transport protocol: Use the SSL transport protocol

image-20221018205345221

image-20221018205422347

1.5.9 Configuring the line ending conversions

Select the first option

image-20221018205533821

1.5.10 Terminal emulator

First option: Use Git's dedicated window (recommended)

Second option: Use Windows' cmd command line window.

image-20221018205911479

image-20221018210055615

1.5.11 For everything else, just keep the defaults and complete the installation

image-20221018210123873

image-20221018210212403

After installation, you can right-click in any directory to open the Git command line window.

image-20221018210719073

Since Git is a distributed version control system, you need to fill in a username and email as an identifier.

--global indicates global properties, which will be shared by all Git projects

git config --global user.name "username"
git config --global user.email "user email"

image-20221018211057201

You can see this in the .gitconfig file at C:\Users\Administrator. If you don't want to use commands, you can also set it directly in the file.

image-20221018211335827

2. Git in Practice

2.1 Creating a Local Repository

Create a directory (to serve as the local repository): C:\GITRepository\git-demo

Open a Git Bash window in the C:\GITRepository\git-demo directory and initialize the repository: git init

image-20221018212049811

Linux commands can be used in the Git Bash window.

2.2 Checking Status

image-20221018213932162

2.3 Creating and Editing Files

image-20221018214255482

2.4 Adding Files to the Staging Area

git add hello.txt

image-20221018215006491

2.5 Committing Files to the Local Repository

-m is followed by the commit message

git  commit -m "first commit" hello.txt

image-20221018215841993

View version information

$ git log

2.6 Modifying Files

Modify the hello.txt file and add some content

image-20221018220715448

After modifying, check the status — the hello file shows up in red again, indicating that the modified file hasn't been added to the staging area yet.

Add it to the staging area and commit the changes

git add hello.txt
git commit -m "second commit" hello.txt

2.7 Version History

2.7.1 Viewing Version History

git reflog  View version information

git log  View detailed version information

image-20221018222140129

2.7.2 Selecting a Version

git reset --hard version_number

3. Git Branches

After the system goes live, you need to fix bugs and develop new features at the same time. Since the new features aren't finished, you need to create a branch — fixing bugs on one side while developing new features on the other, and finally merging them.

image-20221025064316286

3.1 Viewing Branches

git branch -v

image-20221020063420392

3.2 Creating a Branch

Create a test branch: git branch <branch name>
git branch test

image-20221020064139217

3.3 Switching Branches

Switch to the test branch: git checkout <branch name>

image-20221020064328739

3.3.1 On the test branch, create a.java, add it to the staging area, and commit it to the local repository

touch a.java
git add a.java
git commit -m "first commit on test branch" a.txt

3.3.2 Switch back to the master branch and check if there are any changes

git chechout master
ls

You'll find that a.java created on the test branch doesn't exist on master.

image-20221020065750389

3.4 Merging Branches

First switch to the main branch, then merge the branch

git checkout master
git merge test

image-20221020070043378

At this point, you can see the changes from the test branch — the branch merge is complete.

3.5 Deleting Branches

Delete the test branch

git branch -d test

View branches

git branch -v

image-20221020070603722

3.6 Version Conflicts

3.6.1 Operations on the test branch

Create the test branch
git branch test
Switch to the branch
git checkout test
Modify a.java, add 1 line of data
vim a.java
Add a.java to the staging area
git add a.java
Commit a.java to the local repository
git commit -m "test modified a.java"

image-20221024164000767

3.6.2 Operations on the master branch

Switch to the master branch
git checkout master
Modify a.java, add 1 line of data
vim a.java
Add a.java to the staging area
git add a.java
Commit a.java to the local repository
git commit -m "test modified a.java" a.java
A conflict occurs
git merge test

image-20221024165255180

Reason for the conflict: The test branch and the master branch have two different sets of modifications at the same location in the same file. Git cannot decide which set of code to use, so a human must manually determine the content of the new code.

3.6.3 Resolving the Conflict

View the conflicting file
cat a.java
Manually resolve by modifying the conflicting file, keeping the code you need
vim a.java
Add and commit to resolve the conflict
git add a.java
git commit -m "branch merge, conflict resolved" 

image-20221024165456445

4. Integrating Git with IDEA

4.1 Configuring Ignore Files

These files are unrelated to the actual functionality of the project and are not deployed or run on the server. Ignoring them helps mask differences between IDE tools.

# Compiled class file
*.class

# Eclipse
.project
.classpath
.settings/

# Intellij
*.ipr
*.iml
*.iws
.idea/

# Maven
target/

# Gradle
build
.gradle

# Log file
*.log
log/

# out
**/out/

# Mac
.DS_Store

# others
*.jar
*.war
*.zip
*.tar
*.tar.gz
*.pid
*.orig
temp/

Create a git.ignore file, add the above content to it, and reference it in ~/.gitconfig

Note: Use "forward slashes (/)", not "backslashes ()"

[user]
	name = liuzhihao
	email = lzhgy163@163.com
[core]
	excludesfile =C:/Users/刘智豪/git.ignore

4.2 Locating the Git Installation

Top-left corner: File -> Settings

image-20221024180044092

4.3 Initializing Version Information

image-20221024180743025

4.4 Adding to the Staging Area

Right-click the project, select Git -> Add to add the project to the staging area.

image-20221024190045452

4.5 Committing to the Local Repository

image-20221024190307405

image-20221024190657749

4.6 Switching Versions

In the bottom-left corner of IDEA, click Git, then click Log to view versions.

image-20221024191320113

Right-click the version you want to switch to, then click Checkout Revision in the menu.

image-20221024191511388

4.7 Creating Branches

Git -> Branches

image-20221024191844462

In the Git Branches dialog that appears, click the New Branch button.

image-20221024191948588

4.8 Switching Branches

You can select a branch from the bottom-right corner.

image-20221024192229008

4.9 Merging Branches

Git -> Merge

image-20221024192726777

image-20221024192707700

4.10 Version Conflicts

Modify code on the test branch and commit it to the repository

image-20221024194152056

Modify code at the same location on the master branch and commit it to the repository

image-20221024194536995

Merge the test branch into the master branch

image-20221024194608494

A conflict occurs — resolve it manually

image-20221024195209653

5. Gitee

Gitee is a code hosting and collaborative development platform launched by the Open Source China community. It supports Git and provides free private repository hosting. Gitee has now become the largest code hosting platform in China. It helps developers store and manage their project source code, tracks, records, and controls user modifications to their code, and provides a stable, efficient, and secure cloud-based software development collaboration platform. Whether for individuals, teams, or enterprises, Gitee enables code hosting, project management, and collaborative development. Its website is https://gitee.com/, it works just like GitHub, and it's also a Chinese-language site.

5.1 Account Registration

Go to the official website https://gitee.com/, click Register, and fill in the required information in order.

image-20221024174321046

image-20221024174340830

5.2 Account Login

Enter your username and password to log in.

image-20221024195354358

5.3 Creating a Remote Repository

Click "Create Repository" in the top-right corner.

image-20221024195811908

image-20221024200156018

5.4 Integrating Gitee with IDEA

Install the Gitee plugin

image-20221024201843909

Log in with your username and password

image-20221024202137801

Push local code to the Gitee remote repository

image-20221024202523494

Custom remote repository connection

image-20221024202637405

image-20221024202902549

Click Push to complete the upload.

Comments(0)