git软件 常规应用方法记录

(1). 本地仓库

  1. 创建git管理的非中文目录

  2. 在该目录下执行命令 git init

  3. 在本地repository目录下执行命令:

git config --global user.name “你的用户名”

git config --global user.email “你的邮箱”

  1. 安装ssh-keygen
    sudo apt-get install openssh-client ssh-keygen

  2. 生成ssh key,并复制到粘贴板,执行命令:
    ssh-keygen -t rsa -C “你的邮箱”
    #粘贴命令
    clip < ~/.ssh/id_rsa.pub

  3. 注册账号, https://github.com/ 或企业gitlab

  4. Setting --> Setting-SSH,新建SSH keys 粘贴id_rsa.pub 到keys中

  5. 测试是否成功,执行命令:
    ssh -T [email protected]

(2). 关联远程仓库

  1. 本地仓库关联远程仓库
    到github上在仓库Clone or download下拉列表中找到以[email protected]:开头的仓库SSH地址并复制(因为HTTPS较慢,一般用SSH地址)
    git remote add origin 你复制的地址
    git remote add origin xxxxx

  2. 同步远程仓库

注:如果远程仓库中有文件,先拉取远程仓库内容
git pull origin master
然后合并本地代码后,推送代码到远程仓库
git push -u origin master
注:远程仓库为空时,可强制同步远程仓库
git push -u -f origin master

3.重新关联远程仓库:
git remote rm orgin //删除原远程仓库地址
git remote add group //新关联的远程仓库地址
git pull group master
git push -u group master

  1. 子模块关联的方法
    git submodule init # 初始化本地.gitmodules文件
    git submodule add <submodule_url> # 添加子项目 URL
    git submodule update # 同步远端submodule源码

上传本地子模块的方法 eg:

robot@ubuntu:~/routeMonitor/monitor_src/third_party/serial$ git reflog
cbcca7c HEAD@{
    
    0}: clone: from https://github.com/wjwwood/serial.git
robot@ubuntu:~/routeMonitor/monitor_src/third_party/serial$ cd ../../
robot@ubuntu:~/routeMonitor/monitor_src$ git submodule add https://github.com/wjwwood/serial.git
Cloning into 'serial'...
remote: Enumerating objects: 2659, done.
remote: Total 2659 (delta 0), reused 0 (delta 0), pack-reused 2659
Receiving objects: 100% (2659/2659), 1.55 MiB | 354.00 KiB/s, done.
Resolving deltas: 100% (1240/1240), done.
Checking connectivity... done.

robot@ubuntu:~/routeMonitor/monitor_src$ git status
On branch master
Your branch is up-to-date with 'DevGroup/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
	new file:   .gitmodules
	new file:   serial
  1. 删除 test 文件夹为案例
    git rm -r --cached test //–cached不会把本地的test删除
    git commit -m ‘delete test dir’
    git push -u origin master

  2. 添加文件
    git add …
    eg:git add ConfigVersionNumber.h

  3. 确认合并内容
    git commit -am “合c_with_ui.cpp文件宏定义”
    推送到服务器,提交申请
    git push origin master

  4. 提交合并申请
    web界面登录github账户,可提交当前代码申请合并到研发组。

(3).解决合并冲突

基础工具:
sudo apt install kdiff3

  1. 查看版本合并冲突内容
    git mergetool

  2. 解决合并冲突
    git difftool

  3. 确认合并
    git commit -am “合并完成”

  4. 提交本地代码
    git push origin master

(4). 创建分支及合并分支

  1. 创建分支
    git branch dev

  2. dev分支的代码达到上线的标准后,要合并到 master 分支
    git checkout dev
    git pull
    git checkout master
    git merge dev
    git push -u origin master

  3. 当master代码改动,需要更新开发分支(dev)上的代码
    git checkout master
    git pull
    git checkout dev
    git merge master
    git push -u origin dev

(5). 回滚特定版本

1). 在git工作路径,git reflog 查看本地,git log查看远程。
2). 查看指定历史版本 tree 的 SHA; 
3). 指定远程 commit 值
			$ git checkout 4dc7ce33fab01e8ff1858968062c6d57bf2e7571
			
			/*
			Note: checking out '4dc7ce33fab01e8ff1858968062c6d57bf2e7571'.
			You are in 'detached HEAD' state. You can look around, make experimental
			changes and commit them, and you can discard any commits you make in this
			state without impacting any branches by performing another checkout.			
			
			If you want to create a new branch to retain commits you create, you may
			do so (now or later) by using -b with the checkout command again. Example:
			git checkout -b <new-branch-name>
			*/	
			
			$ git branch
			* (HEAD detached at 4dc7ce3)
			  brain_8.10
			  dev
			  master
4). 把此分支check到 此分支上
		git checkout -b brain_8_9 

5). //把此分支推到服务器上,服务器上就出现版本的分支代码 
		git push origin brain_8_9 

6).切换至另一文件夹内,下载分支代码。
$ git clone -b brain_8_9 ssh://git@git.keyi.lan:10022/lijiabo/cellrobot2_app.git
				Cloning into 'cellrobot2_brain_app'...
				remote: Enumerating objects: 1913, done.
				remote: Counting objects: 100% (1913/1913), done.
				remote: Compressing objects: 100% (1456/1456), done.
				remote: Total 1913 (delta 1460), reused 578 (delta 451)
				Receiving objects: 100% (1913/1913), 556.19 KiB | 7.32 MiB/s, done.
				Resolving deltas: 100% (1460/1460), done.
$ ls
	cellrobot2_app
$ git branch
   * dev
	master

Git使用说明文档
https://git-scm.com/book/zh/v2

猜你喜欢

转载自blog.csdn.net/weixin_38387929/article/details/112789914