Facts about Git

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

insert image description here

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.

insert image description here

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 git
  • git commit: Submit all current file versions and generate a code version commitId
  • git push: Submit the current version to Remote, so that you can share the code with others

insert image description here

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 originremote 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?

insert image description here

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, --globalthe 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 catthe command query.
Then, copy the contents of the file to the KEY text box of https://github.com/settings/ssh/new .
[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-EdUlsZLw-1634545944877) (evernotecid://390F6B35-6AB1-4223-A0D2-878BAD026AC8/appyinxiangcom/37642702/ENResource /p22)]

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:

  1. master main branch, maintaining normal and available branches
  2. feature function branch, a branch for developing each function
  3. hotfix repair branch, the branch that repairs the function.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-HvcZkG30-1634545944878) (evernotecid://390F6B35-6AB1-4223-A0D2-878BAD026AC8/appyinxiangcom/37642702/ENResource /p23)]

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 tagmarked 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 diffthe command. I can talk about this when I have time.

Everyday GIT Questions

  1. What should I do if I do not want to submit some documents?
    Answer: Add a new .gitignorefile under the root directory, and then write the file or folder you don't like. It's like drawing a circle and cursing it.
  2. git addWhat should I do if I don’t want to submit it anymore?
    Answer: git reset HEAD <file>, use git resetthe command to remove the file from the temporary storage area.
  3. git commitYes, but the submission is wrong, what should I do?
    Answer: It is still git resetan order. git reset --soft HEAD^, which --softmeans not to undo addthe operation, but if you want to undo all the way, then use it git reset --hard HEAD^. HEAD^Indicates the previous version of the current version, and how many of them ^indicate the previous version.
  4. 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 version git push -ffirst , 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.

Guess you like

Origin blog.csdn.net/qq_41731507/article/details/120829169