Getting Started with Git and GitHub

Today, let's take a look at Git
. Although there is nothing to do with it, it must be used in development. Who makes you and me both hard-working and developing~~~~

1. Download and installation

This thing is simple, give you a picture and study it yourself~~~~
1. Official website: https://git-scm.com/downloads

2. Download:

3. Main matters:
There is also the installation path, try to be in English, not in Chinese

4. After the installation is complete, further settings are required

  • Open this icon:
                                 

  • What it looks like after opening (the version is different, there may be a little difference, but the difference is similar)
          

  • Git is a distributed version control system, so you need to fill in the username and email as an identifier.

    Command:
    set username: git config --global user.name "your own username"
    set mailbox: git config --global user.email "your own email"
          

  • Verify that the setting is successful
    In your Windows system, under C:\Users\admin (mostly this path), find the .gitconfig file and you can see the information consistent with your configuration, indicating that the setting is successful
          

2. Practice

1. Create a repository

First of all, under any drive letter of yours, perform the following operations. My habit is to create a folder under a certain drive as a git repository (it is a folder for now, not a repository, so be clear about this)

For example: I create a folder called gitHome under G:\JavaInstall,
then it must be inside the gitHome folder, right-click, find Git Bash and open it, as shown in the figure:

This is actually the file opened in the git window, the main path is used to create a repository

  • Let's start creating the repository
    command: git init

At this point, you can see that a .git appears in your gitHome folder (if it is not displayed, go to open hidden files)

So at this point, your .git is your local repository

  • Open .git and the following files will appear, let me explain in detail

2. File creation + status view + new addition + submission + new file content

  • ① View the status of each file in the local repository: git status

    Example: use the touch command to create a new a.txt file, but at this time it must be clear that the a.txt file does not yet belong to the git repository, and must be added to the git repository.

    The important point is that after executing git add a.txt at this time, although it should be added to the git version library, it is only added to the git temporary library at this time, and it is necessary to commit

    Execute git commit a.txt and the following information appears

    Indicates that you need to enter the information you changed at this time, otherwise the '#' part will be ignored, and an empty file will be submitted, which
    means that you need to write the file you want to submit again, which is too troublesome, so the q! command , exit without saving

    Use another method:
    command: git commit -m "create file" a.txt

    OK, after completing the above steps, the a.txt file is truly included in git management;

  • ② Modify the a.txt file just now, you will find the difference of git

    Command: vim a.txt to add a few pieces of data

    [Note]: When git updates a file, the a.txt file has already been included in the management. After modifying the file, it needs to be included again. It will not directly save the original file already in the repository.

    命令依次:git add a.txt
    git commit -m "update file" a.txt
    git status

3. Log + version number + contrast is different

3.1 View the log:

① git log a.txt  

② git log --pretty=oneline a.txt  


这两种查看git日志的方式  
第一个显示信息比较全,作者啊,时间啊,以及对文件是create 还是 update啊,都显示的清晰,但是往往格式太占用屏幕,显示不全  
第二种,显示的就是具体做个啥操作,一行显示完全,不去关心谁创建的,谁修改的等信息  

3.2 Version number

在执行更新操作的时候,尽量加个版本号:  
如:git commit -m "update file 01" a.txt  
这个 "01" 就是版本号,每次对该文件进行更新操作时,尽量给个版本,方便以后查找对比  

3.3 Compare and contrast

命令:git diff a.txt

4. Version Rollback + Version Shuttle + Version Revocation

4.1 version rollback

命令:git reset --hard HEAD^     【注】:HEAD是个指针,^:表示回退一步

4.2 version shuttle

① 命令: git reflog a.txt  查看所有日志记录的指针

② 根据上一步的指针,选择要回退到哪一个位置

以上是回退一步,或者回到指定的步骤  
那么当要回退到很多步呢? 
使用命令:git reset --hard HEAD~4   :表示回退4步  


4.3 Version Revocation

命令:git checkout -- a.txt

小总结:
1.库,首先得是个git库 git init 
2.得有个东西管理,新建个文件 touch a.txt
    之后,向里面填东西,首次的话叫纳入git管理  git add a.txt
