git 常用命令

笔记:

git status

cd dataflow-etl-module/

git status

git add .

Git commit -am ‘’

git push origin HEAD:master

git status

cd ..

git status

Git add .

Git commit -am ‘’

git push

git pull

git submodule update --remote

git status



拉取子模块

git submodule updata --init --recursive    下拉子模块



建立远程连接:

1.设置用户名和邮箱(--global 为全局参数,表明本地所有Git仓库都会使用这个配置)

git config --global user.name "yourname"

git config --global user.email "your_email@youremail.com"

非全局配置: 

    git config user.name "yourname"

  git config user.email "your_email@youremail.com"

2.生成密钥(SSH key)

ssh-keygen -t rsa -C "your_email@youremail.com"

3、将上一步骤生成的密钥即.ssh/id_rsa.pub中内容全部复制。配置在gitserver 用户设置->SSH密钥

参考:Git- 连接远程仓库 - zeotoone - 博客园

一、本地操作:

1.其它

git init:初始化本地库

git status:查看工作区、暂存区的状态

git add <file name>:将工作区的“新建/修改”添加到暂存区

git rm --cached <file name>:移除暂存区的修改

git commit <file name>:将暂存区的内容提交到本地库

  tip:需要再编辑提交日志,比较麻烦,建议用下面带参数的提交方法

git commit -m "提交日志" <file name>:文件从暂存区到本地库

git config --list   查看配置

2.日志

git log:查看历史提交

  tip:空格向下翻页,b向上翻页,q退出

git log --pretty=oneline:以漂亮的一行显示,包含全部哈希索引值

git log --oneline:以简洁的一行显示,包含简洁哈希索引值

git reflog:以简洁的一行显示,包含简洁哈希索引值,同时显示移动到某个历史版本所需的步数

3.版本控制

git reset --hard 简洁/完整哈希索引值:回到指定哈希值所对应的版本

git reset --hard HEAD:强制工作区、暂存区、本地库为当前HEAD指针所在的版本

git reset --hard HEAD^:后退一个版本  

  tip:一个^表示回退一个版本

git reset --hard HEAD~1:后退一个版本

  tip:波浪线~后面的数字表示后退几个版本

4.比较差异

git diff:比较工作区和暂存区的所有文件差异

git diff <file name>:比较工作区和暂存区的指定文件的差异

git diff HEAD|HEAD^|HEAD~|哈希索引值 <file name>:比较工作区跟本地库的某个版本的指定文件的差异

5.分支操作

git branch : 查看当前所在分支

git branch -a:查看所有分支

git branch -D <分支名>:(先跳出要删除的分支后才能)删除本地分支

git push origin --delete <分支名>:删除远程分支

git branch <分支名>:新建分支

git push origin <新建分支名称> :上传将本地新建分支

