Git笔记

概念

origin

命令 git push origin 中的"origin",只是一个代号,其实可以为任何单词,比方说remote, aaa, bbb, xxx, repo等。origin指向哪个远程仓库,可以用命令git config --list --show-origin 或者 git config --list 查看(--show-origin只是为了显示这个配置来自于哪个配置文件),比如:

1 PS C:\Users\Jing.He\internal-audit> git config --list --show-origin
2 # 以下选去该命令返回的部分结果
3 file:C:/Users/Jing.He/.gitconfig        user.name=jing.he
4 file:C:/Users/Jing.He/.gitconfig        user.email=jing.he@abcompany.com.cn
5 file:C:/Users/Jing.He/.gitconfig        credential.https://gitlabcode.abcompany.com.cn.provider=generic
6 file:.git/config        remote.origin.url=ssh://git@gitlabcode.abcompany.com.cn:32022/cs/project/cloud/azure/automation/internal-audit.git
7 file:.git/config        remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

 

其中"remote.origin.url"便是origin指向的远程仓库地址。

可以尝试使用命令 git remote rename origin origin2 将 origin 改为 origin2,然后再用命令 git config --list查看,就会发现"remote.origin.url"变成了"remote.origin2.url"。

 

需求

直接下载远程branch到本地

git branch alex /ITCAML-1673 origin /alex/ITCAML-1673
# or
git checkout -b <localbranch> origin/<remotebranch>
# or
git checkout -b <branchname>
git reset --hard origin/<branchname>

*如果提示远程分支不存在,跑一下git fetch origin,就会自动更新本地的远程分支列表。How can I list all remote existing branches in Git? - Stack Overflow

另外还可以用git branch -a 来查看本地有哪些分支,缓存了哪些远程分支

 

git branch

# second form
git branch [--track[=(direct|inherit)] | --no-track] [-f]
    [--recurse-submodules]  []

The command’s second form creates a new branch head named <branchname> which points to the current HEAD, or <start-point> if given. As a special case, for <start-point>, you may use "A...B" as a shortcut for the merge base of A and B if there is exactly one merge base. You can leave out at most one of A and B, in which case it defaults to HEAD.

Note that this will create the new branch, but it will not switch the working tree to it; use "git switch <newbranch>" to switch to the new branch.

 
git checkout

Ref: https://www.atlassian.com/git/tutorials/using-branches/git-checkout

git checkout: Specifying -b causes a new branch to be created as if git-branch[1] were called and then checked out. -b选项表示顺手新建branch。

 

 

本地创建分支并推送到远程

1. 本地创建分支"alex/ITCAML-1680"

git branch alex/ITCAML-1680

*在运行该命令前,请注意目前你所在的分支,因为他会基于你目前checkout的分支来创建新分支

2. 推送到远程

先进入新的分支:git checkout alex/ITCAML-1680

方法1: 修改.git\config文件,添加remote和merge字段,修改成如下样例:

[branch  "alex/ITCAML-1680" ]
    remote = origin
    merge = refs /heads/alex/ITCAML-1680

然后运行git push

 

方法2:

git push --set-upstream origin alex/ITCAML-1680

 此命令会自动在.git\config文件中,添加和本地分支同名的远程分支。

方法3:

git push origin alex/ITCAML-1680:alex/ITCAML-1680

*此命令不会自动设置upstream branch,需要每次push时都输入remote branch name。可以通过如下命令或者方法1、方法2中的方法设置upstream branch:

git branch --set-upstream-to=origin/alex/ITCAML-1680

 

 

查看commit history

git log git log --stat

Git - Viewing the Commit History (git-scm.com)

 

查看git配置

git config --get user.name
git config --list

或者直接用文本编辑器查看.git\config文件。

 

修改git配置

git config --global user.name "maxsu"
git config --global user.email "yiibai.com@gmail.com"

git config  user.name "maxsu"
git config user.email "yiibai.com@gmail.com"

或者直接用文本编辑器编辑.git\config文件。

git config命令 - Git教程 (yiibai.com)

Git - git-config Documentation (git-scm.com)

 

删除branch

删除本地branch: git branch -D <branch-name>

删除远程branch:git push origin :<branch-name>

How to delete a remote branch using Git? - Stack Overflow


 

概念摘抄

Detached Head: 

https://git-scm.com/docs/git-checkout#_detached_head

 

Head:

The current branch. In more detail: Your working tree is normally derived from the state of the tree referred to by HEAD. HEAD is a reference to one of the heads in your repository, except when using a detached HEAD, in which case it directly references an arbitrary commit.

https://git-scm.com/docs/gitglossary.html

 

Start Point: 

Explanation 1:

The name of a commit at which to start the new branch; see git-branch[1] for details. Defaults to HEAD.

