Linux how to quickly use GitHub

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_34258344/article/details/100625434

Foreword

Git is a distributed version control system open source, for quick and efficient handling of any small or large projects. This article describes common git commands, how to quickly share under Ubuntu using Git project management, did not talk much, just start with git.

1, ready to work

(1) Installation git

To use the words of GitHub, it must have a GitHub account, so the first step is to register an official website GitHub GitHub account, the time of registration will let you enter a user name, email, password, keep in mind all the information your GitHub account, because after configuration, log in and submit the code will be used. Then le, you need to install git tools in Linux is very simple, enter the following command in the terminal

sudo apt-get install git

Here Insert Picture Description
As shown above, all the way round, can be installed successfully, if you do not worry, you can also enter the following command

git --version

If you can git version information is displayed, indicating that you do the installation was successful.
Here Insert Picture Description

(2) the user configuration information

Git comes with a tool to help set git config configuration variables control the appearance and behavior of Git. These variables are stored in three different positions:

  • / Etc / gitconfig file: Contains common configuration for each user on the system and their warehouse. If you use git with --system option config, it will read and write configuration variables from this file.
  • ~ / .Gitconfig or ~ / .config / git / config file: only for the current user. Options can be passed --global make Git read and write to this file.
  • config files currently use Git repository directory (that is, .git / config): for the warehouse.

To the current user profile, for example, before using git it, we need to configure user information, such as user name and mailbox, use the following command

git config --global user.name "你的用户名"
git config --global user.email "你的邮箱"

View configuration information, be ordered as follows

git config --list

Here Insert Picture Description
User information has been set up, we can configure the default text editor, and when Git needs you will call it when entering information. If not configured, Git will use the operating system's default text editor, usually Vim. If you want to use a different text editor, such as Emacs, you can do this:

git config --global core.editor emacs

(3) Add SSH key

Since the transfers between the local storage and remote GitHub repository through SSH encryption, so we need to configure the authentication information, create SSH key, in order to more secure remote management of our code repository, only on GitHub website adds SSH key that we can successfully be in native code submitted to a remote warehouse, looking directly at the following:

cd ~/.ssh
ls

Here Insert Picture Description
Yes, that's id_rsa.pub file, like this long after the open
Here Insert Picture Description
copy it, paste it into the GitHub site here

Here Insert Picture DescriptionHere Insert Picture Description
Will be displayed after successfully added
Here Insert Picture Description
some students said, my .ssh folder no SSH key files Yeah, the problem still is not, enter the following command

ssh-keygen -t rsa -C "你的邮箱"

At this generated secret key, use the cat command to view, and then add to the GitHub website.

2, Warehouse Management

(1) Local Warehouse Management

Both methods create a local repository:

  1. We can create a new folder in the local mkdir, then push to Git remote repository
  2. Open from Git clone a remote repository project to others or their own local projects

Then execute the following command at the start of our project root directory repository:

git init

Creating a warehouse

#shortname是自己起的仓库名,url是GitHub地址
git remote add shortname url

Then add a new file

git add *

Submit to the warehouse

git commit -m "first update"

When we changed a lot of documents, and do not want each one add, I would like to automatically submit commit local modifications, we can use the -a logo.

git commit -a -m "add one file"
  • git commit 命令的-a选项可将所有被修改或者已删除的且已经被git管理的文档提交到仓库中,引号里的是描述信息,可根据自己代码的情况修改。
  • 当使用 git commit 进行提交操作时,Git 会先计算每一个子目录的校验和,然后在 Git 仓库中把这些校验和保存为树对象。 随后,Git 便会创建一个提交对象,它除了包含上面提到的那些信息外,还包含指向这个树对象(项目根目录)的指针。如此一来,Git 就可以在需要的时候重现此次保存的快照。

Here Insert Picture Description

(2)远程仓库管理

在GitHub上新建一个仓库,在这里开始创建。
Here Insert Picture Description
输入仓库名、描述信息、勾选公开仓库,创建README文件,点击Create repository创建仓库即可。
Here Insert Picture Description
创建成功后,我们GitHub上就有了一个远程空仓库啦,如下图。
Here Insert Picture Description

3、git基本操作

基本的 Git 工作流程如下:

  1. 在工作区修改文件(add rm等)
  2. 暂存文件,将文件的快照放入暂存区域(commit)
  3. 提交更新,找到暂存区域的文件,将快照永久性存储到 Git 远程仓库(push)

(1)克隆项目

克隆项目是指将远程仓库里别人的公开项目或自己仓库的项目下载到本地用如下命令。

#url指远程仓库地址,如git clone https://github.com/ljrkernel/linuxmooc
git clone url

(2)上传项目

上传项目是指将自己本地的项目同步到GitHub远程仓库用如下命令。

#url指远程仓库地址,后面是分支名
git push url master

Here Insert Picture Description
看一下远程仓库,我们的代码已经上传到GitHub远程仓库了。
Here Insert Picture Description

(3)删除文件

删除仓库中的文件用如下命令

git rm filename

(4)查看状态

查看在你上次提交之后是否有修改用如下命令

git status
#加-s参数可获得简短的结果

(5)查看改动

查看已写入缓存与已修改但尚未写入缓存的改动的区别

git diff
  • 尚未缓存的改动:git diff
  • 查看已缓存的改动: git diff --cached
  • 查看已缓存的与未缓存的所有改动:git diff HEAD
  • 显示摘要而非整个 diff:git diff --stat

4、分支管理

Git branch, in fact, is essentially just a pointer to a volatile submit object. The default branch name in Git is master. After submitting multiple times, you actually have a master branch that last point submitted object. It will automatically move forward in each of the commit operation.

(1) create a branch

In the local branch complete, fast, to create a new branch, we use the following command.

git branch test

(2) branch switching

branch command does not bring us into a branch, just create a new branch, if we want to enter a new test branch, use the following command.

git checkout test

(3) combining the branch

Changes to our other branches are not reflected in the main branch, if you want to commit the changes to the main branch, you need to switch back to the master branch, then merge.

git checkout master
git merge test

(4) deleted branches

If we want to delete the branch, use -d logo.

git branch -d test

Guess you like

Origin blog.csdn.net/qq_34258344/article/details/100625434