Git远程操作

[复制链接]
发表于 2025-7-2 16:48:27 | 显示全部楼层 |阅读模式
目次
1. 理解分布式版本控制系统
2. 远程堆栈
3. 新建远程堆栈
4. 克隆远程堆栈
4.1 利用HTTPS方式:
4.2 利用SSH方式:
5. 向远程堆栈推送
总结:
问题:
6. 拉取远程堆栈
7. 配置Git
7.1 忽略特殊文件
8. 给下令配置别名


1. 理解分布式版本控制系统

我们目前所说的所有内容(工作区,暂存区,版本库等等),都是在本地!也就是在你的笔记本或者盘算机上。而我们的Git着实是分布式版本控制系统!什么意思呢?
可以简单理解为,我们每个人的电脑上都是一个完备的版本库,这样你工作的时候,就不需要联网了,由于版本库就在你本身的电脑上。既然每个人电脑上都有一个完备的版本库,那多个人如何协作呢?比方说你在本身电脑上改了文件A,你的同事也在他的电脑上改了文件A,这时,你们俩之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。


分布式版本控制系统的安全性要高许多,由于每个人电脑里都有完备的版本库,某一个人的电脑坏掉了不要紧,随便从其他人那边复制一个就可以了。
在实际利用分布式版本控制系统的时候,着实很少在两人之间的电脑上推送版本库的修改,由于可能你们俩不在一个局域网内,两台电脑互相访问不了。也可能本日你的同事病了,他的电脑压根没有开机。因此,分布式版本控制系统通常也有一台充当“中心折务器”的电脑,但这个服务器的作用仅仅是用来方便“互换”各人的修改,没有它各人也一样干活,只是互换修改不方便而已。有了这个“中心折务器”的电脑,这样就不怕本地出现什么故障了(好比运气差,硬盘坏了,上面的所有东西全部丢失,包罗git的所有内容)
2. 远程堆栈

Git是分布式版本控制系统,同一个Git堆栈,可以分布到不同的机器上。怎么分布呢?最早,肯定只有一台机器有一个原始版本库,今后,别的机器可以“克隆”这个原始版本库,而且每台机器的版本库着实都是一样的,并没有主次之分。
你肯定会想,至少需要两台机器才能玩远程库不是?但是我只有一台电脑,怎么玩?
着实一台电脑上也是可以克隆多个版本库的,只要不在同一个目次下。不过,现实生活中是不会有人这么傻的在一台电脑上搞几个远程库玩,由于一台电脑上搞几个远程库完全没故意义,而且硬盘挂了会导致所有库都挂掉,以是我也不告诉你在一台电脑上怎么克隆多个堆栈。实际情况往往是这样,找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”堆栈克隆一份到本身的电脑上,而且各自把各自的提交推送到服务器堆栈里,也从服务器堆栈中拉取别人的提交。
完全可以本身搭建一台运行Git的服务器,不过现阶段,为了学Git先搭个服务器绝对是小题大作。幸亏这个天下上有个叫GitHub的神奇的网站,从名字就可以看出,这个网站就是提供Git堆栈托管服务的,以是,只要注册一个GitHub账号,就可以免费获得Git远程堆栈。
GitHub · Build and ship software on a single, collaborative platform · GitHubhttps://github.com/工作台 - Gitee.comhttps://gitee.com/github是国外的网站,速度比较慢,我们博客统一接纳码云来托管代码。下来,我们从零开始,利用一下码云远程堆栈。
3. 新建远程堆栈

新建远程项目堆栈:

填写基本信息:

堆栈名称:堆栈名称一样平常围绕的是项目的名字来写。
堆栈先容:你这个堆栈中存放的是什么东西,什么内容。
Readme文件:项目的先容。
创建成功

lssues:让发现问题的职员与堆栈的管理者举行交换的地方。



Pull Request模板:实际开发过程中不允许dev分支merge到master分支上的,这个时候开发者要提PR,PR就是Pull Request,可以理解为举行一个归并的申请单,再这个申请单中要阐明为什么要归并,dev分支有什么功能,以及其他的缘故原由,都要写再这个里面,这个申请单就是给堆栈的管理员看的,一旦管理员同意了,我们才可以将我们的代码merge到master分支上的。

