热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

gitlab命令行使用(基础篇)

git是分布式代码管理工具,越来越多的企业使用它。所以掌握git的使用至关重要。它的远端管理是基于s

git 是分布式代码管理工具,越来越多的企业使用它。所以掌握git的使用至关重要。它的远端管理是基于ssh,所以在使用远端git的时候需要进行ssh配置。

ssh是一种免密登录,使用的rsa非对称加密来进行服务器认证;

一、git 的ssh配置如下

  • 首先你需要拥有gitlab或是github的一套账号,然后设置git的user name 和 user email:
# git 全局设置账号
git config --global user.name "your name"
git config --global user.email "your e-mail"
  • 根据 user name 和 user email 生成自己的ssh密钥
cd $USER 
cd .ssh #如果存在,需要备份可以备份下,如果不需要可以直接覆盖

ssh-keygen -t rsa -C "your e-mail" #生成 ssh

执行结果:

gitlab命令行使用(基础篇)

最终得到 id_rsa 和 id_rsa.pub

  • gitlab上面添加ssh密钥,使用文件 id_rsa.pub。

打开 https://gitlab.company.com/ ,使用自己账号登录,然后添加ssh密钥;

主页头像 -> profile -> SSH Keys -> Add key

gitlab命令行使用(基础篇)

将id_rsa.pub文件中的内容添加上去,然后点击Add key;

gitlab命令行使用(基础篇)

  • 测试 ssh 链接gitlab.company.com
ssh git@gitlab.company.com

#会出现如下提示,说明链接成功
The authenticity of host 'gitlab.company.com (100.98.137.229)' can't be established.
RSA key fingerprint is SHA256:WiQ1s8RKGjQpO0ICFY3NV2Hs0axwIbrv6j6q0XJsdsc.
RSA key fingerprint is MD5:6c:8d:0f:09:31:c9:08:9b:67:48:09:7c:d9:46:65:3e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.company.com,100.98.137.229' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Welcome to GitLab, your name!
Connection to gitlab.company.com closed.

二、git 的简单使用

创建一个新的仓库

git clone git@gitlab.company.com:yaoname/my-test-git-project.git
cd my-test-git-project
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

touch .gitignore
vi .gitignore #编辑
git add .gitignore
git commit -m "add .gitignore"
git push -u origin master

本地仓库和远端仓库建立连接

cd existing_folder
git init
git remote add origin git@gitlab.company.com:yourname/my-test-git-project.git
git add .
git commit
git push -u origin master

git基本操作讲解

The most commonly used git commands are:
   add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty Git repository or reinitialize an existing one
   log        Show commit logs
   merge      Join two or more development histories together
   mv         Move or rename a file, a directory, or a symlink
   pull       Fetch from and merge with another repository or a local branch
   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status
   tag        Create, list, delete or verify a tag object signed with GPG
  • git 仓库初始化
git init
  • git 生成快照并且存入项目索引
git add .  #或 git add * 提交所有文件
git add README.md #提交指定文件
git add folder/ #提交某个文件夹下面的所有文件
  • git 将项目多索引提交到本地仓库的当前分支下
git commit -m '注解'
  • git 将远端仓库信息更新到本地,并且merge到本地分支
git pull origin master
  • git 推送当前分支到远端分支
git push origin master
  • git 将远端仓库信息更新到本地,不合并到本地分支
git fetch
  • git 创建分支操作
git branch -a #查看所有分支
git branch --list #查看本地分支
git branch feature/20181017.business.chao #创建分支
git checkout feature/20181017.business.chao #切换分支
#或直接创建并且切换
git checkout -b feature/20181017.business01.chao
git push origin feature/20181017.business01.chao #推送到远端
git pull origin feature/20181017.business01.chao #从远端拉取

#删除
git branch -d/-D feature/20181017.business01.chao #删除本地
git push origin --delete feature/20181017.business01.chao #删除远端分
  • git 查看当前分支的状态
git status
  • git merge合并分支
#当前分支 git checkout feature/20181017.business01.chao
touch 1.txt
vi 1.txt # 编辑内容然后提交到远端
git checkout git checkout feature/20181017.business.chao
git merge feature/20181017.business01.chao #合并本地分支
  • git grep 查找当前分支文件中的内容
git grep --text "test" #使用git grep -h 查看更多用法
  • git diff 比较分支
git diff master #比较两个分支的不同
  • git log 查看commit记录
git log -n 10 #查看最近10条提交记录
  • git rm 删除文件以及索引
touch 2.txt
git add 2.txt
git commit -m 'add 2.txt'
git rm 2.txt #删除已经提交到本地仓库的文件
  • git tag 一般用来管理【里程碑】
git checkout master
git tag --list #查看所有标签
git tag v1.0 #添加tag
git tag --list 'v1.*' # 列出version为1的所有子版本
  • git show 获取项目的详细信息
git show #展示项目的各种类型


commit eaa7f945204bed8f2b01d284d99fcf0b3ac3029e
Author: chao 
Date:   Wed Oct 17 06:16:26 2018 +0000

    add README

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7088fed
--- /dev/null
+++ b/README.md
@@ -0,0 +1,5 @@
+
+# this is repositry
  • git rebase 重置当前的操作options,写的不错 rebase详解
