1. Introduction to git
why use git
git mainly manages code
1. Version backtracking
2. Version switching
git repository
trunk
Branch A - used by team member A
Branch B - used by team member B
Branch C - used by team member C
3. Multi-person collaboration
git remote repository
Contribute and pull
A B C
4. Remote backup
github remote repository
push pull
git local repository 1 git local repository 2 git local repository 3
2. Git environment construction:
1. There is already git on the operating system, but the version is too old
Git update-git-for-windows
2. Git has not been installed
Verify installation: git --version
Git installation documentation: Git installation_Dongguo's blog-CSDN blog_git installation
click to run
The installation was successful
3. Set username and email password
It is convenient to submit the code later, and the email address should be consistent with github
git config --global user.name hxy
git config --global user.phone 123456
git config --global user.email "**********@qq.com"
Check if the username is set successfully:
git config --list
Note: git and github mailboxes are consistent
Three, git definition:
Git is currently the most advanced distributed version control in the world
Working principle/process:
Fourth, the use of git
1. Create a repository:
1.1 Create the file first
1.2 Right-click the file and enter the command git init
1, 3 Effect: A .git file will be automatically generated in the file as the version library, indicating that the version library is created successfully
2. Add the file to the repository:
2.1 Create files in the repository
2, 2 Add the file to the temporary storage area git add wenjian.txt
2.3 Submit the remote library git commit -m "wenjian.txt has been submitted"
git status to view the temporary storage area files (no submitted files)
3. How to add the modified file to the repository
3.1 Modify the file and save it
3. 2git status The modified file is in the temporary storage area, but it is red, which means that the file has been modified to add the file
modified modified file
3. 3git diff wenjian.txt to see what has been modified in the file
3, 4 Add the modified one to the temporary storage area
git add wenjian.txt, the red file becomes green
Green indicates successful addition
3.5 Submit the modified files to the local repository
git commit -m "Remarks: file modification content"
There are no files to submit in the temporary storage area
4. How to view history, version rollback, and version rollback to restore the original version
4.1 Modify the file and save it
4.2 Add a temporary storage area and submit it to the local repository
4.3 View log information git log
View simple log information git log --pretty=oneline
4, 4 version rollback
git reset --hard HEAD^ go back to the previous version
git reset --hard HEAD^^ roll back to the last two versions
git reset --hard HEAD ~100 Roll back to the last 100 versions
Fall back to a fixed version number:
git reset --hard 4b77f511a1b77660619a8dec46a530a4bfad194
Note: right click copy paste
5. If the command window does not have this version information
So how to get this version information git reflog
The previous one is the version number, which can be rolled back to a specific version through the version number
What is the difference between a working area and a staging area:
work area
git add . Add all files at the same time
git commit -m "remarks" local repository
6. git undoes modification
Added problematic content, there are two ways to solve it
1. Delete directly, then add commit
2. Go back to the previous version
6, 1 just made a modification and has not been added to the temporary storage area git status
red wenjian.txt
git restore -- wenjian.txt
6. 2 has been added to the staging area git status green wenjian.txt
git restore -- wenjian.txt
The modification made after adding to the temporary storage area will be undone
6. 3git files that have been added and committed are manually deleted
1. Permanently delete files
git status will prompt that the file has been deleted
git commit is submitted directly, and the file is permanently deleted
2. Undo deletion, file recovery
git restore -- wenjian.txt
Five, the use of git combined with github
1. The remote warehouse connects to github
1.1 Register a github account
Email must be consistent
1. Configure SSH to connect to github under 2window, github ssh.key. Since the transmission between your local git warehouse and github warehouse is encrypted by ssh, you need to set:
C:\Users\mihxy find .ssh under the current user
There is no git installation for the first time
(1) Create ssh key. dos command
ssh-keygen -t rsa -C "[email protected]"
(2) github configuration
(3) Create a warehouse
Order:
or create a new repository on the command line with
echo "#banbenku" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/Lynn1234567/banbenku.git
git push - You originate main or push an existing repository from the command line
git remote add origin https://github.com/Lynn1234567/banbenku.git
git branch -M main
git push -u origin main
(4) Associate github
git remote add origin https://github.com/Lynn1234567/banbenku.git
(5) Submit the main branch to the remote library
2. If the remote library has new content, how to clone the content of the remote library to the local
2, 1 Association
git remote add origin https://github.com/Lynn1234567/banbenku.git
2, 2 clones
git clone https://github.com/Lynn1234567/banbenku.git
3. Branch
3.1 Create a branch
master main branch
git checkout -b dev Create and switch dev branch
git branch view all branches
git branch dev creates a dev branch
git checkout master switch to the main branch
cat wenjian.txt View the contents of the file
The meaning of the branch is two environments, and the contents of the files in the environments can be different
3.2 Merge branches
git merge dev merges the modified content under the dev branch into the master
Meaning: Merge the dev branch into the current branch
3.3 delete branch
git branch -d dev delete dev branch
3.4 Conflicts occur when merging branches:
Manually modify the conflicting parts of the file
4. Branch management
Generally speaking, deleting a branch will lose branch information, and we don't want to lose branch information
Create a dev branch:
Modify wenjian.txt content
Add to the staging area and submit to the repository git add . + git commit -m ""
Switch to the main branch (master) git checkout master
Merge the dev branch while disabling fast forward
Use git merge -no ff -m " comment " dev
Git branch -d dev deletes the dev branch, and the dev branch record still exists in the computer
view history
The deleted dev branch can be retrieved through the version number
5. Bug branch
Problems encountered in multi-person collaboration
Make temporary bug fixes on the branch
git stash hides the current fenzhi1
Hide the current worker thread
Switch to the main branch and create a temporary modification defect branch 2
Branch 2 modification completed add commit add commit
Switch to master branch to merge
switch to branch 1
git stash list find stash workspace
git stash pop restores the previously hidden working state
6. GitHub delete project
setting pull to the bottom, delete
Six, the main difference between git and svn:
Svn is a centralized version control system, and the version library is actually placed on the central server (requires networking)
git is a distributed version control system, so it does not have a central server (no networking required)