git使用

Posted on By yuanfang

初始

1.git status
2.git add hello.html
3.git commit -m “提交描述”
4.初始化设置,说明是谁提交了更新,所以会随更新内容一起被永久纳入历史记录
$ git config --global user.name "lanluohaisi"
$ git config --global user.email ceshi@example.com

如果用了 –global 选项,那么更改的配置文件就是位于你用户主目录下的那个,以后你所有的项目都会默认使用这里配置的用户信息。如果要在某个特定的项目中使用其他名字或者电邮,只要去掉 –global选项重新配置即可,新的设定保存在当前项目的.git/config 文件里。

5.设置git仓库: 在需要的目录下 git init,会生成 .git 隐藏文件夹
6.git rm hello.html 删除git库里面文件
7.设置记住密码

  • 1、先cd到根目录,执行
    git config --global credential.helper store
  • 2、执行之后会在.gitconfig文件中多加红色字体项
    [user]
            name = 天明
            email = xxxx@xxxx.com
    [credential]
            helper = store
    
  • 3、之后cd到项目目录,执行git pull命令,会提示输入账号密码。输完这一次以后就不再需要,并且会在根目录生成一个.git-credentials文件
    # cat .git-credentials https://Username:Password@git.oschina.net
  • 4、之后pull/push代码都不再需要输入账号密码了~

8.bug: the romote end hung up unexpectedly
git config --global http.postBuffer 1048576000

常用命令

1. 建分支 : git checkout -b dev  
2. 查看状态 : git status  
3. 删除分支  git branch -d dev  
4. 查看远程分支  git branch -s  
5. 查看远程仓库 git remote -v  
6. 查看分支不同  git diff master dev --stat  
7. 查看日志  git log  
8. git fetch      
9. git merge  dev  
10.git log --pretty=oneline  查看版本号  
11.git reset --hard e377f60e28c8b84158  回退版本  
git reset --hard HEAD 回退到上一个版本  
git merge --abort 取消合并  
12.git checkout B message.html message.css message.js other.js 使用git checkout 将B分支上的文件添加到当前分支上  
13.  git checkout b46ff0 -- js/legos/wxsq/jd.dealcancle.js  回退某个文件某个版本   
14. git log --oneline  --查看记录  
15. git checkout -b br-2.1.1.1  origin/br-2.1.2.1  -- 跟踪远程分支  
16. git branch -vv  --查看跟踪远程分支  
17.git branch -a  -- 查看远程分支  
18.git push origin ceshi_deal  --把分支推到远程分支  

19. 删除远程分支  
git branch -r -d origin/branch-name   
git push origin :branch-name  
或者 git push origin -d feature_order_all_chongzhi_test

20 git stash show -p stash@{1}  -- 查看stash文件  
21 追踪远程分支
1)新分支 git branch -u origin/remote_branch local_branch  
2)已有分支  git checkout -b new_branch origin/remote_branch  

22 推送本地分支local_branch到远程分支 remote_branch并建立关联关系  
    a.远程已有remote_branch分支并且已经关联本地分支local_branch且本地已经切换到local_branch
        git push
    b.远程已有remote_branch分支但未关联本地分支local_branch且本地已经切换到local_branch
        git push -u origin/remote_branch
    c.远程没有有remote_branch分支并,本地已经切换到local_branch
        git push origin local_branch:remote_branch

22.git stash
$git stash
$git stash pop

git stash list命令可以将当前的Git栈信息打印出来,你只需要将找到对应的版本号,用 git stash show stash@{1}可以查看,例如使用’git stash apply stash@{1}’就可以将你指定版本号为stash@{1}的工作取出来,当你将所有的栈都应用回来的时候,可以使用’git stash clear’来将栈清空。

如果想删除一个stash,git stash drop 删除所有stash,git stash clear 查看stash时间 git stash list --date=local

23.消除远程追踪状态缓存
git rm -r –cached .
git add .
git commit -m ‘update .gitignore’

24.git revert git revert HEAD 撤销前一次 commit
git revert HEAD^ 撤销前前一次 commit
git revert commit (比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存。
git revert是提交一个新的版本,将需要revert的版本的内容再反向修改回去,版本会递增,不影响之前提交的内容.

git revert 和 git reset的区别

  1. git revert是用一次新的commit来回滚之前的commit,git reset是直接删除指定的commit。
  2. 在回滚这一操作上看,效果差不多。但是在日后继续merge以前的老版本时有区别。因为git revert是用一次逆向的commit“中和”之前的提交,因此日后合并老的branch时,导致这部分改变不会再次出现,但是git reset是之间把某些commit在某个branch上删除,因而和老的branch再次merge时,这些被回滚的commit应该还会被引入。
  3. git reset 是把HEAD向后移动了一下,而git revert是HEAD继续前进,只是新的commit的内容和要revert的内容正好相反,能够抵消要被revert的内容。
  1. git cherry-pick
    git cherry-pick 可以理解为”挑拣”提交,它会获取某一个分支的单笔提交,并作为一个新的提交引入到你当前分支上。 当我们需要在本地合入其他分支的提交时,如果我们不想对整个分支进行合并,而是只想将某一次提交合入到本地当前分支上,那么就要使用git cherry-pick了。

git cherry-pick [<options>] <commit-ish>...

常用options: –quit 退出当前的chery-pick序列 –continue 继续当前的chery-pick序列 –abort 取消当前的chery-pick序列,恢复当前分支 -n, –no-commit 不自动提交 -e, –edit 编辑提交信息

  1. git rebase
    学习参考-这一次彻底搞懂 Git Rebase
    git checkout master
    git pull
    git checkout local
    git rebase -i HEAD~2  //合并提交 --- 2表示合并两个
    git rebase master---->解决冲突--->git rebase --continue
    git checkout master
    git merge local
    git push
    
    
  2. fork同步
    git remote -v 查看远程状态
    git fetch xxx(比如legosv5-wxa-cli)  远程分支名称
    git merge legosv5-wxa-cli/master  同步远程master