git rebase branchName #消除本分支下面的commit
git rebase --continue #在rebase的过程中,也许会出现冲突(conflict). 在这种情况,Git会停止rebase并会让你去解决 冲突;在解决完冲突后,用"git-add"命令去更新这些内容的索引(index), 然后,你无需执行 git-commit,只要执行
git rebase --abort #在任何时候,你可以用--abort参数来终止rebase的行动,并且"mywork" 分支会回到rebase开始前的状态
  • git stash 储藏(临时保存到git栈中,独立于git分支管理之外。那个分支使用恢复到哪个分支就行了)
#储藏使用名称和不使用名称保存未提交到仓库的(暂存和非暂存)
git stash 
git stash save "stash-name-1"
#变更统计
git stash show
#每次储藏
git stash list
#恢复到git版本管理中
git stash pop #弹出栈
#通过名字使用储藏
git stash apply 
#删除
git stash drop
#清空
git stash clear
#直接使用stash创建分支
git stash branch testbranch
  • git revert 向前滚动的方式回滚,不会丢失源代码
git revert commit_id
  • git reset 使用HEAD的状态的方式回滚,会丢失源代码
git reset --hard HEAD^         #回退到上个版本
git reset --hard HEAD~3        #回退到前3次提交之前,以此类推,回退到n次提交之前
git reset --hard commit_id     #退到/进到 指定commit的sha码
git push origin HEAD --force   #强推到远端,使用git push 推送推送失败
  • git bisect 通过二进制搜索找到引入错误的更改
git bisect start #开启查找
git bisect good v2.6.18 
git bisect bad master #查找报错的版本
  • 删除并忽略已经加入到git仓库的文件
#查找到对应的文件
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
#加入git忽略
echo .DS_Store >> ~/.gitignore
#add
git add --all
#提交忽略
git commit -m '.DS_Store banished!'

以上所述就是小编给大家介绍的《gitlab命令行使用(基础篇)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 我们 的支持!


推荐阅读
  • 本文详细介绍了git常用命令及其操作方法,包括查看、添加、提交、删除、找回等操作,以及如何重置修改文件、抛弃工作区修改、将工作文件提交到本地暂存区、从版本库中删除文件等。同时还介绍了如何从暂存区恢复到工作文件、恢复最近一次提交过的状态,以及如何合并多个操作等。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • 加密世界下一个主流叙事领域:L2、跨链桥、GameFi等
    本文介绍了加密世界下一个主流叙事的七个潜力领域,包括L2、跨链桥、GameFi等。L2作为以太坊的二层解决方案,在过去一年取得了巨大成功,跨链桥和互操作性是多链Web3中最重要的因素。去中心化的数据存储领域也具有巨大潜力,未来云存储市场有望达到1500亿美元。DAO和社交代币将成为购买和控制现实世界资产的重要方式,而GameFi作为数字资产在高收入游戏中的应用有望推动数字资产走向主流。衍生品市场也在不断发展壮大。 ... [详细]
  • 像跟踪分布式服务调用那样跟踪Go函数调用链 | Gopher Daily (2020.12.07) ʕ◔ϖ◔ʔ
    每日一谚:“Acacheisjustamemoryleakyouhaven’tmetyet.”—Mr.RogersGo技术专栏“改善Go语⾔编程质量的50个有效实践” ... [详细]
  • 现在比较流行使用静态网站生成器来搭建网站,博客产品着陆页微信转发页面等。但每次都需要对服务器进行配置,也是一个重复但繁琐的工作。使用DockerWeb,只需5分钟就能搭建一个基于D ... [详细]
  • 初始化初始化本地空版本库,仓库,英文名repositorymkdirtest&&cdtestgitinit克隆项目到本地gitclone远程同 ... [详细]
  • pc电脑如何投屏到电视?DLNA主要步骤通过DLNA连接,使用WindowsMediaPlayer的流媒体播放举例:电脑和电视机都是连接的 ... [详细]
  • 先记住几个专用名词,如下:Workspace:工作区IndexStage:暂存区Repository:仓库区(或本地仓库)Remote:远程仓库一、新建代码库#在当前目录新建一个G ... [详细]
  • 开发笔记:对称加密详解,以及JAVA简单实现
     (原)常用的加密有3种1、正向加密,如MD5,加密后密文固定,目前还没办法破解,但是可以能过数据库撞库有一定概率找到,不过现 ... [详细]
  • 用户视图(查看运行状态或其他参数)系统视图(配置设备的系统参数)system-viewEntersystemview,returnuservi ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • ps:写的第一个,不足之处,欢迎拍砖---只是想用自己的方法一步步去实现一些框架看似高大上的小功能(比如说模型中的toArraytoJsonsetAtt ... [详细]
  • hadoop1.2.1文档中这样写:Nowcheckthatyoucansshtothelocalhostwithoutapassphrase:$sshlocalhostIfyou ... [详细]
author-avatar
杨胤才_669
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有