4. 克隆远程堆栈

克隆/下载远端堆栈到本地,需要利用gitclone下令,后面跟上我们的远端堆栈的链接,远端堆栈的链接可以从堆栈中找到:选择“克隆/下载”获取远程堆栈链接:

利用ssh协议的话利用的是公钥加密和公钥登录的机制,体现的是实用性和安全性,假如我们实用ssh提供的连接举行克隆操作的话,我们得先将我们本地或者本地服务器上的公钥存到git的服务器上。

HTTPS,它提供的链接我们再克隆的时候是没有任何要求的,直接利用链接克隆即可。
4.1 利用HTTPS方式:

  1. aurora@wanghao:~$ ls
  2. 112  gitcode  Linux  mysql-apt-config_0.8.34-1_all.deb  SQL  test  Udp_Test
  3. aurora@wanghao:~$ git clone https://gitee.com/Axurea/remote-gitcode.git
  4. Cloning into 'remote-gitcode'...
  5. remote: Enumerating objects: 10, done.
  6. remote: Counting objects: 100% (10/10), done.
  7. remote: Compressing objects: 100% (10/10), done.
  8. remote: Total 10 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
  9. Unpacking objects: 100% (10/10), 7.13 KiB | 3.57 MiB/s, done.
  10. aurora@wanghao:~$ ls
  11. 112  gitcode  Linux  mysql-apt-config_0.8.34-1_all.deb  remote-gitcode  SQL  test  Udp_Test
  12. aurora@wanghao:~$ cd remote-gitcode/
  13. aurora@wanghao:~/remote-gitcode$ la
  14. .git  .gitee  LICENSE  README.en.md  README.md
  15. aurora@wanghao:~/remote-gitcode$
  16. aurora@wanghao:~/remote-gitcode$ git remote
  17. origin # 远程仓库的默认名字
  18. aurora@wanghao:~/remote-gitcode$ git remote -v
  19. origin        https://gitee.com/Axurea/remote-gitcode.git (fetch) # 获取
  20. origin        https://gitee.com/Axurea/remote-gitcode.git (push) # 推
  21. # 拥有和远程仓库的推权限和拉权限
复制代码
执行git clone操作不能在本地堆栈的目次下去执行,只要不在本地堆栈地点的目次下执行git clone下令,在哪都可以执行这个下令。remote-gitcode就是对于的我们的远程堆栈,进入remote-gitcode文件,.gitee README.en.md README.md这三个就是我们堆栈中存在的一些内容,.git着实就是一个版本库,也就是git的一个堆栈。
4.2 利用SSH方式:

ssh利用的是公钥加密和公钥登录的机制,我们要想利用SSH提供给我们的链接举行克隆操作的话,必须要将我们本身本地的服务器的公钥放到git服务器上举行管理,才可以克隆成功。
  1. aurora@wanghao:~$ rm -rf remote-gitcode/ # 首先得删掉HTTPS克隆的,否则会冲突
  2. aurora@wanghao:~$ ls
  3. 112  gitcode  Linux  mysql-apt-config_0.8.34-1_all.deb  SQL  test  Udp_Test
  4. aurora@wanghao:~$ git clone git@gitee.com:Axurea/remote-gitcode.git
  5. Cloning into 'remote-gitcode'...
  6. git@gitee.com: Permission denied (publickey).
  7. fatal: Could not read from remote repository.
  8. Please make sure you have the correct access rights
  9. and the repository exists.
