Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Git/Github

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use Git/Github". In daily operation, I believe many people have doubts about how to use Git/Github. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Git/Github"! Next, please follow the editor to study!

Preface

Git is an open source distributed version control system for agile and efficient handling of any small or large project. Git is an open source version control software developed by Linus Torvalds to help manage Linux kernel development. Different from the commonly used version control tools such as CVS and Subversion, Git adopts the way of distributed version library and does not need to be supported by server-side software.

Download and install

Installation in 1.Windows/Mac OS version download 2.Linux

Yum install git (some Linux systems come with git)

The first experience of Git

Through the study in the previous section, we have basically known what git is, as well as the download and installation of git.

We also want to know exactly how git does version control.

The requirement is: for example, there is a text file, want to manipulate this text file. I keep changing the contents of this file, and after each change, I need to record the history of the changes.

Before using git for version management, you need to make a configuration that tells git your user name and your email account

Tip: when you forget how to write the command, you can use the Tab key to prompt.

Git preschool knowledge

You might as well draw a picture first

A workspace is a directory you are currently using, such as the "gupaogit" folder

The version library is the mess after add and commit.

To use git, steps:

Download and install

Git config-global user.email/name

Git init initializes a folder directory with git, which is to allow git to version the files in that folder.

After initialization, there will be a .git folder in the current directory, which is what we call a version library.

Now you can use git to control a version of the file under it, that is, you can roll back a version of it, go back to the current, and so on.

The next thing you do is

Nothing more than creating a file in this workspace or warehouse, and then making a series of changes to the file and so on.

The requirement of Git is that if you want git to manage your version, you must first have a folder, which must be initialized by git, so that you can do a git version management.

Basic operation of Git

Git status git add git commit requirements: create a new file named "gupao.txt" in the git workspace.

Git status: means to view a status in the git workspace (warehouse)

(1) nothing to commit

(2) when you create a new file, such as 1.txt Untracked files: untracked file

(3) when we carry out git add 1.txt

(4) when we conduct git commit-m 1.txt

Only after entering the submission area can our documents be managed by git in a real sense.

(5) corresponding to the state when we create a new file

Git add: add the contents of the workspace to the staging area

Git commit: commit the contents of the staging area to the submission area

In order for git to version the contents of your file, you must go through two steps, the first is git add, and the second is git commit. You can use git status anytime, anywhere to view the status in the workspace

Git log and version switching

Everyone knows that git can be versioned and managed. If you want to switch versions, all you need to know is this commitid, that is, you need to know the information submitted each time, you have to check what the commitid is each time.

Git log log, that is, to view the log information of git

Commit Author: the author is that you configured git global-user.name Date: the date of submission will be switched from one version to another based on each commitid. Git reset-- hard commitid (this id can be found by viewing log information based on git log)

I think it's too much to print out the log information like this. Can you simplify it?

Git log-pretty=oneline

Additional knowledge: I think I just want to go back to the previous version and changed something today.

Directly back to the previous version git reset-- hard head ^ back to the previous version Git reset-- hard head ^ ^ back to the previous version

The trouble is that after reversing to the previous version, the latest commitid is gone. If your previous operation window is available, you can copy it and proceed with it.

The point is to find the latest version of commitid. Git reflog

If you do a version fallback and find that the latest commitid is gone, you can use git reflog for all commitid queries. The   8 workspace staging area and commit area have been modified in the workspace, and then git status will have a hint of what you need to do next

In normal thinking, you will git add regret the current operation, git checkout-gupao.txt will undo a change in the workspace, which means you do not need to do the git add operation.

For content that is already in the staging area, it is not feasible to use git checkout-gupao.txt to return git reset HEAD gupao.txt to the latest version in the submission area

The next thing you need to do is to use git checkout-- gupao.txt again, pull the changes back to the workspace, and empty the changes in the workspace to the initial branch of   9.

Connect each submission with a line, and you will find that it is very consistent.

The concept of the pointer + Cpicurus

Git reset-hard commitid HEAD if the content has been add to the temporary storage area, if you want to undo it at this time, you need to go back to the latest commitid, HEAD head ^ head ^ HEAD, which means the latest version of commitid, that is, the latest pointer to

The Master branch is the main branch and the branch that git creates by default for you.

There must be other branches.

The Master branch points to the latest commit commitid, and the header points to our master branch. But the power of git is much more than that, but there is a concept of multi-person collaborative development. Not only a single developer for version control, but also a multi-person collaborative development for version control. It is done through the branch of git.   10 detail branch cuts out a development branch on the main branch master, such as creating a branch called "dev_wang" git checkout-b dev_wang;

Now all your operations are done on a new branch.

Just now a series of operations have been done on the dev_wang branch. Now you can switch to the main branch Git branch to view all the current branches, and you can see the current branch (*)

Versioning on different branches takes effect only on its own branches.

A supplement to HEAD: the HEAD header pointer points to the latest commitid of the current branch

Suppose we think a branch is useless, what if we want to delete it?

The branch you are now in is dev_wang, and if you want to delete it, you must be on another branch. At this point, you can only delete the branch git branch-D dev_wang forcefully.

  11 branch merge and conflict resolution main branch master we will not move, will only be used for version release. After each has completed the development on its own branch, it is necessary to merge the developed content into the main branch. Become merge at this time. Requirements: a new developer dev_zhang (1) git checkout-b dev_zhang; (2) modifies the file and add,commit (3) has a commitid on the branch of Xiao Zhang at this time, at this time, the modified content of Xiao Zhang needs to be released, and the modified content of Xiao Zhang needs to be merged into the master branch. Switch to master branch, merge dev_zhang 's development content merge operation: quickly merge on master branch, git merge dev_zhang

