Git仓库管理(1)——基本操作

一、常用指令

1、基础指令

git status					//查看当前分支的状态
git diff					//查看当前分支的修改内容
git blame	test.txt		//查看test.txt这个文件的修改记录

git add test.txt			//将test.txt这个文件添加到git管理
git rm test.txt				//将test.txt这个文件从git管理中删除

git checkout -- test.txt	//撤销test.txt这个文件这个文件的修改

git stash					//将当前的修改保存起来,并且使这些修改git不可见
git stash pop				//将之前保存的修改释放并处于git可见状态

git push origin :project_FDU						//删除远程分支

git tag -d linux4.4.83-v1.0.0						//删除本地tag
git push origin  :refs/tags/linux4.4.83-v1.0.0		//删除远程tag

git clone -depth=1			//只拿到一个分支下的一个commit.对于拿下来的这个仓库,只能进行本地操作,不能进行远程推仓或更新
http://blog.chinaunix.net/uid-24467128-id-4762864.html

2、扩展指令

git log --name-only			//只显示每次提交修改的文件名称
git show --pretty="format:" --name-only 73167b96			//只显示某次提交修改的文件名称
为project_FDU分支设置跟踪信息,追踪远程的origin/project_FDU分支
git branch --set-upstream-to=origin/project_FDU project_FDU
把一个分支上的某次修改的提交记录合并到另外一个分支上:
git cherry-pick 73167b96
# 修改最近提交的 commit 信息
$ git commit --amend --message="modify message" --author="junshou <junshou@163.com>"

# 仅修改 message 信息
$ git commit --amend --message="modify message"

# 仅修改 author 信息
$ git commit --amend --author="junshou <junshou@163.com>"

二、仓库操作

1、建立远程git仓库并将本地的代码提交到远程仓库

创建远程仓库容器

第一步:新建仓库文件夹,文件名添加.git后缀
    mkdir  u-boot-2016.01.git
    
第二步:进入这个仓库文件夹
    cd  u-boot-2016.01.git
    
第三步:创建初始化git仓库
    git init --bare

将本地代码加入git管理

第一步:解压u-boot-2016.01代码
    tar xvf u-boot-2016.01.tar.bz2

第二步:进入u-boot-2016.01代码文件夹,加入git管理
    cd u-boot-2016.01
    git init
    git add .

第三步:提交第一个版本,形成master分支
    git commit -a -m "base code from nexell"

将本地代码挂接到远程仓库容器中去

第一步:创建原始代码于远端个仓库关联
    git remote add origin ../u-boot-2016.01.git
    (git remote add origin ssh://zhangchu@10.2.2.142/Users/eyrh/Service.git)
	(git remote set-url origin    修改远程关联)
	  (git remote remove xx        移除名为xx的remote)
第二步:将原始代码的master分支提交到远端仓库
    git push origin master

验证远端仓库是否有效

第一步:创建一个测试文件夹,并跳进去
    mkdir test
    cd test

第二步:将远端仓库中的代码拉下来    
    git clone ../u-boot-2016.01.git
    (git clone ssh://junshou@10.2.2.142/Users/eyrh/Service.git)
    (git clone ssh://junshou@10.2.2.142:10010/home/uboot)
    (git clone git@10.2.2.142:Users/eyrh/Service.git)

3、删除git仓库中某部分记录文件

当有部分不需要git管理的文件被误添加到git仓库中,比如编译的中间文件等,使用如下命令删除管理信息:

e.g. 删除out目录的管理信息
git rm --cached out -f -r

4、Git忽略规则(.gitignore配置)

清除本地缓存

参考链接:
https://www.cnblogs.com/rainbowk/p/10932322.html

git清除本地缓存(改变成未track状态),然后再提交:

[root@kevin ~]# git rm -r --cached .
[root@kevin ~]# git add .
[root@kevin ~]# git commit -m 'update .gitignore'
[root@kevin ~]# git push -u origin master

忽略语法

# 这是注释,将被忽略
*.log       # 忽略所有.log文件
build/      # 忽略build文件夹及其内容
/test       # 忽略根目录下的test文件
!important.log  # 但是不要忽略important.log文件

5、clone仓库

克隆ssh远程仓库
命令一:git clone git@(IP):(仓库路径)
e.g. :
git clone git@192.168.0.233:jushou/work/linux-source.git
命令二:git clone ssh://(user)@(IP):(端口号)/(仓库路径)
e.g. :
git clone ssh://junshou@192.168.0.233:10000/home/jushou/work/linux-source

浅克隆

https://www.codenong.com/54213420/
git log 看到 grafted 标签时,意味着 git clone 时用了--depth参数,当前仓库是 shadow 的,即 commit 历史不完整的。
恢复完整commit
使用git fetch origin --unshallow来拉取完整 commit 历史。再次合并就可以了
将git浅层仓库转换为普通仓库
git rev-parse --verify 61c7581f36158236aabc9169f1a5aa9753273fc3 > .git/info/grafts
git filter-branch -f -- --all
git replace --convert-graft-file

三、分支操作

1、打标签

参考链接:
https://git-scm.com/book/zh/v1/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE

创建一个含附注类型的标签非常简单,用 -a (译注:取 annotated 的首字母)指定标签名字即可:

$ git tag -a project_meizheng_v1.0.0 -m 'beijingmeizheng first version'
$ git tag
v0.1
v1.3
v1.4
project_meizheng_v1.0.0

$ git push origin project_meizheng_v1.0.0

$ git ls-remote	会列出所有的分支/标签

删除本地标签:git tag -d tagName

删除远程仓库标签:git push origin :refs/tags/tagName

2、切出本地分支,合并到主分支

参考链接:
https://blog.csdn.net/sinat_39150454/article/details/77816179
切出新分支
git chechout -b <branchName>
增加新的修改并提交
git commit -m "***"
切到主分支上并更新
git checkout master
git pull
(git rebase)有可能会用到
使用如下命令合并分支到主分支
git merge <branchName>
推送到远程
git push origin master

push denied

问题

Counting objects: 19, done.
Delta compression using up to 144 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (19/19), 3.85 KiB | 3.85 MiB/s, done.
Total 19 (delta 12), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
! [remote rejected]

solve

git config receive.denyCurrentBranch ignore


修改.git/config添加如下代码:
[receive]
denyCurrentBranch = ignore

3、merge分支

    git checkout master
    git merge test-***

四、代码操作

1、打patch

git format-patch -1 												打最近的两次提交的patch 
git format-patch -n 												打最近n次提交的patch
git format-patch 736d2489d0faf6b8b287e8aeb81b39d5ca08a061 			依次打最近到73***61的每次提交的patch
git format-patch -1 4d795d26ab0a4500927328cb76fd9616c9fb5cea		将指定commit号打出来
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值