git

git to use.

0.git schematic graph.

flowchart RL; id1[(Remote)]--fetch/clone-->id2[(Repository)] id2--push-->id1 id2--checkout-->id3 id1--pull-->id3 subgraph workspacetion id3(Workspace) id5(coding/edit) end id3--add-->id4[(index/stage)] id4--commit-->id2

1.system to configure.

  • windows---Git Bash.
  • linux---terminal.

00.配置用户名邮箱.

# 查看配置
git config --global --list
# 全局配置
git config --global user.name "user_name"
git config --global user.email  "user_primary_email"
# 全局配置取消
git config --global --unset user.name "user_name"
git config --global --unset user.email "user_primary_email"

01.配置公钥.

  • 系统配置
sudo apt-get install openssh-server
mkdir .ssh
cd .ssh
ssh-keygen -t username_azuregit_rsa
# 生成多个公钥key
ssh-keygen -t rsa -f ~/.ssh/username_rsa.github -C "user_primary_email"
ssh-keygen -t rsa -f ~/.ssh/username_rsa.gitee -C "user_primary_email"
ssh-keygen -t rsa -f ~/.ssh/username_rsa.gitlab -C "user_primary_email"
ssh-keygen -t rsa -f ~/.ssh/username_rsa.gitcode -C "user_primary_email"
ssh-keygen -t rsa -f ~/.ssh/username_rsa  # 不加email的话,需要passwd.
# 查看公钥
cat username_azuregit_rsa.pub
cat username_rsa.github.pub
cat username_rsa.gitee.pub
cat username_rsa.gitlab.pub
cat username_rsa.gitcode.pub

# 此处以gitlab为例
copy key into Login--user--settings--ssh keys--add key
cd .ssh
vim config  
# 此处可配置多个ssh key
# 将下面配置添加到config中:
# azure company
Host azuregit
         User azuregitolite
         HostName 221.4.216.35
         Port 36822
         Identityfile ~/.ssh/username_azuregit_rsa
#gitee
Host gitee.com
         User git
         HostName gitee.coms
         Port port_number
         Identityfile ~/.ssh/username_rsa.gitee
# gitlab
Host gitlab.com
         User git
         HostName gitlab.com
         Port port_number
         IdentityFile ~/.ssh/username_rsa.gitlab
# github
Host github.com
         User git
         HostName github.com
         Port port_number
         IdentityFile ~/.ssh/username_rsa.github
# gitcode
Host gitcode.net
         User git
         HostName gitcode.net
         Port port_number
         IdentityFile ~/.ssh/username_rsa.gitcode
# 参数解析
# Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件.
# HostName : 要登录主机的主机名ip.
# User : 登录名.
# Port : 端口.
# IdentityFile : 指明上面User对应的identityFile路径.
  • 测试连接结果
ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T git@gitee.com
  • 项目配置
mkdir folder_name
cd folder_name
git clone ssh://git@gitee.com/kuang-hongliang/khl_project.git
# 指定clone分支
git clone -b branch_name ssh://git@gitee.com/kuang-hongliang/khl_project.git
la
vim .git/config
# 添加以下内容
[core]
        repositoryformatversion = 0        
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://git@github.com/kuanghl/khl_project.git
        url = ssh://git@gitee.com/kuang-hongliang/khl_project.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        # 此处可以添加多个url,同时通过ssh推送多个仓库,但只有第一个url可以fetch or pull 
[user]
        # 解决push需要输入user.name 和 user.email问题
        name = kuanghl
        email = kuanghl1998@163.com
[credential]
        # 解决远端remote not found问题
        helper = store
  • git本地仓库与远程仓库链接同步
# 网页端新建仓库,本地保存提交代码,链接到远端新建仓库
git init
git add .
git commit -m 'git branch init'
git remote add origin https://gitee.com/kuang-hongliang/embeded_os.git
# 设置远程分支
git branch --set-upstream-to=origin/master
# 允许合并到本地分支
git pull origin master --allow-unrelated-histories
# 推送到远端
git push -u origin master

2.git command and using process.

00.本地仓库初始化.

将这个目录当成自己本地仓库。

git init

01.clone project or merge code.

# case 0 new
git clone shh_address/http_address

# case 1 new merge old
# case 00 新建临时分支branch_name,进行拉取远端分支base_branch到branch_name, 
比对本地master分支与刚拉取分支的差异,合并拉取分支到本地分支中去,删除branch_name.
git branch branch_name
git fetch origin base_branch : branch_name
git diff branch_name
git merge branch_name
git branch -d branch_name
# case 01 拉取远端base_branch,比对其与本地master分支的差异,将远端分支合并到本地分支中去.
git fetch origin base_branch
git log -p branch_name..origin/base_branch
git merge origin/base_branch
# case 02 直接拉取远端分支base_branch合并到本地分支中去.
git pull origin base_branch
# case 03 暂存本地冲突修改比对远端base_branch
git stash
git pull origin master
git stash pop

02.create or delete branch.

# case0 新建并切换到branch_name分支
git checkout -b branch_name
# case1 基于远端base_branch创建并切换到新分支branch_name
git checkout -b branch_name origin/base_branch
# case2 两步走
git branch branch_name 
git checkout branch_name

# 新建本地分支推送到远端,然后将本地分支与远端分支关联,查看远端分支
# 关联则是以后的push/pull/fetch操作都是对远端分支branch_name进行
git checkout -b branch_name
git push origin branch_name 
git branch --set-upstream-to=origin/branch_name  
git branch -a  

# 删除本地分支
git branch -d branch_name
# 删除远端分支
git push origin --delete branch_name

03.enter workspace.

# 开始编写和修改代码
code .
vim file_name
...and so on

04.code compare and commit.

# 将所有修改代码暂存,提交,推送到远程分支base_branch
git add .
git commit -a -m 'notes'
git push
or git push origin base_branch
or git push origin branch_name : branch_name 

05.recovering disaster accidental code.

# case0查看版本及回退
git log
git reset --hard HEAD^   注:***(^个数代表回退版本个数)
or git reset --hard HEAD~3   注:***(回退到前3个版本)
or git reset --hard HEAD id  注:***(回退到指定id版本) 
# case1撤销文件修改
git restore file_name  注:***(对已git add文件使用无效)
git restore --staged file_name  注:***(从暂存区撤出文件,但保留其更改)

06.check branch information.

# 查看分支来源
git remote -v
# 查看远端分支
git branch -r
git branch -a
# 查看当前分支版本信息
git log
# 查看当前分支状态
git status

07.tag.

# 基于当前分支创建tag
git tag v1.0
# 查看tag
git tag
# 发布所有release tag
git push origin --tags
# 查看tag信息
git show v1.0
# 删除本地tag
git tag -d v1.0
# 删除远端tag
git push origin :refs/tags/v1.0
# 基于tag创建分支
git checkout -b branch_name-v1.0 v1.0
# coding
# 切换到master分支并合并到master分支,删除分支
git checkout master
git merge branch_name-v1.0
git branch -d branch_name-v1.0

3.git for development collaboration.

00.develop coarse drawing.

gitGraph commit commit branch kuanghl1 branch kuanghl2 branch kuanghl3 checkout kuanghl1 commit checkout main commit merge kuanghl1 checkout kuanghl2 commit commit checkout main commit commit checkout kuanghl3 commit commit checkout main merge kuanghl2 merge kuanghl3 commit

4.git版本控制及发布.

posted @ 2023-01-20 16:03  天纵之才  阅读(50)  评论(0)    收藏  举报