Git笔记

需求

直接下载远程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] <branchname> [<start-point>]

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 <notMainDev>
git diff --name-only <mainDev>

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  阅读(27)  评论(0编辑  收藏  举报

导航