Git from zero to entry

             **

Git from zero to entry

1. Git basics
1. Introduction to
Git Git is currently the most advanced distributed version control system in the world.
2. The
difference between Git and Github 2.1.
Git is a distributed version control system. Simply put, it is a software that records changes in the content of one or several files so that you can check the revision of a specific version in the future.

Github (https://www.github.com) is a website that provides users with Git services. Simply put, it is a place where you can put code (but of course you can put more than just code). In addition to providing a web interface for managing Git, Github also provides rich functions such as subscription, follower, discussion group, and online editor. Github is known as the world's largest friend website.
2.2, Github registration

Open the official Github website: https://github.com/ and click the "Sign up" button in the upper right corner.
3. Git installation Just
download and install it on the official website.
Second, the use of Git
1. Local warehouse
1.1,
the three areas of the workflow Git local operation:
Insert picture description here
Insert picture description here
Why is there a temporary storage area?
It is equivalent to people going to the supermarket to buy things, and the temporary storage area functions as a shopping cart, so there is no need for people to go back and forth to the shopping counter.
1.2. Local warehouse operation
What is a warehouse? The warehouse is also known as the version library, and the English name is repository. We can simply understand it as a directory for storing code. All files in this directory can be managed by Git. Git can modify and delete each file. Tracked to.

①After installation, you need to perform global configuration for the first use.
Right-click on a blank space on the desktop and click "Git Bash Here" to open the Git command line window
$ git config --global user.name "Username"
$ git config --global user. email "Email address"
②Create a warehouse
When we need Git to manage a new project/existing project, we need to create a warehouse. Note that the directory used when creating a warehouse does not necessarily require an empty directory. It is also possible to choose a non-empty directory, but it is not recommended to learn Git on existing projects, otherwise you will not be responsible for all the consequences! (It is recommended to use an empty directory for operation)
a. Create an empty directory
1, mkdir file name 2, right-click to create a new
b. Enter the project directory pro_git in the command line (cd.. Is to go back to the previous level)
cd pro_git
c. Git repository Initialization (let Git know that it needs to manage this directory)
instruction: git init
performance: After execution, a hidden directory of ".git" will be created in the project directory. This directory is created by Git and cannot be deleted or changed at will The content of it.
③Git common command operation
View current status: git status is
added to the cache area: git add File name (Note: git add command, you can add one file, or you can add multiple files at the same time. Syntax 1: git add File name Syntax 2: git add file name 1 file name 2 file name 3… Syntax 3: git add.)
Submit to the repository: git commit -m "comment content"
1.3, version rollback
The version rollback is divided into two steps:
Steps: ①Check the
version and determine the point in time that needs to be returned.
Instruction:
git log
git log --pretty=oneline (usually the second one is more comfortable to look at) ②Backup
operation
Command:
git reset --hard commit number

Note: After going back to the past, if you want to go back to the latest version before, you need to use commands to view historical operations to get the latest commit id.
Command: git reflog
summary:

a. If you want to go back to the past, you must first get the commit id, and then go back through git reset -hard;

b. If you want to go back to the future, you need to use git
reflog to view historical operations and get the latest commit id; c. When writing a rollback command,
you don’t need to write all the commit id, git automatically recognizes it, but you can’t write too little. At least the first 4 characters need to be written;
2. Remote warehouse
Online warehouse operation learning takes Github as an example.
2.1. Online warehouse creation
Open the create warehouse page: https://github.com/new The
circled part is required, and the rest can be optionally added according to actual needs:
Insert picture description here
2.2. Based on the http/https protocol
a. Create an empty directory , The name is called shop
Insert picture description here
b. Use the clone command to clone the online warehouse to the local.
Syntax: git clone the address of the online warehouse
Insert picture description here
c. Do the corresponding operations on the warehouse (submit temporary storage area, submit local warehouse, submit online warehouse, pull Take the online warehouse)
Instructions for submitting to the online warehouse: git push
Insert picture description here
has a fatal error of 403 when submitting content to the online warehouse shop for the first time. The reason is that anyone can submit content to the online warehouse, and authentication is required. right.

Need to modify the content of the ".git/config" file:
Insert picture description here
try the push command again after setting the username and password:

If there is no fatal error, the submission is successful.
[Verification] At this time, you can observe the browser and refresh the address of the online warehouse:

Pull online warehouse: git pull
reminder:
The first thing to do every day is to git pull to pull the latest version of the online; the thing to do before work every day is git push to submit the local code to the online warehouse.

2.3. Branch management
What is a branch?
Insert picture description here
In the chapter of version rollback, there will be records after each submission. Git will string them into a timeline to form something similar to a timeline. This timeline is a branch, which we call the master branch.
During development, it is often teamwork and multiple people develop. Therefore, one branch alone cannot meet the needs of multiple people developing at the same time, and working on a branch does not affect the normal use of other branches, which will be safer. Git encourages Developers use branches to complete some development tasks.

Branch related instructions:
view branch: git branch
create branch: git branch branch name
switch branch: git checkout branch name
delete branch: git branch -d branch name
merge branch: git merge branch name to be merged

View branch:
Insert picture description here
Note: There is a mark "*" in front of the current branch.

Create branch:
Insert picture description here
switch branch:
Insert picture description here
merge branch:
now add a new line in the readme file under the dev branch and submit it locally.
Insert picture description here
Switch to the master branch to observe the readme file.
Insert picture description here
Merge the content of the dev branch with the master branch:
Insert picture description here
delete the branch:
Insert picture description here
note: delete When branching, you must exit the branch you want to delete before you can delete it.

After merging all branches, you need to submit the master branch to the online remote warehouse:

2.4. The generation and resolution of
conflicts. Case: Simulation generates conflicts.
①The colleague modified the code of the online warehouse after work.
Note: At this time, the content of my local warehouse is inconsistent with that of the online warehouse.
②When I went to work the next day, I did not do a git pull operation, but directly modified the contents of the local corresponding file.
③I need to submit the code modification to the online warehouse (git push) when I am off work.
At this time, the code line will be Prompt us to perform a git pull operation before pushing again.
[Resolve conflicts]
④First git pull
At this point, git has merged the conflicts between online and local warehouses into the corresponding files.
⑤Open the conflict file and resolve the conflict.
Solution: You need to discuss with your colleague (who submitted it first) to see how the code is retained, and you can submit the modified file again.
⑥ Resubmit
Tips: The first git pull at work can avoid conflicts to a certain extent.

Guess you like

Origin blog.csdn.net/weixin_48683410/article/details/106820424