复制代码
这个错误着实就是我们没有在git服务器上配置本地服务器的公钥,
第一步:创建SSHKey。在用户主目次下,看看有没有.ssh目次,假如有,再看看这个目次下有没有id_rsa(私钥)和id_rsa.pub(公钥)这两个文件,假如已经有了,可直接跳到下一步。假如没有,需要创建SSH Key:
  1. aurora@wanghao:~$ la
  2. 112            .bashrc  gitcode     Linux                              .mysql_history  .ssh      .viminfo        .wget-hsts
  3. .bash_history  .cache   .gitconfig  .local                             .profile        test      .vscode         .Xauthority
  4. .bash_logout   .dotnet  .lesshst    mysql-apt-config_0.8.34-1_all.deb  SQL             Udp_Test  .vscode-server
  5. aurora@wanghao:~$ cd .ssh/
  6. aurora@wanghao:~/.ssh$ ls # 没有公钥和私钥,需要我们生成
  7. known_hosts
  8. aurora@wanghao:~/.ssh$ cd ..
  9. aurora@wanghao:~$ ls
  10. 112  gitcode  Linux  mysql-apt-config_0.8.34-1_all.deb  SQL  test  Udp_Test
  11. aurora@wanghao:~$ ssh-keygen -t rsa -C "1358954942@qq.com" # 填写自己的邮箱,和git中的要一样
  12. Generating public/private rsa key pair.
  13. Enter file in which to save the key (/home/aurora/.ssh/id_rsa):
  14. Enter passphrase (empty for no passphrase):
  15. Enter same passphrase again:
  16. Your identification has been saved in /home/aurora/.ssh/id_rsa
  17. Your public key has been saved in /home/aurora/.ssh/id_rsa.pub
  18. The key fingerprint is:
  19. SHA256:rODEDbxMvCiB0jRBPGzVc0iXaTuC1o1Au145TbOJjaQ 1358953=4942@qq.com
  20. The key's randomart image is:
  21. +---[RSA 3072]----+
  22. | +=+oo...o       |
  23. |.o=+..+.=        |
  24. |+...*+.*o.       |
  25. |.. =oO+O++       |
  26. |. ..E B.S.       |
  27. | . + o o         |
  28. |    o .          |
  29. |                 |
  30. |                 |
  31. +----[SHA256]-----+
复制代码
顺利的话,可以在用户主目次里找到.ssh目次,里面有id_rsa和id_rsa.pub两个文件,这两个就是sSHKey的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
  1. aurora@wanghao:~$ cd .ssh/
  2. aurora@wanghao:~/.ssh$ ls
  3. id_rsa  id_rsa.pub  known_hosts
复制代码
第二步:添加本身的公钥到远端堆栈。
  1. aurora@wanghao:~/.ssh$ cat id_rsa.pub
  2. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC2HSXxvaHDHeIRTEkb//W2mnKAkim30DwMIO+RJuNv5KvLfgsyMteak1HEIcy8LBbSGJlPoIYK3+8nLUhu7VWIKbDY4PLnH2YPDUyUgSBfvF0hEId8PxjyQkU9Vw/0AKem9zeAFLYkacTYVvhmequ8BHAUtLZOeg7HYTxw3R38wcAfaVsHxungsz/BM3iD0Oz/zxOD1wLPzKXxbD2bvYhU/Rs/GOlLJWYgWC8V3E7tVuwNwrtwJIpRxRrgitDed1Gxq5PZJ8Vy6RwXDfcrAyaS1Easl8DgByX6mBiE5bLbjQgqS2caSWTejnZb8j0QrN3o/J8bHYC+F+yPlzBa0jVdTqB3LXfC047IolVomXkrTPpQyt++LfDai6OttsO5m8JvuoKWGZ755xfQTQJpxaWzsobUDXSdQB5636FWNTZwbdSgJH1kpMU+Tk1n6WNswVptdd3BRjAjseatjXxQZY5QSg0mSUaNCZTo+t/JKKU3X47eRqKyWS6048a/az0TOs= 1358954942@qq.com
复制代码
添加到远端堆栈。

点击确定,输入本身码云的密码即可。

添加成功。

在一个堆栈下,有多人协作的开发,假如你们都用的ssh,我们需要配置多个人,多个电脑上的公钥,直接添加即可,是支持多个公钥的。
  1. aurora@wanghao:~$ ls
  2. 112  gitcode  Linux  mysql-apt-config_0.8.34-1_all.deb  SQL  test  Udp_Test
  3. aurora@wanghao:~$ git clone git@gitee.com:Axurea/remote-gitcode.git
  4. Cloning into 'remote-gitcode'...
  5. remote: Enumerating objects: 10, done.
  6. remote: Counting objects: 100% (10/10), done.
  7. remote: Compressing objects: 100% (10/10), done.
  8. remote: Total 10 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
  9. Receiving objects: 100% (10/10), 7.15 KiB | 7.15 MiB/s, done.
  10. Resolving deltas: 100% (1/1), done.
  11. aurora@wanghao:~$ ls
  12. 112  gitcode  Linux  mysql-apt-config_0.8.34-1_all.deb  remote-gitcode  SQL  test  Udp_Test
  13. aurora@wanghao:~$