(4) git branch-d dev_zhang; / / when we did this in the previous section, it had a hint that it could not be deleted, which told you that you needed to merge branches.

When you have a lot of developers, 2, Xiao Zhang, Xiao Li, cooperate to develop a project requirement: Xiao Zhang and Xiao Li work together to develop a project, in which two people are responsible for different modules Xiao Zhang: commodity management module Xiao Li: the master branch of order management module is released as a version branch, should not be developed directly on it (1) git checkout-b dev_zhang (2) git checkout-b dev_li

(3) after developing on the branches of Xiao Zhang and Xiao Li respectively, it is found that there is no development content of Xiao Zhang and Xiao Li on the master branch. (4) the development content of Xiao Zhang and Xiao Li is released to the merger of the master branch.

The problem of merged conflicts

You need to resolve the conflict manually and then do the add,commit operation. Git checkout-b test

12 Git config and configuration aliases when you first installed git, the teacher asked you to configure an email name. Only in this way can you use the git git config command git config-l to configure git to view all the configuration information

All the information here is actually integrated. Which side is it? What is the priority of warehouse level, global level, and system level

The so-called repository level configuration: the current repository level under the .git > config file global level: the current user represents the global level system level: in our git installation directory etc Git config-- global-- add user.name itcrazy2016 Git config-- global-- unset user.name this command is actually more practical Git status git add git commit Git log-pretty=oneline these commands are too long, can you simplify? Git st means to view user status Git cm submit commit Git log one to view one line of information? Configure aliases? Git config-- global alias.st status stands for status with st.   13 tagging and ignoring files tagging: Commitid is not easy to remember. Can you remember each version 118.222.113.253 in a unique way so that it is convenient to visit the website? Is it convenient for www.baidu.com domain name to access the website? Commitid as2731aa tag wants to tag the latest version of id-git tag v1 will correspond to the latest version of commitid with v1 to check the list of tags in the current warehouse. Git tag tags commitid that has been missed before. How to do this? Git tag v1pre d619d86 tag you still don't look good enough, you can also add a description to this tag Git tag v2.0-m "here is a tag" to be able to delete the tag git tag-d v1

Ignore the file to create a Person.class class file, found that git marked as Untracked file untracked file Git add Person.class must be possible. I hope this file can not be managed by git. Can git ignore this file (this kind of file)? Class git, you don't have to help me manage creating a .gitignore file in the root of the .class class file repository. This rule is for git to understand.

Tell git not to manage this file for you.

14 Local and remote repositories assume that the main branch master already exists, so how do you give him a branch when there is an extra developer? Should everyone develop at least on their own computer? This master branch is only used for release of the version. Use a common computer as a remote warehouse for every developer to use, each developer can create their own independent branch for development, after the development is completed, push their own code to the remote warehouse. Local area network: a remote warehouse private public warehouse project: very small, want to involve all developers, then there should be such a public remote warehouse that everyone can access. At the same time, the advantage of this is that it is more convenient to manage the open source code. The warehouse that everyone uses together.

Obviously, GitHub remote repositories abroad can be used by developers all over the world to manage their own codes, cloud domestic remote repositories.

The problem is that you put the code on them, and the code you submit to your account will be made public unless you pay some membership fee so that the code can't be seen by others. What should you do if you build your own private warehouse and make it invisible to others? Gitlab

  15 GitHub and Ciyun Github remote warehouse Gupaogit local warehouse

Upload the local warehouse gupaogit to github. (1) create a corresponding project in the remote warehouse github, such as gupaogit repository warehouse

16 operate remote and local warehouse push,pull and clone. Git fetch: it is equivalent to getting the latest version remotely to the local, without automatically pushing the content of the merge code to a specific remote repository. The local gupaogit-> github/ cloud gupaogit (1) should make the two related to configure the relationship between it and the remote warehouse in the local warehouse. They are a family. Is there a remote warehouse in the current local warehouse, and if so, what is its remote warehouse? In the git remote local warehouse: the address of the git remote add origin remote warehouse? An association is made between the address of the git@github.com:itcrazy2016/gupaogit.git origin local warehouse and the remote warehouse

Git push-u origin master is the push code, so it does not specify a remote repository. After this association, the next step is to push the code to the remote warehouse

(2) after the local warehouse is associated with the remote warehouse, git push-u origin master pushes the contents of the local warehouse to the remote warehouse.

Whether it is a push or pull code, you need to have such a permission, that is to say, whether the computer you are currently using is secure or not. If it is not secure, people will not let you push it.

[if there is no permission] you need to add permission A. Need to generate a ssh key ssh-keygen-t rsa-C "itcrazy2016@163.com" locally. Add a secure ssh key to your calculation with a chapter, which means that you are the certified ssh_key B. You need to send this key to github/ Cloud to put the public key in ssh key.

Ensure a security of data transmission

If it is a modification made in other branches, you need to merge one branch to push other branches. Specify the branch to be pushed and you can git push-u origin dev_zhang.

A new buddy, Xiao Wang, needs to carry out a development and needs to pull the code of the remote warehouse to develop locally. (1) the premise of clone cloning operation is that sshkey is also added to complete Git clone git@github.com:itcrazy2016/gupaogit.git.

If the version content of the remote warehouse is inconsistent with that of the local warehouse, you need to git pull first, drop the content of the remote warehouse to the latest version, and then resolve the conflict manually.

At this point, the study on "how to use Git/Github" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report