3.git commit -m "create file" a.txt  提交到自己的本地库
4.git log 查看git的各种日志
5.git status 查看当前版本库的状态 
6. git commit 相当于往前走,那么 就有往后退 git reset --hard HEAD^ 表示往后退一步
    其中,一个 ^ 表示退一步,多个 ^ 表示退多步
7.版本穿梭 用git reflog a.txt  查出他的全部指针版本号
    再结合 "git reset --hard 7位版本号" 穿梭到想要的版本
8.撤销  使用 "git checkout -- 文件名"  

5. Understand Workspace + Repository + Staging Area

5.1 Workspace

工作区(Working Directory):就是你电脑本地硬盘目录

5.2 Version area

版本库(Repository):工作区有个隐藏目录.git,它就是Git的本地版本库

5.3 Temporary storage area

暂存区(stage):一般存放在"git目录"下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。

当在工作区时  撤销要用 "git checkout -- a.txt"
当在暂存区时  撤销要用 "git reset HEAD a.txt" 再 "git checkout -- a.txt"
当在版本区时  撤销要用 "git reset -hard HEAD^"

Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。  

我们把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用“git add”把文件纳入Git管理,实际是把本地文件修改添加到暂存区;
第二步是用“git commit”提交更改,实际上就是把暂存区的所有内容提交到当前分支 。

因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以commit就是往master分支上提交更改。

可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。一旦提交完后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的。即:nothing to commit (working directory clean)。

用“git diff HEAD -- filename”命令可以查看工作区和暂存区里面最新版本的区别。
新建过撤销未add: git checkout  -- 文件名
撤销已add未commit:先git  reset HEAD  文件名 再 git checkout -- 文件名
撤销已add已commit:       git reset –hard HEAD^  


所以,一定要明确,当前所操作的文件是在git的哪一个区,才能保证执行前进和回退的正确性

5.4 Delete command

① git rm a.txt 
② git commit -m "delete a.txt" a.txt

即 在git中删除要执行两步操作,虽然删了,但是还得提交一下 

5.5 Branch (view+new+switch+merge(branch name)+delete)

  • 5.5.1 View branch
    • Command: git branch

  • 5.5.2 Create a new branch
    • Command: git branch branch name

  • 5.5.3 Switching branches
    • ① Command: git checkout branch name

    • ② Command: git branch -b branch name: indicates that the branch and switch are completed with one command

    Moreover, the sub-branch will copy the contents of the master (main branch) to its own branch, the purpose is to try not to operate in the main branch

  • 5.5.4 Merging branches
    • Command: git merge branch name

    [Note]: work on the sub-line, merge on the main line
  • 5.5.5 Delete branch
    • Command git branch -d branch name

    [Note]: It is necessary to ensure that the content of the sub-line is consistent with the main line, so after each merge, delete the sub-line, and then create a new sub-line to keep the content consistent with the main line forever.

