Simple Guide for Git&GitHub

From MIT Technology Roadmapping
Jump to navigation Jump to search

If this tutorial helps you, please copy the following content to the end of your page to add this tutorial to the reference link on the page so that more people can see it, thank you!

[[Simple Guide for Git&GitHub]]

The first draft of this tutorial is written for students of the 2018 industry frontier course. It was translated by Google Translation. If you find problems, omissions or incomprehensible areas, please feel free to modify or Contact me.

Git

Git is an open source, cross-platform, distributed version control system for agile and efficient processing of any small or large project. In this course, file sharing and online storage are easy.

Git installation

Download Git (with Windows/MacOS/Linux version) from official website download page

Download Git desktop version from [1] (graphical interface version, not recommended)

On Ubuntu systems, you can install directly with the following command

Install with yum on Fedora:

$ yum install git-core

Install with apt-get on a Debian system such as Ubuntu:

$ sudo apt-get install git

Git installed configuration

Configure environment variables, students using cmd or powershell need to do this step, students who do not need the command line can skip

How to configure [2]

Add in the Path variable: C:\Program Files\Git\cmd


Git basic operation

This tutorial only covers the Git operations necessary for file sharing, including creating a repository, adding files, submitting versions, viewing versions, rolling back versions, and involving as few command-line operations as possible. For other operations such as branches and compound commands, please see A complete tutorial online.

Create an empty warehouse

Create a folder in the appropriate location that will serve as a repository for future file storage. Enter this folder, open the Git command line (right click on the mouse in the blank space under Windows -> click Git Bash, direct Ctrl+Alt+T under Linux to open the command line)

Enter the Create Empty Warehouse command (the $ symbol is automatically displayed under the command line interface, no manual input, it will be used to distinguish between commands and general text)

$ git init

This completes the establishment of the empty repository, and you can see a .git folder in the folder (if it is not visible, it will be hidden), it will be used to store some information needed by Git, not delete.

add files

In order to add files to the repository, you first need to edit the files you need to add to the repository under the repository folder (or copy them from somewhere else). Then use the git add command, followed by the relative path of the file, the command format is as follows

$ git add directory/to/file/FileName

For example, if you directly write a description document readme.md in the repository folder, use the command.

$ git add readme.md

This command adds the file to Git's buffer so that all changes are saved and a new version is generated.

If you add a lot of files at once, you can use the command

$ git add -A

or

$ git add .

To add all new or changed files into the buffer.

Submit version

After adding the file, this file will be in the Git buffer, not actually saved to the repository, and become a version. To save your changes and generate a new version, use the command

$ git commit -m "Message"

The -m is followed by a description of the version, which can be changed at will. If you don't lose the following -m "Message", Git will automatically pop up an editor asking the user to fill in the description text.

Undo add and commit operations

If git add adds the wrong file, or git commit misses the file, you can use

$ git reset HEAD [directory/to/file/FileName]

To undo the git add operation, and

$ git commit --amend

To overwrite the last git commit operation.

These operations involve many more complex commands and Git mechanisms, which are not explained in detail here. Of course, the easiest and most practical way is to make sure to re-add and commit once after correcting all the files.

View version information

Use command

$ git log

You can see the history, including the version number, the modifier, the modification time, and the description text that was filled in when commit. This command will display information about the last 3 commits.

Version rollback

If you find that you accidentally deleted an important file after commit, and you can't recover it on your computer, you can use Git to fall back to an earlier version and use the command.

$ git reset --hard HEAD^

You can roll back 1 version, which is the state before the last commit. If you want to roll back multiple versions, for example 3, you can change it to

$ git reset --hard HEAD^^^

Use 3 ^ symbols, or you can write

$ git reset --hard HEAD~3

Or you can fall back to the specified version number, and if you find that you have turned back too far, you can use this method to recover. Command is

$ git reset --hard xxxx

Where xxxx is the version number of the target version that git log can find (a very long hexadecimal number). You don't have to fill it in. Fill in the first few bits to make Git recognize the corresponding unique version.

Other better tutorials from the web

Since this tutorial is written for Chinese students, some resources are Chinese websites. If you have some better replacement, feel free to add it.

Git official documentation

Git Tutorial - Liao Xuefeng's official website

Git Five Minutes Tutorial

Students who are not used to the command line mode can also use GitGUI usage

Github

Github

Github is a file hosting platform that can be linked to Git. It also adds a lot of statistical analysis and social related functions in addition to basic functions such as file uploading, version control, and cooperative development of file management. Github is also a large open source community with a wealth of open source software resources.

