Git

Git常用命令

Posted by DEVIN on Sun, Jun 18, 2023

1.git remote

1# 关联远端仓库
2git remote add origin git@github.com:git_username/repository_name.git
3git remote remove origin
4
5git remote -v

2.git branch

 1# 本地分支关联远程分支(目的是在执行git pull/push操作时就不需要指定对应的远程分支)
 2git branch --set-upstream-to=origin/master
 3git branch -u origin/master
 4
 5# 切换分支
 6git checkout -b <new_branch>
 7
 8# 查看所有分支
 9git branch -a
10
11# 更改分支名称
12git branch -m [old_branch] <new_branch>

3.git push

1# 删除远程分支
2git push origin --delete <branch>
3
4# 同步到远程分支
5git push -u origin <local_branch>:<remote_branch>

4.git diff

 1# 查看当前没有add的内容修改
 2git diff
 3
 4# 查看已经add没有commit的改动
 5git diff --cached
 6
 7# 查看当前没有add和commit的改动
 8git diff HEAD 或者
 9git status 再查看任意连个版本之间的改动 git diff <版本号码1> <版本号码2>
10
11# 比较两个版本号src文件夹的差异
12git diff <版本号码1> <版本号码2> src

5.代码回退

5.1 HEAD~与HEAD^

What’s the difference between HEAD^ and HEAD~ in Git? - Stack Overflow
git在回退版本时HEAD~和HEAD^的作用和区别_git head^-CSDN博客

  • Use~most of the time — to go back a number of generations, usually what you want
  • Use^on merge commits — because they have two or more (immediate) parents

Here is an illustration, by Jon Loeliger. Both commit nodes B and C are parents of commit node A. Parent commits are ordered left-to-right. (N.B. The git log –graph command displays history in the opposite order.)

 1G   H   I   J
 2 \ /     \ /
 3  D   E   F
 4   \  |  / \
 5    \ | /   |
 6     \|/    |
 7      B     C
 8       \   /
 9        \ /
10         A
11
12A =      = A^0
13B = A^   = A^1     = A~1
14C = A^2
15D = A^^  = A^1^1   = A~2
16E = B^2  = A^^2
17F = B^3  = A^^3
18G = A^^^ = A^1^1^1 = A~3
19H = D^2  = B^^2    = A^^^2  = A~2^2
20I = F^   = B^3^    = A^^3^
21J = F^2  = B^3^2   = A^^3^2

5.2 git reset

1# 撤销没有commit的修改
2git checkout .
3
4git reset --hard/mixed/soft origin/HEAD
5git reset --hard/mixed/soft f52c633

6.git tag

 1# 查看标签
 2git tag
 3
 4# 查看标签的版本信息
 5git show v1.0
 6
 7# 打新标签
 8git tag v1.0
 9git tag v0.9 f52c633
10
11# 删除标签
12git tag -d v0.1
13
14# 推送标签到远端
15git push origin v1.0
16
17# 一次性推送全部尚未推送到远程的本地标签
18git push origin --tags
19
20# 如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除,再从远端删除
21git tag -d v0.9
22git push origin :refs/tags/v0.9

7.git show

1# 显示对应commit-id修改信息
2git show f52c633