复制代码
done,成功!假如有多个人协作开发,GitHub/Gitee允许添加多个公钥,只要把每个人的电脑上的Key都添加到GitHub/Gitee,就可以在每台电脑上往GitHub/Gitee上提交推送了。
当我们从远程堆栈克隆后,实际上Git会自动把本地的master分支和远程的master分支对应起来,而且,远程堆栈的默认名称是origin。在本地我们可以利用gitremote下令,来查看远程库的信息,如:
  1. aurora@wanghao:~/remote-gitcode$ git remote
  2. origin
复制代码
或者,用git remote-v显示更具体的信息:
  1. aurora@wanghao:~/remote-gitcode$ git remote -v
  2. origin        git@gitee.com:Axurea/remote-gitcode.git (fetch)
  3. origin        git@gitee.com:Axurea/remote-gitcode.git (push)
复制代码
5. 向远程堆栈推送

将本地堆栈最新的内容推送到远端。
不是将本地堆栈推送到远端,而是将本地堆栈中的某一个分支推送到远端,push操作是分支和分支之间的交互。
我们首先查看一下配置:
  1. aurora@wanghao:~$ ls
  2. 112  gitcode  Linux  mysql-apt-config_0.8.34-1_all.deb  remote-gitcode  SQL  test  Udp_Test
  3. aurora@wanghao:~$ cd remote-gitcode/
  4. aurora@wanghao:~/remote-gitcode$ git config -l
  5. user.name=17309339946
  6. user.email=1358954942@qq.com
  7. core.repositoryformatversion=0
  8. core.filemode=true
  9. core.bare=false
  10. core.logallrefupdates=true
  11. remote.origin.url=git@gitee.com:Axurea/remote-gitcode.git
  12. remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
  13. branch.master.remote=origin
  14. branch.master.merge=refs/heads/master
  15. aurora@wanghao:~/remote-gitcode$ git config --global user.name "Axurea"
  16. aurora@wanghao:~/remote-gitcode$ git config -l
  17. user.name=Axurea
  18. user.email=1358954942@qq.com
  19. core.repositoryformatversion=0
  20. core.filemode=true
  21. core.bare=false
  22. core.logallrefupdates=true
  23. remote.origin.url=git@gitee.com:Axurea/remote-gitcode.git
  24. remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
  25. branch.master.remote=origin
  26. branch.master.merge=refs/heads/master
复制代码

这里我修改了一下user.name,着实电话号码当作user.name都是可以的。
本地以及clone成员远程堆栈后,我们便可以向堆栈中提交内容,比方新增一个file.txt文件。
  1. aurora@wanghao:~/remote-gitcode$ git branch
  2. * master
  3. aurora@wanghao:~/remote-gitcode$ vim file.txt
  4. aurora@wanghao:~/remote-gitcode$ cat file.txt
  5. hello git
  6. aurora@wanghao:~/remote-gitcode$ git add .
  7. aurora@wanghao:~/remote-gitcode$ git commit -m "create file.txt"
  8. [master ec314c3] create file.txt
  9. 1 file changed, 1 insertion(+)
  10. create mode 100644 file.txt
  11. aurora@wanghao:~/remote-gitcode$ git status
  12. On branch master #在哪个分支
  13. Your branch is ahead of 'origin/master' by 1 commit.
  14.   (use "git push" to publish your local commits)
  15. # 克隆的远程仓库就有上面的两行,建议使用git push将本地的进行推送操作。推送到远程仓库的master,origin/master
  16. nothing to commit, working tree clean # 工作树很干净
  17. aurora@wanghao:~/remote-gitcode$ git push origin master:master #  origin 表示远端,master:master表示本地分支:远端分支,本地分支和远端分支名字一样的话可以省略远端分支
  18. Enumerating objects: 4, done.
  19. Counting objects: 100% (4/4), done.
  20. Delta compression using up to 2 threads
  21. Compressing objects: 100% (2/2), done.
  22. Writing objects: 100% (3/3), 274 bytes | 274.00 KiB/s, done.
  23. Total 3 (delta 1), reused 0 (delta 0)
  24. remote: Powered by GITEE.COM [1.1.5]
  25. remote: Set trace flag 4e4879ab
  26. To gitee.com:Axurea/remote-gitcode.git
  27.    326cc7c..ec314c3  master -> master
  28. # 看一下状态,已经不让我们去push了  
  29. aurora@wanghao:~/remote-gitcode$ git status
  30. On branch master
  31. Your branch is up to date with 'origin/master'.
  32. nothing to commit, working tree clean