The following content will be developed around the function of file sharing. For other functions, please refer to other tutorials.

New warehouse

Log in to Github, click New repository in the repository section, or click the "+" symbol in the upper right corner, click New repository in the drop-down menu, fill in the name and description of the repository, and click the Create repository button below to create a new repository.

After creating a new warehouse, the Github website will display a description of how to create a new warehouse, add a readme file, commit and push the repository to Github. Follow this step.

If you have already created a repository locally, go to the repository directory and open the command line, enter the following command

$ git remote add origin https://github.com/YourUserName/YourRepositoryName.git
$ git push -u origin master

You can push the current local repository to Github and refresh the page to see the files that have just been pushed on Github. Where YourUserName is your username and YourRepositoryName is the name of the new repository you just created on Github, which can be modified at will.

Download Warehouse

If you want to download all the files in a repository from someone's Github, use the following command in the local Git command line.

$ git clone https://github.com/UserName/RepositoryName.git

A new folder with the same name as the repository (that is, RepositoryName) will be created in the current directory. It contains all the files in the repository, and it will come with a .git folder, which stores the previous version information.

Of course, you don't have to remember the user name and the warehouse name. Open the corresponding homepage of the warehouse on the Github website. There will be a green Clone or download button in the upper right corner. Click to display the link required after git clone. Choose to download the zip archive containing all the files directly, but this zip file does not have a .git folder.

Cooperative development

There are a variety of ways to co-develop in Github, as well as some troublesome permissions issues. Because the industry's cutting-edge courses mainly use the function of file sharing, the following content will introduce the partner mode that basically does not require permission control and is most convenient to use (of course, the risk is also the biggest).

Before co-development, you need to make sure that every student on the team is reliable and will not maliciously destroy the warehouse. At the same time, you should be cautious in your operation. After confirming the correctness, you can operate the warehouse on Github.

Add collaborator

This step will be done by the creator of the warehouse. After entering the warehouse homepage, click Settings -> Collaborators, enter the username of the collaborator you want to add in the text box below, and click the Add collaborator button on the right to send the invitation to the user. Once the user agrees, they can become partners.

Receive partner invitation

The received collaborator invitation will be displayed on the message page (new message will appear in the upper right corner), and the invitee can choose to accept or reject.

If you do not receive the message or accidentally delete it, the creator of the warehouse can provide an invitation link (in the Settings -> Collaborators interface, the partner list on the right will show the users who have not accepted the invitation, select Copy invite link to view And copy the invitation link)

Sync file information

Each member's local repository needs to be as synchronized as possible with the remote repository on Github. Use commands when there is a change in file information in the remote repository

$ git fetch

or

$ git pull

It is possible to pull the updated portion of the remote server to the local repository.

If you make changes to the files in your local repository and want to commit them to the remote repository, you need to commit and then use the command.

$ git push origin master

It is possible to push files from the current local repository to the remote repository. This operation requires two premises

  1. You have write access to the remote repository, that is, you must first become Collaborator
  2. Before you push, you must clone or pull or fetch to get the latest repository content. That is, if you clone, before the push, other members push earlier than you, your push operation will fail. In this case, you need to clone to get the latest file, and then try to push.

By repeating the above operations, you can keep the remote and local warehouses up to date.

github function that needs to be used with caution

Indicates that the current code base of your own machine is pushed to the far end and overwritten.

$ git push -f

Merging local warehouses and workspaces (especially for students using the GUI version)

$ git merge

Github's other popular features

On the Github repository's home page, there are a series of tabs at the top, such as Code, Pull Requests, and more. The following sections briefly describe the features of some common tabs.

  1. Code, shows all the files, for some text files you can also view the content directly on Github. If there is a readme.md file in the root directory of the repository, it will be automatically displayed in the Markdown format on the front page of the Code.
  2. Pull Requests, is a way to merge files and update the warehouse, and manage the permissions more strictly. If you only use the collaborator mode, you do not need to use this feature.
  3. Wiki, you can write a wiki for the repository
  4. Insights, some statistics, including participating users, frequency of push, branching, etc., very detailed
  5. Settings, set up the collaborators, branches, etc. of the warehouse.

Set

Other good Github tutorials

Most of the more comprehensive Git tutorials have Github content. Here are some of the things for Github:

Github Concise Tutorial

Recommended: Github Pages

Github Pages

You can build your own website based on the Github warehouse. Each account/organization can build one. Don't miss the students who are interested in website operation and front-end design.