git push origin <远程分支名> -u : 上传本地新建分支到远程(

git push origin dev -u   这个意思是把本地dev push到origin的dev -u表示同时建立关联,以后再推送到远程只需git push origin

git checkout <分支名>:切换分支

git merge <被合并分支名>:合并分支

  tip:如master分支合并 hot_fix分支,那么当前必须处于master分支上,然后执行 git merge hot_fix 命令

  tip2:合并出现冲突

    ①删除git自动标记符号,如<<<<<<< HEAD、>>>>>>>等

    ②修改到满意后,保存退出

    ③git add <file name>

    ④git commit -m "日志信息",此时后面不要带文件名

6、git submodule

http://192.168.152.10:19009/matedata/scheduler/taskBatch/read

二、本地库跟远程库交互:

git clone <远程库地址>:克隆远程库

  功能:①完整的克隆远程库为本地库,②为本地库新建origin别名,③初始化本地库

git remote -v:查看远程库地址别名

git remote add <别名> <远程库地址>:新建远程库地址别名

提交代码到gitee: git remote add origin 远程项目的Https地址

git remote rm <别名>:删除本地中远程库别名

git push <别名> <分支名>:本地库某个分支推送到远程库,分支必须指定

git push -u origin master -f   #-f强制上传

git pull <别名> <分支名>:把远程库的修改拉取到本地

  tip:该命令包括git fetch,git merge

git fetch <远程库别名> <远程库分支名>:抓取远程库的指定分支到本地,但没有合并

git merge <远程库别名/远程库分支名>:将抓取下来的远程的分支,跟当前所在分支进行合并

git fork:复制远程库

  tip:一般是外面团队的开发人员fork本团队项目,然后进行开发,之后外面团队发起pull request,然后本团队进行审核,如无问题本团队进行merge(合并)到团队自己的远程库,整个流程就是本团队跟外面团队的协同开发流程,Linux的团队开发成员即为这种工作方式。

 git reset --hard  ***(版本号) 回退版本

常用操作记录:

-----------------------上传文件到空白项目--------------------
     cd yearbooks
     git init
     git remote add origin git@192.168.111.111:xx/doc.git    (git@192.168.111.111:xx/doc.git 根据实际自己定义)
     git add .
     git commit -m "xxxxxx"
     git push -u origin master

---------------推送现有文件夹

cd existing_folder
git init
git remote add origin git@192.168.110.570:f-cr/ut-mod.git
git add .
git commit -m "Initial commit"
git push -u origin master

----------------已有项目下载后更新,然后上传--------------
     git clone git@192.168.1.1:xx/doc.git
     cd doc/
     #####更新文件##############
     git add .
     git commit -am 'change xxxx'
     git push

-----------------新建分支并上传文件----------------

     git clone git@192.168.1.1:xx/xx-xx.git
     cd xx/
     git branch -a
     git branch 20200430
     git checkout 20200430
     git commit -am '20200430 update xx'
     git push origin 20200430 -u

-----------------删除分支--------------------------
     ###本地删除
     git branch -D 20200430
     ###远程删除
     git push origin --delete 20200430

---------在安装了git以后发现idea类名出现了不同的颜色,如下:-------

它们分别表示的含义:

绿色,已经加入控制暂未提交
红色,未加入版本控制
蓝色,加入,已提交,有改动
白色,加入,已提交,无改动
灰色:版本控制已忽略文件。

--------上传代码到gitee----------------

cd 项目所在目录

git init

git add 文件名(. 表示当前目录所有内容)

git commit -m "注释的内容"   #提交到本地仓库

提交代码到gitee: git remote add origin 远程项目的Https地址

git push -u origin master -f   #-f强制上传

弹出帐号密码框时:输入账号密码

-----------拉取远程更新本地------

cd 项目所在目录

git pull

-------------------------------------

分支下载:

git clone xxx

cd xxx

git checkout dev

========================================================================================

Git master branch has no upstream branch的解决

具体原因: 出现这种情况主要是由于远程仓库太多,且分支较多。在默认情况下,git push时一般会上传到origin下的master分支上,然而当repositorybranch过多,而又没有设置关联时,git就会产生疑问,因为它无法判断你的push目标。

Git 的 “master” 分支并不是一个特殊分支。 它就跟其它分支完全没有区别。 之所以几乎每一个仓库都有 master 分支,是因为git init命令默认创建它,并且大多数人都懒得去改动它

远程仓库名字 “origin” 与分支名字 “master” 一样,在 Git 中并没有任何特别的含义一样。origin” 是当你运行git clone时默认的远程仓库名字。 如果你运行 git clone -o booyah,那么你默认的远程分支名字将会是 booyah/master。

git push origin dev -u   这个意思是把本地dev push到origin的dev -u表示同时建立关联,以后再推送到远程只需git push origin

推荐大家一个好用的git分支管理工具SourceTree,如下图所示可以直接从远程拉取分支到本地,并且可视化对分支进行操作

提示出错信息:fatal: remote origin already exists.

1、先输入$ git remote rm origin

2、再输入$ git remote add origin git@192.168.1.1:xxx.git 就不会报错了!

ssh免密设置后,仍提示输入密码,报错如下:

$ ssh -vT git@192.168.1.1
OpenSSH_8.2p1, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to 192.168.1.1 [192.168.1.1] port 22.
debug1: Connection established.
debug1: identity file /c/Users/xxx/.ssh/id_rsa type 0
debug1: identity file /c/Users/xxx/.ssh/id_rsa-cert type -1
debug1: identity file /c/Users/xxx/.ssh/id_dsa type -1
debug1: identity file /c/Users/xxx/.ssh/id_dsa-cert type -1
debug1: identity file /c/Users/xxx/.ssh/id_ecdsa type -1
debug1: identity file /c/Users/xxx/.ssh/id_ecdsa-cert type -1
debug1: identity file /c/Users/xxx/.ssh/id_ecdsa_sk type -1
debug1: identity file /c/Users/xxx/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /c/Users/xxx/.ssh/id_ed25519 type -1
debug1: identity file /c/Users/xxx/.ssh/id_ed25519-cert type -1
debug1: identity file /c/Users/xxx/.ssh/id_ed25519_sk type -1
debug1: identity file /c/Users/xxx/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /c/Users/xxx/.ssh/id_xmss type -1
debug1: identity file /c/Users/xxx/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug1: Authenticating to 192.168.1.1:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:lf0RvfYICh0bL3B6tRIKy7ZeMChslcUPk5szuki0pCA
debug1: Host '192.168.1.1' is known and matches the ECDSA host key.
debug1: Found key in /c/Users/xxx/.ssh/known_hosts:3
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /c/Users/xxx/.ssh/id_rsa RSA SHA256:ukv3UkX9RQXkmhq6MGbWg2IBTh1rBCLr+0pbyUQ2sbw
debug1: Will attempt key: /c/Users/xxx/.ssh/id_dsa
debug1: Will attempt key: /c/Users/xxx/.ssh/id_ecdsa
debug1: Will attempt key: /c/Users/xxx/.ssh/id_ecdsa_sk
debug1: Will attempt key: /c/Users/xxx/.ssh/id_ed25519
debug1: Will attempt key: /c/Users/xxx/.ssh/id_ed25519_sk
debug1: Will attempt key: /c/Users/xxx/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Offering public key: /c/Users/xxx/.ssh/id_rsa RSA SHA256:ukv3UkX9RQXkmhq6MGbWg2IBTh1rBCLr+0pbyUQ2sbw
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /c/Users/xxx/.ssh/id_dsa
debug1: Trying private key: /c/Users/xxx/.ssh/id_ecdsa
debug1: Trying private key: /c/Users/xxx/.ssh/id_ecdsa_sk
debug1: Trying private key: /c/Users/xxx/.ssh/id_ed25519
debug1: Trying private key: /c/Users/xxx/.ssh/id_ed25519_sk
debug1: Trying private key: /c/Users/xxx/.ssh/id_xmss
debug1: Next authentication method: password
解决办法:关闭git服务器的SeLinux

参考:ssh免密设置后,仍提示输入密码,报错如下:_u012026446的专栏-CSDN博客

fatal: could not create work tree dir ‘xxxx’: Permission denied

跳出删除文件,然后重建

Git 之 恢复修改的文件 ( Git 之 恢复修改的文件 - 刘[小]倩 - 博客园  git checkout 撤销多个文件,撤销整个文件夹 - ibingshan - 博客园

对于恢复修改的文件,就是将文件从仓库中拉到本地工作区,即 仓库区 ----> 暂存区 ----> 工作区。

对于修改的文件有两种情况:

  • 只是修改了文件,没有任何 git 操作
  • 修改了文件,并提交到暂存区(即编辑之后,gitadd但没有gitadd但没有 git commit -m ....)
  • 修改了文件,并提交到仓库区(即编辑之后,gitadd和gitadd和 git commit -m ....)

情况I:

只是修改了文件,没有任何 git 操作,直接一个命令就可回退:

$ git checkout -- aaa.txt # aaa.txt为文件名

   git checkout <folder-name>/    回退整个文件

  git checkout -- <folder-name>

回滚子模块的特定提交

如果您只想回滚Git子模块的特定提交,可以使用以下命令:

$ cd path/to/submodule
$ git checkout <commit_sha>

git submodule 使用小结

git submodule 使用小结 - mouseleo - 博客园

Git Submodule 允许一个git仓库,作为另一个git仓库的子目录,并且保持父项目和子项目相互独立。

#常用指令
git clone <repository> --recursive 递归的方式克隆整个项目
git submodule add <repository> <path> 添加子模块
git submodule init 初始化子模块
git submodule update --init --recursive  递归初始化子模块
git submodule update 或者 git submodule update --remote  更新子模块为远程项目的最新版本
git submodule foreach git pull 拉取所有子模块
git submodule  #查看子模块

git submodule 子模块删除/添加 - 简书 (jianshu.com)

1. 添加子模块

git clone git@github.com:haojiehappy/my_learning.git
cd my_learning/
git submodule add git@github.com:haojiehappy/test.git
git commit -am 'add auth-module'
git push

.gitmodules 中存放的为子模块的信息

$ git submodule add git@192.168.1.1:t-v2/a-module.git
A git directory for 'a-module' is found locally with remote(s):
  origin        git@192.168.1.1:b-module/a-module.git
If you want to reuse this local git directory instead of cloning again from
  git@192.168.1.1:t-v2/a-module.git
use the '--force' option. If the local git directory is not the correct repo
or you are unsure what this means choose another name with the '--name' option.

本地仓库中存在这个子模块,删除掉就可以增加
cd /.git/modules
rm -rf a-module


2.删除子模块

根据官方文档的说明,应该使用 git submodule deinit 命令卸载一个子模块。这个命令如果添加上参数 --force,则子模块工作区内即使有本地的修改,也会被移除。
git submodule deinit test
git rm test

3.项目中修改子模块
# 进入子模块进行修改提交
cd project/moduleA
git branch
echo "This is a submodule." > b.txt
git add .
git commit -m "add b.txt"
git push origin master
# 进入项目进行提交
cd ..
git status
git diff
git add .
git commit -m "update submodule add b.txt"
git push origin master
cd ..
4.克隆带子模块的项目

方法一,先clone父项目,再初始化submodule,最后更新submodule,初始化只需要做一次,之后每次只需要直接update就可以了,需要注意submodule默认是不在任何分支上的,它指向父项目存储的submodule commit id。

git clone project.git project
cd project
git submodule init
git submodule update
cd ..
方法二,采用递归参数--recursive,需要注意同样submodule默认是不在任何分支上的,它指向父项目存储的submodule commit id。

git clone project.git project --recursive

5、项目迁移子模块处理

删除 .git 文件

删除 .gitmodules 文件

添加子模块 

  git clone git@192.168.1.1:tob-v2/df.git
  cd df
  rm -rf ./.git
  rm -rf auth-module/
  rm -rf df-etl-module/
  rm -rf tob-base-module/
  rm -rf utils-module/
  rm -rf ./.gitmodules
  git init
  git remote add origin git@192.168.1.1:ftz/df.git
  git add .
  git commit -am 'init df'
  git push -u origin master
  git submodule
  git submodule add git@192.168.1.1:ftz/auth-module.git
  git submodule add git@192.168.1.1:ftz/df-etl-module.git
  git submodule add git@192.168.1.1:ftz/crebase-module.git
  git submodule add git@192.168.1.1:base-module/utils-module.git
  git submodule
  vi .gitmodules
  git status
  git commit -am 'add submodule'
  git push


https://blog.csdn.net/hjliu_happy/article/details/118557756

git中remotes/origin/HEAD指向的分支丢失
https://blog.csdn.net/anci5794/article/details/101399065

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python Git常用命令大全: Git是现代化的版本控制系统,常被用于软件开发,协作和管理。它允许在开发过程中创建和管理不同的版本,跟踪文件的更改,以及支持团队合作。Python则是一种广泛应用于开发Web应用程序以及数据科学和人工智能领域的高级编程语言。在使用Git时,Python的代码可以与Git进行集成。这里是Python Git常用命令的大全: 1. git init:初始化一个新的 Git 仓库。 2. git clone:从现有的 Git 仓库克隆项目,可以是本地仓库或远端仓库。 3. git add:将文件添加到 Git 仓库中。git add . 可以添加所有更改。 4. git commit:将所有已添加的文件提交到本地 Git 仓库中。 5. git status:查看当前工作目录中 Git 仓库的状态。 6. git log:查看提交记录。 7. git push:将本地 Git 仓库的更改推送到远端仓库。 8. git pull:将远端 Git 仓库的更改拉到本地仓库。 9. git branch:创建新的分支。 10. git checkout:切换分支。 11. git merge:将一个分支的更改合并到另一个分支。 12. git revert:撤销一个提交。 13. git rebase:将一个分支的修改合并到当前分支。 14. git config:配置 Git。 15. git remote:管理远端仓库。 这是Python Git常用命令的大部分命令,但这并不是全部。在使用Git和Python时,这些命令应该是最为重要的。无论是在个人项目中还是团队合作中,这些命令会让你更加高效地使用Git,并保护你的代码免遭不可挽回地灾难。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值