复制代码
推送成功!这里由于我们利用的是SSH协议,是不用每一次推送都输入密码的,方便了我们的推送操作。假如你利用的是HTTPS协议,有个麻烦地方就是每次推送都必须输入口令。
接下来,看看码云远端:

代码已经推送到远端了:

总结:

提交时要留意,假如我们之前设置过全局的name和e-mail,这两项配置需要和gitee上配置的用户名和邮箱一致,否则会堕落。或者从来没有设置过全局的name和e-mail,那么我们第一次提交时也会报错。这就需要我们重新配置下了,同样要留意需要和gitee上配置的用户名和邮箱一致。
该下令用于将本地的分支版本上传到远程并归并,下令格式如下:
   git push <远程主机名> <本地分支名>:<远程分支名>
  假如本地分支名与远程分支名类似,则可以省略冒号以及后面的内容:
  git push <远程主机名> <本地分支名>
  问题:

为什么可以push成功?
  1. aurora@wanghao:~/remote-gitcode$ git remote -v
  2. origin        git@gitee.com:Axurea/remote-gitcode.git (fetch)
  3. origin        git@gitee.com:Axurea/remote-gitcode.git (push)
复制代码
我们利用remote -v选项查看,发现我们是有push权限的,肯定可以推送成功,推送是远程分支和本地分支之间的交互,以是,我们必须要让两个分支要举行链接,建立关系,有这个关系了,才可以成功的push,这个关系我也没有建立啊,那什么时候建立的呢?
对于远程堆栈和本地堆栈的master分支来说,再克隆的时候git会自动帮我们建立联系,以是对于master直接push就成功了。
6. 拉取远程堆栈

将远程堆栈最新的内容拉取到本地。
在gitee上点击file.txt文件并在线修改它。

这里只是模仿,千万不要在远程堆栈上修改任何的代码,要改也是在本地改,然后推送到远端。提交成功我们远程堆栈的内容是要比本地堆栈内容新的。
此时,远程堆栈是要领先于本地堆栈一个版本,为了使本地堆栈保持最新的版本,我们需要拉取下远端代码,并归并到本地。Git提供了gitpull下令,该下令用于从远程获取代码并归并本地的版本。格式如下:
   git pull<远程主机名> <远程分支名>:<本地分支名>
  假如远程分支与当前分支归并,则可以省略冒号以及后面的内容:
  git pull<远程主机名> <远程分支名>
  利用以下:
  1. # 拉取远程分支,并与当前分支进行合并
  2. # pull: 拉去 + 合并
  3. # aurora@wanghao:~/remote-gitcode$ git pull origin master:master # 也可以这样写
  4. aurora@wanghao:~/remote-gitcode$ git pull origin master
  5. remote: Enumerating objects: 5, done.
  6. remote: Counting objects: 100% (5/5), done.
  7. remote: Compressing objects: 100% (2/2), done.
  8. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
  9. Unpacking objects: 100% (3/3), 945 bytes | 945.00 KiB/s, done.
  10. From gitee.com:Axurea/remote-gitcode
  11. * branch            master     -> FETCH_HEAD
  12.    ec314c3..c815ddc  master     -> origin/master
  13. Updating ec314c3..c815ddc
  14. Fast-forward
  15. file.txt | 1 +
  16. 1 file changed, 1 insertion(+)
  17. aurora@wanghao:~/remote-gitcode$ cat file.txt
  18. hello git
  19. hello world
  20. # 查看当前仓库的状态 - 当前仓库是非常干净的,原因就是pull帮我们合并了,所以对于本地仓库来说没有任何的改动
  21. aurora@wanghao:~/remote-gitcode$ git status
  22. On branch master
  23. Your branch is up to date with 'origin/master'.
  24. nothing to commit, working tree clean