As a special case, you may use "A...B" as a shortcut for the merge base of A and B if there is exactly one merge base. You can leave out at most one of A and B, in which case it defaults to HEAD.

https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-ltstart-pointgt

 

Explanation 2:

The new branch head will point to this commit. It may be given as a branch name, a commit-id, or a tag. If this option is omitted, the current HEAD will be used instead.

https://git-scm.com/docs/git-branch#Documentation/git-branch.txt-ltstart-pointgt

 

index

A collection of files with stat information, whose contents are stored as objects. The index is a stored version of your working tree. Truth be told, it can also contain a second, and even a third version of a working tree, which are used when merging.

https://git-scm.com/docs/gitglossary.html#Documentation/gitglossary.txt-aiddefindexaindex

index entry

The information regarding a particular file, stored in the index. An index entry can be unmerged, if a merge was started, but not yet finished (i.e. if the index contains multiple versions of that file).

https://git-scm.com/docs/gitglossary.html#Documentation/gitglossary.txt-aiddefindexaindex

 

Working Tree

Tree-ish

Tree Object

 

https://stackoverflow.com/questions/3689838/whats-the-difference-between-head-working-tree-and-index-in-git

 


 

常用命令

git commit parent

commit 有parent,当merge的时候,merged commit有两个parent -- parent 1 和 parent 2

tortoisegit - git revert - git asking parent1 or parent2 - Stack Overflow

 

git merge

git merge master —— merge当前的branch到master branch

 

git revert

可以revert上一个commit的change,revert到这个commit的parent。由于merged commit有两个parent,所以revert时需要指名时哪个commit

git revert 9c31a35
git revert -m 1 5bffe10d943d027485263a569899f284a6b622ca

git revert -m 1 5bffe10d943d027485263a569899f284a6b622ca —— revert commit 5bffe10d943d027485263a569899f284a6b622ca,到parent 1 (就是自己这条分支,不是merge过来的那条分支。merge过来的那条是parent 2)

git/revert-a-faulty-merge.txt at master · git/git · GitHub

 

git reset

如果要revert还没有commit的merge,可以直接用git reset --merge。commit过也可以reset。reset的话好像是不会留下逆向commit的记录,而revert会留下

# 直接reset到commit 9c31a35,commit过的也可以reset
git reset --hard 9c31a35

# 取消还没有commit的merge
git reset --merge

 

As a beginner, after some trial and error (more errors than trials) I've got an important point:

  • git revert requires the id of the commit you want to remove keeping it into your history

  • git reset requires the commit you want to keep, and will consequentially remove anything after that from history.

 

git restore

discard unstaged changes.

git restore .

用来解决报错:

PS C:\Users\s14my9\itcaml\configuration> git reset
Unstaged changes after reset:
M config/bridger/uncontrolled/ci.libsonnet
PS C:\Users\s14my9\itcaml\configuration> git checkout master
error: Your local changes to the following files would be overwritten by checkout:
config/bridger/uncontrolled/ci.libsonnet
config/bridger/uncontrolled/dev.libsonnet
Please commit your changes or stash them before you switch branches.
Aborting

 

git reflog

git reference log。本地head切换的log

 

git diff

git diff: To see all the diff in tracked files but not staged. git - How to see changes to a file before commit? - Stack Overflow

git diff --staged 显示已经git add过的change

git diff --cached 同上,staged和cached意思一样

Git - git-diff Documentation (git-scm.com)

查看其他branch和当前brach的差异,列出修改过的文件的文件名(see what files have changed in a branch)

git checkout 
git  diff --name-only 

Get all files that have been modified in git branch - Stack Overflow

 

git commit

git commit -v : show the changes which are going to be commited

 

git prune

 

 

 

问题

branch diveraged

PS C:\Users\s14my9\itcaml\configuration> git checkout master
error: you need to resolve your current index first
config/Bridger-Infra-Agent/common.libsonnet: needs merge
config/Bridger-Infra-Agent/uncontrolled/dev1.libsonnet: needs merge

解决办法:

git reset --merge

 

 [remote rejected] You need the Git 'ForcePush' permission to perform this action

! [remote rejected] alex/LHUTILXX2-540_backup (TF401027: You need the Git 'ForcePush' permission to perform this action. Details: identity '45597f60-6e37-4be7-xxxx\Alex_He@xxx.com', scope 'branch'.)
error: failed to push some refs to 'https://dev.azure.com/xxxx/ITCAML-IML-ITCAML/_git/Bridger-IaC-OfflineDownloader'

这个brach不是我自己的,所以没权限删除。


 

posted on 2023-03-30 23:35  hejing195  阅读(33)  评论(0编辑  收藏  举报

导航