Git 使用教程

版权声明: https://blog.csdn.net/huangpb123/article/details/72356799

一、关于 SSH 密钥

    1. 查找公钥

         ① 先查看当前电脑是否存在 .ssh 文件夹:cd ~/.ssh (Win 默认路径 C:/Users/Administrator 文件夹下)  

         ② 若不存在 .ssh 文件夹,新建 .ssh 文件夹

             终端输入:$ ssh-keygen

             当系统提示输入要保存密钥的文件夹时,直接按 Enter 键,就会接受默认的文件位置。

             .ssh 文件夹里面有 id_rsa 和 id_rsa.pub 两个文件,id_rsa 是私钥,不能泄露出去,id_rsa.pub 是公钥,添加到 GitHub 上时要用到。

     2. GitHub 上添加 SSH Key 

          $ cat id_rsa.pub

         进入 https://github.com -> 点击右上角公文包 -> Settings -> SSH and GPG keys -> Add SSH Key -> 在 Key 输入框添加 id_rsa.pub 文件的内容。

二、常用操作

     1. 如何删除 GitHub 上的项目?

         点击要删除的项目 -> Settings -> 右下角 ‘Delete this repository’ -> 输入要删除的项目名。

     2. 空文件夹不会被提交到 GitHub 上。

     3. 删除 GitHub 上项目里的文件夹但不删除本地文件夹的方法(以删除 .idea 文件夹为例)

         $ git rm -r --cached .idea   //--cached 不会把本地的 .idea 删除

         $ git commit -m 'delete .idea dir'

         $ git push  

     4.  合并分支的某次特定 commit 方法?

$ git log //查看某次特定 commit (例如 f64327)   
$ git checkout master
$ git cherry-pick f64327
$ git pull 
$ git push

        git cherry-pick --abort 可以取消本次合并。

    5. 如何合并多个 commit 为一个?

         我们需要将 2dfbc7 和 c4e858 合并成一个 commit,我们输入以下命令:git rebase -i f1f92b

        接着会进入 vim 界面,按 i 会进入编辑模式,我们把第一条命令之外的 pick 改成 squash(pick 是要提交的,squash 是被压缩的),然后按 ESC 或 Ctr + C 退出编  辑模式进入命令模式,接着输入 :wq 保存并退出。

       接着我们看到了 commit message 的编辑页面

         我们要做的是把2次提交信息修改成一个新的 commit message。

        输入 :wq 保存并退出,再次输入 git log 查看历史信息,发现2次 commit 已经合并了。

        最后,git push 到远程库。

        注意事项:如果合并过程中发生错误,可以使用 git rebase --abort 来撤销修改,回到没有开始操作合并之前的状态。

三、技术点

   1. <<<<  当前代码   ====  传入的代码  >>>>>

   2. Git 管理系统有四个完全独立的存储空间:工作区(本地的工作目录)、暂存区(项目.git文件夹里的Stage或Index,git add  之后缓存到暂存区)、本地仓库(git commit 之后)和远程仓库(git push 之后)。

四、Git 使用案例

Git global setup

git config --global user.name "Noah"

git config --global user.email "[email protected]"

Create a new repository

git clone https://git.xxx.com/xxx/tutorials.git

cd tutorials

touch README.md

git add README.md

git commit -m "add README"

git push -u origin master

Existing folder

cd existing_folder

git init

git remote add origin https://git.xxx.com/xxx/tutorials.git

git add .

git commit -m "Initial commit"

git push -u origin master

Existing Git repository

cd existing_repo

git remote rename origin old-origin

git remote add origin https://git.xxx.com/xxx/tutorials.git

git push -u origin --all

git push -u origin --tags

Mac 可以下一个 Git 的可视化工具 Source Tree 来简化对 git 的操作。

1. SourceTree 创建新分支后要重新拉取一遍远程代码。

猜你喜欢

转载自blog.csdn.net/huangpb123/article/details/72356799