Introduction to Git
Git is a distributed version control software that must be used in development. This is its official website Git .
We can find its documentation on the official website, which is very detailed and easy to use.
Download and install
The official website documentation is very detailed, which contains the installation steps of weindows, Linux, and mac. Here is an example of Windows:
After the download is complete, just click the next step. After the installation is complete, right-click the mouse on the desktop. If the following two selection instructions appear, the installation is complete
Local warehouse
Since Git is a distributed version control software, the first thing we have to learn is how to submit the code locally and then to the remote code base.
Version submission
In Git, the first thing we need to do is to manage a project.
Enter the project folder and execute the initialization command to allow Git to take over the version control of the current project. As follows
Then right click, select Git Bash Here and enter the terminal, let Git take over the MyProject folder.
Then enter git init (turn this directory into a warehouse that can be managed by Git). When the command takes effect, a hidden folder of .git will be generated, as shown below
Then we can use git status to check the status of the files being managed in the current folder.
At this time, the newly added files and the modified files are all unmanaged files and are shown in red, as shown above.
At this time, we need to manage the file, use the add command to tell git to add the file to the warehouse.
- git add folder name# manage only one file
- git add. # Represents all files in the current directory
When we manage and then use git status to check the status, it turns green, and then use git commot to tell Git to submit the file to the warehouse, as shown below:
Here Git tells you that it is abnormal again, let you configure user information, enter account password and other information. We only need to configure the personal information according to the prompts. After configuration, submit it again as shown below:
At this point, we have completely submitted our files to the remote warehouse. You can use git log to check the generated version.
Let's summarize the commands we just used:
command | description |
---|---|
git init | Initialize git, let Git take over the current folder |
git status | View the current file status, newly added or modified files are all red, and green after the add command is executed |
git add [./filename] | Submit the file |
git commit -m "Describe the message" | Generate a version |
git log | View version information. |
Version rollback
Roll back from high version to low version
-
- First use git log to view the commit log
- Then use git reset --hard version number to roll back
The same can also be rolled back from a low version to a high version
-
- First of all, you can't use git log but use git reflog to view the log
- Then use git reset --hard version number to roll back
As usual, let’s summarize
command | description |
---|---|
git log | View version submission information, used for high version rollback low version |
go reflog | View version submission information, used for lower version to roll back higher version |
git reset --hard version number | Perform version rollback |
Partition concept
In the above operation, we can find that a file has multiple states:
- In control state: refers to the state in which the file can be managed by git. At present, all files located in the git init folder are in control state
- Change status: when there is a new file, or the original file is modified, use git status to show the file in red
- Temporary area status: refers to the file whose red status changes to green status after the git add command is executed
- Repository status: refers to the version submitted to the version after executing the git commit command
These different states correspond to different partitions. Since what we are doing is local version control at this time, the version library here is the local version library.
Use branch
Branching is a very important concept in Git. It allows you to deal with more emergencies and allows you to better develop collaborative operations. Let you in development, multiple branches do not affect each other, there are official versions, and development versions, etc., as shown below
In this way, the committed versions we see in different branches are not the same. When a new branch is created in a branch, the sub-branch will inherit all the files in the current branch node, and there is a master branch by default.
Common commands are as follows
command | description |
---|---|
git branch | View all branches of the current project (* before the branch indicates the current branch) |
git branch branch name | Create a new branch |
git checkout branch name | Switch branch |
git merge the branch to be merged | Merge branches (switch branches first) |
git branch -d branch name | Delete branch |
Merge conflict
Due to branch conflicts, multiple branches may modify the same code. At this time, there is a merge conflict problem. Since Git does not know who is listening, you will keep them all and you can modify them manually.
Remote push
The above is very lively, but they are all based on local warehouses. When they are actually used, they have to cooperate with other people. This is also a big advantage of Git.
Git is a distributed version control software. There are many remote version libraries to choose from. Here is our most commonly used GItHub.
The first is to register a user, so I won’t demonstrate it here.
Create warehouse
When we log in to GitHub, the first thing we need to do is to find a remote repository. First open the warehouse page now,
Then click NEW to create a new warehouse, as shown below
After the creation is complete, you will see the following page
So far our warehouse is created, and it can be used later
Push code
Follow the above prompts to start pushing the code to GitHub
git remote add origin https://https://github.com/iszhonghu/Test.git
git push -u origin master
Then you can see that our file was successfully uploaded
This is just the master branch uploaded. If you want to upload the dev branch, you still need to enter the push command
git push -u origin dev
to sum up:
command | description |
---|---|
git remote add alias warehouse address | Give the warehouse an alias |
git push -u origin branch name | Push branch to remote warehouse |
have to be aware of is:
When pushing code, you need to push one by one branch
Pull code
Now that you can push the code, you can also pull the code to facilitate the realization of collaborative office.
First, create a folder to save the code, and then just pull it directly,
command | description |
---|---|
git clone remote warehouse address | Clone all the codes of the remote warehouse (only for the first time) |
git pull origin branch name | Incrementally update the code in a branch |
Partition concept
Because it involves a remote warehouse, there is one more partition, as shown below
The pull using incremental update can actually be split into two commands
- The git fetch origin branch pulls the branch code in the remote repository to the local repository
- git marge origin branch merges the code in the local repository into the local branch
Other operations
Configuration file
The Git configuration file has three parts:
Warehouse-level configuration file: In the warehouse's .git/.gitconfig, the configuration file is only valid for the warehouse where it is located.
-
- local
Global configuration file: Mac system is in ~/.gitconfig, Windows system is in C:\Users<user name>.gitconfig.
-
- global
System-level configuration file: gitconfig in the etc folder of the Git installation directory (the installation directory under Mac system is /usr/local/git).
-
- system
SSH
Use SSH to push and pull code
Generate public key and secret key
-
- ssh-keygen
-
-
- The default is placed in the /.ssh directory, where id_rsa.pub is the public key and id_rsa is the private key
-
to sum up
Here are some commands in this article:
command | description |
---|---|
git init | initialization |
git status | Check status |
git add | Manage specified files |
git commit -m "description" | Build version |
git log | View version history (previously) |
go reflog | View version history (after) |
git reset --hard version number | Version rollback |
git branch | View all branches |
git branch branch name | Create new branch |
git checkout branch name | Switch branch |
go merge | Branch merge |
git branch -d branch name | Delete branch |
git remote add alias address | Alias for remote warehouses |
git push -u alias branch | Push a branch to a remote warehouse |
git clone address | Clone remote warehouse code |
git pull alias branch | Pull the code of a branch in the remote warehouse and update incrementally |
git rebase -i HEAD~number of entries | Version merge |
At last
- If you feel that you are rewarded after reading it, I hope to pay attention to it. By the way, give me a thumbs up. This will be the biggest motivation for my update. Thank you for your support.
- Welcome everyone to pay attention to my public account [Java Fox], focusing on the basic knowledge of java and computer, I promise to let you get something after reading it, if you don’t believe me, hit me
- Seek one-click triple connection: like, forward, and watching.
- If you have different opinions or suggestions after reading, please comment and share with us. Thank you for your support and love.
——I am Chuhu, and I love programming as much as you.
Welcome to follow the public account "Java Fox" for the latest news