复制代码
7. 配置Git

7.1 忽略特殊文件

在一样平常开发中,我们有些文件不想或者不应该提交到远端,好比生存了数据库密码的配置文件,那怎么让Git知道呢?在Git工作区的根目次下创建一个特殊的·gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件了。
不需要重新写·gitignore文件,gitee在创建堆栈时就可以为我们天生,不过需要我们主动勾选一下:

假如当时没有选择这个选择,在工作区创建一个也是可以的。无论哪种方式,终极都可以得到一个完备的·gitignore文件,比方我们想忽略以so和.ini末端所有文件,·gitignore的内容如下:
  1. # 省略选择模本的内容
  2. ...
  3. # My configurations:
  4. *.ini
  5. *.so
复制代码
在.gitignore文件中也可以指定某个确定的文件。
最后一步就是把.gitignore也提交到远端,就完成了:
  1. aurora@wanghao:~/remote-gitcode$ vim .gitignore
  2. aurora@wanghao:~/remote-gitcode$ cat .gitignore
  3. # 可以直接写文件名称
  4. *.so
  5. *.ini
  6. aurora@wanghao:~/remote-gitcode$ touch a.so
  7. aurora@wanghao:~/remote-gitcode$ ls
  8. a.so  file.txt  LICENSE  README.en.md  README.md
  9. aurora@wanghao:~/remote-gitcode$ git status
  10. On branch master
  11. Your branch is up to date with 'origin/master'.
  12. Untracked files:
  13.   (use "git add <file>..." to include in what will be committed)
  14.         .gitignore
  15. nothing added to commit but untracked files present (use "git add" to track)
复制代码
打印堆栈的状态我们发现新增的a.so文件并没有打印出来,没有提示修改新增文件,这就是由于我们在.gitignore文件中去举行了配置,以.so为末端的文件都让git不去追踪管理,以是git status就没有它们。
  1. aurora@wanghao:~/remote-gitcode$ git add .
  2. aurora@wanghao:~/remote-gitcode$ git status
  3. On branch master
  4. Your branch is up to date with 'origin/master'.
  5. Changes to be committed:
  6.   (use "git restore --staged <file>..." to unstage)
  7.         new file:   .gitignore
复制代码
我们add一下,发现在暂存区中只有一个文件新增了,也就是.gitignore文件,我们的a.so文件并没有被新增到暂存区中,以是.gitignore文件生效了。
但有些时候,你就想添加一个文件到Git,但由于这个文件被.gitignore忽略了,根本添加不了,那么就可以用-f强制添加。
  1. aurora@wanghao:~/remote-gitcode$ touch b.so
  2. aurora@wanghao:~/remote-gitcode$ git add -f b.so
  3. aurora@wanghao:~/remote-gitcode$ git status
  4. On branch master
  5. Your branch is up to date with 'origin/master'.
  6. Changes to be committed:
  7.   (use "git restore --staged <file>..." to unstage)
  8.         new file:   .gitignore
  9.         new file:   b.so
复制代码
另有些时候,当我们编写了规则排除了部分文件时,比方:
  1. # 排除所有so结尾的文件
  2. *.so
复制代码
固然可以用git add-f强制添加进去,但有还是希望不要破坏 .gitignore规则,这个时候,可以添加一条例外规则: 
  1. # 排除所有so结尾的文件
  2. *.so# 不排除c.so!c.so
复制代码
把指定文件排除在.gitignore规则外的写法就是!+文件名,以是,只需把例外文件添加进去即可。
  1. aurora@wanghao:~/remote-gitcode$ vim .gitignore
  2. aurora@wanghao:~/remote-gitcode$ cat .gitignore
  3. # 可以直接写文件名称
  4. *.so
  5. *.ini
  6. !c.so
  7. aurora@wanghao:~/remote-gitcode$ touch c.so
  8. aurora@wanghao:~/remote-gitcode$ git status
  9. On branch master
  10. Your branch is up to date with 'origin/master'.
  11. Changes to be committed:
  12.   (use "git restore --staged <file>..." to unstage)
  13.         new file:   .gitignore
  14.         new file:   b.so
  15. Changes not staged for commit:
  16.   (use "git add <file>..." to update what will be committed)
  17.   (use "git restore <file>..." to discard changes in working directory)
  18.         modified:   .gitignore
  19. Untracked files:
  20.   (use "git add <file>..." to include in what will be committed)
  21.         c.so
