This article mainly introduces the basic usage of git in windows environment and some introduction to github. Take two IDEs-as and vs as examples to introduce how to carry out version control in the process of writing programs.
Git is an open source version control tool, its developer is Linus Torvalds. The original intention of git development is to better manage the Linux kernel, and it is now used in team development. The most important problem of team development is code synchronization. Git can help us solve this problem very well. Github is a code hosting website and currently only supports git hosting projects. Github is also a good communication community, where you can find many excellent open source projects for learning.
Git
Download and installation
Because git is open source, anyone can download it for free. Download address After
installation, open the command line and enter git. If you get an output similar to this, it means you have successfully installed it. Right-click on the desktop and you will find that the first one is opened with a graphical interface, and the second one is opened with a command window. Here we only introduce the simple operation of the command line.
Create a code repository
Open the Git Bash just now, first you will be asked to configure your identity, the command is as follows
git config --global user.name "your name"
git config --global user.email "yourname@mail,com"
Then type
git config --global user.name
If you can output your name, the setup is successful.
Next we start to create the warehouse. The warehouse is to save our version, all submitted codes will be saved in the warehouse for our subsequent operations. We create a new folder, then right-click on the blank space of the folder to open git bash and enter git init
. After the operation is completed, we will see a hidden folder named .git in the folder. This is the warehouse we created.
Submit code
Submit code is to submit the code we have written to the warehouse. There are two commands: add and commit. Add is to add the code first but not save, and commit is to save the added code. In other words, only add A first, and then commit A to complete a submission. We can create a new notepad file named t.txt and write something in it. Then open bash in the blank space, type git add t.text
and type again and git commit -m "firstcommit"
you are done! Note here: we must use the -m parameter to add description information, otherwise it is illegal. The content in double quotes is the description information.
If you want to add folders git add 文件夹名
, add all. git add .
At this time, there is a (master) after the folder, which is the current branch. You can create multiple branches and switch.
Ignore files
Need to consider when submitting code, do some files need to be submitted? For example, some files are for our own use and do not want to add. The .gitignore file solves this problem for us: it will exclude files added to this file from version control. But now we don't have this file in our folder, because this file needs to be created by ourselves. Create a new blank file and name it .gitignore (it can only be this name!), just write the file name in .gitignore if you want to ignore any file. If it is a file in another folder, you need to add the path of the file, such as: /folder/file
.
View the content The
above is how to submit the content, then how to view it after submission? Use status naming.
Input git status
will get information similar to the above picture. Now we modify the text of t.txt and check it after submitting it again.
You will get a result similar to the figure above. It can be seen that git already knows our modification.
Then we use to git diff
view the modified content.
-Represents the deleted part, + represents the added part.
What if I regret after submitting it? There are also git checkout
ways: . Now I modify the text of t.txt, and then enter the checkout command to view.
Now open the t.txt file and find that the modified content has become the last content. But checkout is only used for files that have not been added. If I add after the modification, I cannot return with checkout. After checking with status, I found that it failed.
Open the t.txt file for verification and found that there is indeed no rollback.
You need to use git reset
commands at this time . Now we enter git reset, and then use the status command to check and find that it is back to the unadded state.
Check the record.
Use git log
to check how many commits have been made. Now we make a few more changes and submit it. Then look at the effect:
The yellow commit is followed by the commit id, which is unique. Author is the name of the author. Date is the date of submission.
Well, that's it for the brief introduction to git. If you think what I wrote is too simple or wrong, you can also go directly to the git download address to find the document for learning.
GitHub
Registration and login
This is the official website github of github . First register an account of your own, and then enter your homepage.
You can find many interesting projects in the explore, and these projects are all open source, you can download and learn by yourself. Now we find a project.
Watch is to follow, star is to collect items, and fork is to copy items to your own item list and modify them.
Click clone or download to download the project
The copied URL can be opened directly in some IDE integrated with Github. If you don’t need to open it, download the zip package and unzip it to get the project.
You can also set personal information in the right .
Create a project
Click on the upper left to enter the homepage.
Click NEW and after
we have entered the name of the project, select the public version (so that everyone can see your project, private is a private version but requires a fee), and then complete the creation.
Next we will clone the remote version to the local.
Click Copy on the right, select a folder to open bash, and then enter git clone
and paste the url just copied.
Obtaining a picture similar to the above shows that the clone has been successful. Then we copy all the files in the newly produced folder (don't forget the .git folder) and delete them.
Then add, commit (don't forget -m "msg"), git push origin master
you can push to the remote warehouse. Origin master is the original branch. Now open the github project and find that the file has been added successfully.
At this time, you will be asked to enter your username and password, just enter them as required. Now we are done!
Use of git in Android studio
Android studio is built-in git, no need to install plug-ins. Git in as is not as cumbersome as using git bash directly, let's take a look.
Add git and registration
First, use alt+ctrl+s in as to open the settings page and find git, and then add the location of the git you downloaded on the right. Click Test. If it appears, it means that the configuration is successful. Next , add your own github account, so that you can link to the remote warehouse.
Create a warehouse and add it to
create a warehouse without entering commands. We find VCS in the menu bar and open it. Then click Create Git Repository..., and then select the folder. as will automatically help us create warehouses and ignore files. Now I find that the project file on the left has turned red, which means that this file has not been added to the warehouse.
Now we right-click this project to find Git and click add.
Then we found that the red file has turned green.
Now that all files have been successfully added, how to submit it next?
Find VCS and click commit,
then add the description information, click commit. An error may be prompted after commit. This is because the project itself may have a warning or the program may not be able to run because the erro has not been resolved. Here we can submit it directly.
View version status
Press alt+9 or directly open version control in the status bar below, in this panel we can view some version control information.
You can switch branches in the lower right corner. The current default is the master branch, and you can choose to add a new branch. Using branches can better help us version control the project. For example, in team development, each creates its own branch, so that the code of the master branch will not be affected due to operational errors. Of course, Git flow is usually used in actual development. If you are interested, you can refer to the introduction of git flow .
Clone project
If we find a good project on Github, how can we clone it? It's very simple. First, copy the url of the project, then click test after pasting it, and clone it after success. Of course, we can also clone the project directly when starting As.
Then paste the url.
Push and rollback
It is also very simple to push your own project to github.
Then enter your user name and password, enter the project name, and you can see the project on github's personal homepage.
So how to checkout? You can click reset in git, or you can do this:
find the submitted hash value and copy, and then paste it into check out. Click ok, smart check out is fine.
The use of git in Visual studio
Install the plugin
. To use git in vs, first install the git plugin
After the installation is complete, configure your own github account, and then connect to github.
Clone and push After the
connection is successful, enter this interface,
you can click clone to clone or create your own project to github.
Vs about how to use github can refer to this article . In addition, the built-in code hosting warehouse can also be used in VS, and you need to log in to your micorsoft account and perform the corresponding operations.