5.6 version conflict

  • Sub-branch adds data in a.txt file

  • The main branch adds different data to the same position in a.txt
  • Merge found conflicts

  • View conflicts, perform manual intervention, and delete conflicting symbols

  • The effect after deletion (don't delete the data submitted by teammates)
  • Add to the repository and commit

    In this way, the conflict is resolved, that is, manually delete the conflict

3. Using GitHub to work with git

github是一个git项目托管网站,主要提供基于git的版本托管服务

github是一个基于git的代码托管平台, Git 并不像 SVN 那样有个中心服务器。目前我们使用到的 Git 命令都是在本地执行,如果你想通过 Git 分享你的代码或者与其他开发人员合作。 你就需要将数据放到一台其他开发人员能够连接的服务器上

1. GitHub application:

 第一步 注册+检查.ssh秘钥:由于你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以我们需要配置验证信息(No such file or directory表示第一次)

第二步 创建SSH Key: ssh-keygen -t rsa -C [email protected]成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。  


第三步 粘贴id_rsa.pub 的内容到 GitHub,注意粘贴的内容一定要恰好,不要多一个空格或者少一个空格  
      操作依次为:登录GitHub → 头像位置找到settings → 选择左侧菜单的 SSH and GPG keys → 左上角 New SSH key → 起个名 粘贴公钥 



第四步 测试连通性: ssh -T [email protected]  

表示连接成功了,此时再回到你的 .ssh 文件 ,会多了一个名字为 known_hosts文件

第五步 本地 → github远程库

    现在的情景是,我在本地创建了一个OA项目后,我又想在GitHub创建一个OA项目,并且让这两个仓库进行远程同步
    5.1  先在本地新建好一个git项目  

    5.2  到Github上新建一个同名的空项目 (是同名  我这里后来又建了一个,忘记修改,都默认oa.txt即可)


    5.3  本地和github上的仓库进行关联
         命令:git remote add origin https://github.com/zzyybs/oa.git

    5.4 把本地库的内容推送到远程git push命令,实际上是把当前分支master推送到远程
        命令:git push -u origin master
        
        命令:git pull origin master : 是从远程库的master 下载到 本地库的 master


Since the remote library is empty, when we push the master branch for the first time, we add the -u parameter. Git will not only push the contents of the local master branch to the remote new master branch, but also push the local master branch and the remote master branch. The master branch is linked to simplify commands for future pushes or pulls.

第六步 github远程库 → 本地

现在的情景是,我们从零开发,那么最好的方式是先创建远程库,然后从远程库克隆

6.1  登陆GitHub,创建一个新的仓库,名字叫OA2
6.2  远程库OK,下一步是用命令git clone克隆一个本地库
    Git clone [email protected]:xxx/oa2.git

7.远程交互模型 -- 上

7.Git交互模型-下

一般工作流程如下:

    1 克隆 Git 资源作为工作目录。

    2 在克隆的资源上添加或修改文件。

    3 如果其他人修改了,你可以更新资源。

    4 在提交前查看修改。

    5 提交修改。

    6 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。

8.Fork

现在的情景是,用叉子把别人的东西(copy no cut)叉到你碗里~
就是把别人的项目clone一份,但是owner变成自己,这样你就可以在遵守Open source license的前提下任意修改这个项目了。

相当于你在原项目的主分支上又建立了一个分支,你可以在该分支上任意修改,如果想将你的修改合并到原项目中时,可以pull 
request,这样原项目的作者就可以将你修改的东西合并到原项目的主分支上去,这样你就为开源项目贡献了代码,开源项目就
会在大家共同的努力下不断壮大和完善。

9.解决Git push时重复输入用户名密码
    C:\Users\admin目录下新建名字为_netrc的文件并编辑该文件写如下内容:
        machine github.com
        login 你的用户名
        password 你的密码

    比如:
        machine github.com
        login li4
        password 123

10.Git 常用命令小总结
   mkdir:         XX (创建一个空目录 XX指目录名)
   pwd:          显示当前目录的路径。
   git init          把当前的目录变成可以管理的git仓库,生成隐藏.git文件。
   touch           xx文件或者新建文件
   git add XX       把xx文件添加到暂存区去。
   git commit –m “XX”  提交文件 –m 后面的是注释。
   git status        查看仓库状态
   git diff  XX      查看XX文件修改了那些内容
   git log          查看历史记录
   git reset  --hard HEAD^
   cat XX         查看XX文件内容
   git reflog       查看历史记录的版本号id
   git checkout -- XX  把XX文件在工作区的修改全部撤销。
   git rm XX          删除XX文件
   git remote add origin https://github.com/zzyybs/testgit 关联一个远程库
   git push –u(第一次要用-u 以后不需要) origin master 把当前master分支推送到远程库
   git clone https://github.com/arjrzhouyang/testgit  从远程库中克隆
   git checkout –b dev  创建dev分支 并切换到dev分支上
   git branch  查看当前所有的分支
   git checkout master 切换回master分支
   git merge dev    在当前的分支上合并dev分支
   git branch –d dev 删除dev分支
   git branch name  创建分支
   git remote 查看远程库的信息
   git remote –v 查看远程库的详细信息
   git push origin master  Git会把master分支推送到远程库对应的远程分支上  

 常用基本操作命令25个左右

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339267&siteId=291194637