复制代码
固然打印的是没有被追踪的文件,但是它能把c.so打印出来,就阐明已经开始管c.so了,并没有忽略。
假如有一天.gitignore忽略的文件特殊多,我们不知道忽略了哪些文件,那么这个时候不知道.so末端的文件被忽略了,我们有一天创建了d.so,那么这个时候由于.gitignore文件特殊多,可能已经找不到忽略.so末端的代码了,那么我们想查看一下d.so为什么被忽略,我们应该怎么办?
  1. aurora@wanghao:~/remote-gitcode$ git check-ignore -v d.so
  2. .gitignore:3:*.so        d.so
复制代码
利用该下令,我们就会发现打印了内容,d.so被忽略的缘故原由就是在第三行有一个*.so,由这个规则把我们的d.so给忽略了。
最后给全部推送到远端:
  1. aurora@wanghao:~/remote-gitcode$ git add .
  2. aurora@wanghao:~/remote-gitcode$ git commit -m "md"
  3. [master f777118] md
  4. 3 files changed, 6 insertions(+)
  5. create mode 100644 .gitignore
  6. create mode 100644 b.so
  7. create mode 100644 c.so
  8. aurora@wanghao:~/remote-gitcode$ git push
  9. Enumerating objects: 5, done.
  10. Counting objects: 100% (5/5), done.
  11. Delta compression using up to 2 threads
  12. Compressing objects: 100% (2/2), done.
  13. Writing objects: 100% (4/4), 352 bytes | 352.00 KiB/s, done.
  14. Total 4 (delta 1), reused 0 (delta 0)
  15. remote: Powered by GITEE.COM [1.1.5]
  16. remote: Set trace flag 034f17ba
  17. To gitee.com:Axurea/remote-gitcode.git
  18.    c815ddc..f777118  master -> master
复制代码

远端就会有相应的文件。
8. 给下令配置别名

在我们利用Git期间,有些下令敲的时候着实让人头疼(太长了。。),幸运的是,git支持对下令举行简化!
举个例子,将git status简化为git st,对应的下令为:
  1. aurora@wanghao:~/remote-gitcode$ git status
  2. On branch master
  3. Your branch is up to date with 'origin/master'.
  4. nothing to commit, working tree clean
  5. aurora@wanghao:~/remote-gitcode$ git config --global alias.st status
复制代码
--global参数是全局参数,也就是这些下令在这台电脑的所有Git堆栈下都有效。假如不加,那只针对当前的堆栈起作用。
好了,现在敲git st看看效果:
  1. aurora@wanghao:~/remote-gitcode$ git st
  2. On branch master
  3. Your branch is up to date with 'origin/master'.
  4. nothing to commit, working tree clean
  5. # git status还能用
  6. aurora@wanghao:~/remote-gitcode$ git status
  7. On branch master
  8. Your branch is up to date with 'origin/master'.
  9. nothing to commit, working tree clean
复制代码
我们再来实行一个下令:
  1. aurora@wanghao:~/remote-gitcode$ git log --pretty=oneline --abbrev-commit
  2. f777118 (HEAD -> master, origin/master, origin/HEAD) md
  3. c815ddc update file.txt.
  4. ec314c3 create file.txt
  5. 326cc7c add LICENSE.
  6. b2cc496 Initial commit
  7. aurora@wanghao:~/remote-gitcode$ git config --global alias.lpa 'log --pretty=oneline --abbrev-commit'
  8. aurora@wanghao:~/remote-gitcode$ git lpa
  9. f777118 (HEAD -> master, origin/master, origin/HEAD) md
  10. c815ddc update file.txt.
  11. ec314c3 create file.txt
  12. 326cc7c add LICENSE.
  13. b2cc496 Initial commit
  14. aurora@wanghao:~/remote-gitcode$
复制代码
不建议刚开始学习git的时候就去配置别名,还是建议多敲敲长下令,认识认识用法,等工作的时候再简化。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表