foreword
This is a small article about Git.
What is Git? Git is an open source distributed version control system for agile and efficient handling of any project, small or large.
Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development. Reference: Backstory about GIT development
The concept of GIT
There are 3 places where the code is stored:
- Workspace: The workspace is where you usually store the project code
- Index / Stage: Temporary storage area, used to temporarily store your changes, in fact it is just a file, save the information about to be submitted to the file list
- Repository: The warehouse area (or version library) is the place where data is safely stored. It contains the data you have submitted to all versions. Where HEAD points to the latest version put into the warehouse
- Remote: The remote warehouse, the server hosting the code, can be simply considered as a computer in your project team for remote data exchange
General code identification:
- Branch: Code branch, a historical path of code.
- Tag: code tag, which records the code of a specific version.
- commitId: Each version of the code has a unique 40-bit hash value
- HEAD: A pointer to the version of the current code.
Brainless daily operations:
git add
: Register the file in the git warehouse, and get an objectId for each file, so that it can be monitored by gitgit commit
: Submit all current file versions and generate a code version commitIdgit push
: Submit the current version to Remote, so that you can share the code with others
Remote GIT setup
Git warehouses are generally maintained on github or gitlab. Of course, since git itself belongs to distributed version management, it means that everyone will have all a warehouse, so some local files can also be controlled by git, but for the version control of large files, git will It is relatively difficult, which is mainly related to the local version control principle of git.
When initializing the github repository, there will be the following 2 texts:
…or create a new repository on the command line
echo "# demo" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin [email protected]:artikell/demo.git
git push -u origin main
…or push an existing repository from the command line
git remote add origin [email protected]:artikell/demo.git
git branch -M main
git push -u origin main
The difference between these two operations is: Does the warehouse already exist locally?
So it can be seen that the operation of creating a new warehouse is:
git init
git add README.md
git commit -m "first commit"
Of course, the common code of these two operations can be seen as follows:
git remote add origin [email protected]:artikell/demo.git
git push -u origin main
The meaning of this operation is to register the remote address : register the warehouse as a origin
remote link, and then push it to origin.
The original master was changed to main due to political correctness.
However, the registration remote in github itself also pays attention: HTTPS or SSH?
Brainless judgment is: lazy or not? If you don’t want to enter a password every time you push (of course https can also store passwords), then use SSH. But the public key of the SSH key pair is uploaded to the server.
And https can be used on public servers, so that two people can use their own accounts to submit to each other.
Simple GIT setup
Every submission is your own labor and you must bring your own name, so we need to tell git what our name is!
$ git config --global user.name "iamstarlee"
$ git config --global user.email "[email protected]"
Among them, --global
the meaning of is whether it is valid for the current user. If it is valid for the system --system
, it is valid for the warehouse --local
. In this way, different settings can be made for the submitter of the warehouse.
Then, in order to be lazy, we definitely need to set up SSH keys. So, we need to see, is there a secret key now? In a normal linux system, most of the configuration is ~/
under the user directory ( ), so we only need the cat command to check it.
cat ~/.ssh/id_rsa.pub
If an error is reported No such file or directory
, it means that it does not exist, and a secret key needs to be generated. Simple one command:
ssh-keygen -t rsa -C "[email protected]"
Then drive all the way back. Finally, use cat
the command query.
Then, copy the contents of the file to the KEY text box of https://github.com/settings/ssh/new .
Finally, if you are excellent, you can use ssh to submit code.
Of course, if the key has been used by other users, an exception will also occur.
GIT workflow
How can using git be more standard? This is about the branch control of git itself.
git branch <feature_name>
git checkout <feature_name>
git merge <feater_name>
The above operations are: create branch, switch branch, merge branch.
In the normal warehouse version, it can be divided into three types of branches:
- master main branch, maintaining normal and available branches
- feature function branch, a branch for developing each function
- hotfix repair branch, the branch that repairs the function.
The general development process is developed by developers in the feature branch, and finally merged into the main branch. When an online exception occurs, a new hotfix branch is created from the master, and the master branch is directly merged after the repair.
However, for the special master main branch version, it will be git tag
marked with , so that the code of the relevant version can be quickly located, which is also the basis for the release of many open source project versions.
Here, it will involve the difference view of the branch, which is git diff
the command. I can talk about this when I have time.
Everyday GIT Questions
- What should I do if I do not want to submit some documents?
Answer: Add a new.gitignore
file under the root directory, and then write the file or folder you don't like. It's like drawing a circle and cursing it. git add
What should I do if I don’t want to submit it anymore?
Answer:git reset HEAD <file>
, usegit reset
the command to remove the file from the temporary storage area.git commit
Yes, but the submission is wrong, what should I do?
Answer: It is stillgit reset
an order.git reset --soft HEAD^
, which--soft
means not to undoadd
the operation, but if you want to undo all the way, then use itgit reset --hard HEAD^
.HEAD^
Indicates the previous version of the current version, and how many of them^
indicate the previous version.- If they are all pushed to the remote warehouse. What can I do then?
Answer 1: The best but least usable method is to roll back to the corresponding versiongit push -f
first , and then force the current master to be submitted to the remote.git reset
(However, this operation will cause the loss of the version, so it is generally not used)
Answer 2: You can only roll back the code manually, and then submit a new version.