git 命令

  • origin/master master origin区别:
    1)two branches:
    master is local branch
    origin/master is remote branch( which is a local copy of the branch named “master” on the remote named “origin”)
    2)one remote:
    origin is a remote repo
    reference:https://stackoverflow.com/questions/18137175/in-git-what-is-the-difference-between-origin-master-vs-origin-master
    - origin/HEAD
    The origin/HEAD reference is optional. It only acts as a syntactic shortcut: If it exists and points to origin/master, you can use specific simply origin where you would otherwise specify origin/master.
    The git remote(1) man page describes this:
    > set-head
    > Sets or deletes the default branch (i.e. the target of the symbolic-ref refs/remotes//HEAD) for the named remote. Having a
    > default branch for a remote is not required, but allows the name of
    > the remote to be specified in lieu of a specific branch. For example,
    > if the default branch for origin is set to master, then origin may be
    > specified wherever you would normally specify origin/master.

      	`git remote set-head origin -d` to delete the origin/HEAD symbolic ref
      	`git remote set-head origin -a` to query the remote and automatically set the origin/HEAD pointer to the remote's current branch.
      		reference: https://stackoverflow.com/questions/12613793/why-is-there-a-remotes-origin-head-origin-master-entry-in-my-git-branch-l
    
  • delete remote branch

    git branch -r -d remote-repo/remote-branch
    git push remote-repo :remote-branch
    

    第一条命令:删除本地记录的[remote-branch]
    第二条命令:向remote repo推送空的分支到[remote-branch],即删除remote-repo中的[remote-branch]
    reference:https://blog.csdn.net/furzoom/article/details/53002699

  • rebase 交互:git rebase -i HEAD~x

  • 合并2个commit:

    1. git rebase -i HEAD~2
    2. 修改pick为squash
    3. 如果有冲突:解决冲突,git add, git rebase --continue
    4. git push -f
    

    reference: https://blog.csdn.net/itfootball/article/details/44154121

  • 修改之前的commit

    1. git rebase --interactive '64e37ee^'
    2. 修改 'pick' with 'edit'. Save & Quit from commit message editor
    3. Make changes, add/rm the files and run git commit --amend
    4. Run git rebase --continue
    5. If you instead want to abort the process after starting rebase, run git rebase --abort
    
  • git reset
    reference:https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E7%BD%AE%E6%8F%AD%E5%AF%86

  • git stash pop出现CONFLICT
    git add 解决冲突后,用git reset HEAD恢复
    由于git stash 没有被删除,用git stash drop stash@{n}删除该stash
    reference: https://blog.csdn.net/jy692405180/article/details/78520251

  • git comit出现fatal: could not read ‘/localview/build/git/git-commit-template’: No such file or directory
    查看commit.template git config --get commit.template,发现确实是这个文件,并且这个文件确实不存在,使用git config commit.template build/git/git-commit-template 指定到存在的template文件

  • git: list new files only

    git diff --name-only --diff-filter=A --cached # All new files in the index  
    git diff --name-only --diff-filter=A          # All files that are not staged  
    git diff --name-only --diff-filter=A HEAD     # All new files not yet committed
    

    reference:https://stackoverflow.com/questions/9000163/git-list-new-files-only

  • 出现:git fetch refusing to fetch into current branch non-bare repo

    在当前分支下fetch 。git checkout 到其他分支,再进行fetch即可。 顺便提及,Non-bare
    repository即可看做是除掉工作区后剩下的.git文件夹,也就是裸仓库没有如git
    init同目录下的工作区文件,只有记录git版本控制相关的文件。通过git init –bare
    repo创建repo.git文件夹,文件夹下即使原.git文件夹下类似的文件。
    一般远程仓都是bare仓库。如果远程仓库不是裸仓库,则如果有人在master上工作,则大家就无法在该分支上push,pull。由于远程、本地是平级关系,可以反过来看:也无法将一个远程分支fetch到当前有人工作的目录(就是在执行fetch操作的自己)。

    reference:https://blog.csdn.net/Kevin_cc98/article/details/78311098

  • git patch
    1)生成patch

    • 两个commit之间的修改(包含两个commit):
      git format-patch <commit1>...<commit2> [-o outputpath]
      
    • 单个commit:
      git format-patch -1 <commit1>
      
    • 从某个commit以来的修改(不包含该commit):
      git format-patch <commit1>
      

    2)应用patch

    • 检查patch文件

      git apply --stat 0001-fix.patch
      
    • 查看是否可以应用成功

      git apply --check 0001-fix.patch
      
    • 应用patch

      git am 0001-fix.patch
      
    • 解决冲突

      git am --abort
      git apply --reject <patch_name> 
      //发生冲突的部分会保存为.rej文件(例如发生冲突的文件是a.txt,那么运行完这个命令后,发生conflict的部分会保存为a.txt.rej)
      根据.rej文件,编辑patch文件来解决冲突
      git am --abort //废弃patch
      git am <修改后的patch>
      

      方案一(个人推荐):
      (1) 根据git am失败的信息,找到发生冲突的具体patch文件,然后用命令git apply --reject
      <patch_name>,强行打这个patch,发生冲突的部分会保存为.rej文件(例如发生冲突的文件是a.txt,那么运行完这个命令后,发生conflict的部分会保存为a.txt.rej),未发生冲突的部分会成功打上patch

      (2) 根据.rej文件,通过编辑该patch文件的方式解决冲突。

      (3) 废弃上一条am命令已经打了的patch:git am --abort

      (4) 重新打patch:git am ~/patch-set/*.patchpatch

      方案二:
      (1) 根据git am失败的信息,找到发生冲突的具体patch文件,然后用命令git apply --reject
      <patch_name>,强行打这个patch,发生冲突的部分会保存为.rej文件(例如发生冲突的文件是a.txt,那么运行完这个命令后,发生conflict的部分会保存为a.txt.rej),未发生冲突的部分会成功打上patch

      (2) 根据.rej文件,通过编辑发生冲突的code文件的方式解决冲突。

      (3) 将该patch涉及到的所有文件(不仅仅是发生冲突的文件)通过命令git add <file_name>添加到工作区中

      (4) 告诉git冲突已经解决,继续打patch: git am --resolved (git am --resolved 和 git am --continue是一样的)
      分析:方案一和方案二主要区别是解决冲突的方法不一样。方案一是通过编辑patch文件的方式解决冲突,方案二十通过编辑冲突code文件的方式解决冲突。这两种方案区别比较大:经过实验,核心区别在于,方案二无法验证冲突有没有切实的解决。即使你在方案二的第二步乱改一通,也能“打完”发生冲突的patch(并没有检测修改后的code文件跟patch期望的是否相同)。因此,如果采用方案二,那么再解决code文件冲突后,需要人工去确认修改的正确性。

      reference: https://www.jianshu.com/p/814fb6606734
      https://blog.csdn.net/qq_15936309/article/details/90521360

  • 一些命令
    git remote -v shows remote url(fetch and push) / git remote show origin show details of remote named “origin”
    git remote add xxx git@aaa.com:bbb.git names “git@aaa.com:bbb.git” as xxx, which can be showed by git remote -v and .git/config file, after add remote, do git fetch xxx to get remote branch

    git branch -a shows all the branch
    git branch -av git branch -vv
    git branch -r shows remote branch
    git branch --set-upstream=[remote-repo]/[remote-branch] set upstream git branch -u [remotes/xxx]/[branch]
    git branch -u [remote-repo]/[remote-branch]git branch --set-upstream-to [remote-repo]/[remote-branch] set upstreams for local branch
    git remote rename [origin-name] [destination-name]

    git push origin master origin指定了你要push到哪个remote; master其实是一个“refspec”,正常的“refspec”的形式为”+<src>:<dst>”,冒号前表示local branch的名字,冒号后表示remote repository下 branch的名字。注意,如果你省略了,git就认为你想push到remote repository下和local branch相同名字的branch (reference :https://blog.csdn.net/abo8888882006/article/details/12375091 )
    git fetch <remote-repo> <remote-branch>:<local-branch> fetch code from remote-branch of remote-repo to local-branch (reference: https://stackabuse.com/git-fetch-a-remote-branch/)
    git merge bar/somebranchMerge branch somebranch from the barrepository into the current branch
    git pull <remote-repo> <remote-branch> --rebase
    git pull <remote-repo> refs/pull/<pr id>/head --rebase 拉取远程的PR
    git checkout -b [local-branch] [remote-repo]/[remote-branch] create local-branch and set upstream to [remote-repo]:[remote-branch] (reference: https://stackoverflow.com/questions/9537392/git-fetch-remote-branch)
    git rebase master
    git log -L :function:file_path
    git log -p file_name: shows detailed changes of file_name in every commit
    git log git log --author="John\|Mary" --committer=Mary
    git ls-files : -m modify/ --others --exclude-standard(show untracked file) see https://git-scm.com/docs/git-ls-files
    git cherry-pick [commit id]: https://jaskey.github.io/blog/2015/12/22/git-cherry-pick/
    https://blog.csdn.net/ybdesire/article/details/42145597

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值