git 分享

git stash
git stash list
git stash apply stash@{2}

常用命令

1. 配置

git --version

git config --list

git config --global user.name "first name"

git config --global user.email "javalive09@163.com"

git config color.ui true


 demo/ .git/config

git config -e


/home/peter/.gitconfig
git config -e --global


/etc/gitconfig
git config -e --system

ssh-keygen -t rsa -C "javalive09@163.com"


clip < ~/.ssh/id_rsa.pub

ssh -T git@github.com

git touch ~/.ssh/config
#GitHub
Host github.com
  User git
  HostName github.com
  IdentityFile ~/.ssh/id_rsa

#Maxthon 
Host mgit  
  User git    
  HostName mgit.maxthon.net
  Port 22    
  IdentityFile ~/.ssh/id_rsa

#Bitbucket
Host bitbucket.org
 User git
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_rsa




2. 创建仓库

git init


3. 添加到缓存区

git add -A


4. 提交代码

git commit -m "提交代码信息"

修改提交信息

git commit --amend



5. 添加远程仓库

git remote -v
git remote add origin https://github.com/javalive09/CodeBag.git

删除远程仓库

git push origin --delete <branchName>



6. 推送到远程仓库

git push origin master


7. 从远程仓库clone

git clone https://github.com/javalive09/CodeBag.git



8. 从远程仓库拉取

git pull

pull = fetch + merge



9. 还原单个文件到HEAD

git checkout -- /Users/peter/git/xui/xxx.java

git checkout HEAD /Users/peter/git/xui/XuiPSdk/libs/armeabi/libgetuiext.so



9.1 还原单个文件的某个版本

git checkout <commit> <filepath>

git checkout 45b92e3 /Users/peter/git/xui/CarClient/assets/video/video.mp4



9.2 强制切换分支 (放弃本地修改切换)

git checkout develop -f




10. 还原整个项目到master

git reset --hard master


还原到HEAD

git reset --hard HEAD

还原到HEAD上次提交

git reset –hard HEAD^




11. 查看仓库状态

git status


12. 查看历史

git log

git log --graph
git log --oneline
git log --oneline --graph




13.  tag 标签

git tag 
git tag -l
git show 1.0
git tag -a v1.0 -m "新加tag"
git tag -d v1.0
git push origin --tags
git push origin :refs/tags/<tag name>



14. 分支

git branch
git branch abc
git checkout abc
git push origin abc
git push origin :abc

git checkout -b abc



列出所有分支(本地+远程)

git branch -a



创建并且切换分支

git checkout -b abc



删除本地分支

git branch -d <branch name>


修改分支名字

git branch -m release_v1.24 release_v1.0.0


15. 分支切换时没提交 想暂存数据


暂存

git stash
暂存列表
git stash list

暂存使用

git stash apply stash@{2}



16.忽略已经提交的文件

在 .gitignore 文件中加入这个文件夹

git rm --cache path

提交


删除版本库中的文件

git rm path

修改版本库中的文件名

git mv welcome.txt README



17.git 恢复丢失的分支

git reflog show


git branch recover_branch commitid 

可以找回


18.git 对比文件


对比(工作区和上次提交)的区别

git diff

对比(工作区某个文件和上次提交的这个文件)的区别

git diff filename


对比(缓存区和上次提交)的区别

git diff --cached

对比(缓存区某个文件和上次提交的这个文件)的区别

git diff –cached filename


对比(某两个版本)的区别

git diff ffd98b291e0caa6c33575c1ef465eae661ce40c9 b8e7b00c02b95b320f14b625663fdecf2d63e74c

对比(某两个版本文件)的区别

git diff ffd98b291e0caa6c33575c1ef465eae661ce40c9:filename b8e7b00c02b95b320f14b625663fdecf2d63e74c:filename 

19. git 归档

基于最新提交建立归档文件latest.zip。

git archive -o latest.zip HEAD


只将目录src和doc建立到归档partial.tar中。

git archive -o partial.tar HEAD src doc

基于里程碑v1.0建立归档,并且为归档中文件添加目录前缀1.0。

$ git archive --format=tar --prefix=1.0/ v1.0 | gzip > foo-1.0.tar.gz



一些使用场景


1. 合并和冲突解决

      D---E test
     /
A---B---C---F master


a. 在master中执行 merge:

      D--------E
     /          \
A---B---C---F----G   test, master


b. 在master中执行 rebase:

A---B---D---E---C'---F'   test, master



c. 在master中执行 cherry-pick:

A---B---C---F---D'----E'  master


d.三方合并(Three-way merge)

100  --------101

   |

   |------102


1. 以父节点做为判断标准

2. 101,102 有不同的文件的改变,留下这些改变。

3. 101,102 有相同的文件的改变,

1)如果改变相同,留下这些改变。

2)如果改变不同,提示冲突让用户去解决。


2. 回滚

a 没有push到remote 仓库的回滚

git reset --hard commitid

b 已经push到remote仓库的回滚

git revert commitid

c.定位并回滚到引入bug的版本

git bisect start
git bisect bad
git bisect good commtid
git bisect bad
...
git bisect good
git bisect reset


3. 当前工作被打断

a 保存工作进度

git stash
git stash list
git stash apply stash@{2}

b 补充提交

git commit --amend 


4.挽救误操作

分支丢失

git reflog show


git 内部原理 及 .git 文件夹结构

git-stage 暂存区

git-checkout 检出

git-diff 对比差异

git-reset 重置

git-repos-detail 版本库

.git 文件夹结构





github使用 及使用github搭建免费博客





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值