Git 命令必知必会汇总

286 阅读3分钟

Git 命令必知必会汇总

1. Setup & Init 基本的设置和初始化

//initialize an existing directory as a Git repository
git init

git clone [url]
retrieve an entire repository from a hosted location via URL

2 . stage & snapshot & .git directory 工作区,暂存区和.git目录

Alt

git status
show modified files in working directory, staged for your next commit
git add [file]
add a file as it looks now to your next commit (stage)

git add -u

git add .


  • Changes to be committed:

  • Changes not staged for commit:

  • Untracked files:

3 . Git diff 找不同

git diff
diff of what is changed but not staged

git diff --staged
diff of what is staged but not yet commited

git diff -cached // 效果和staged 一样

4. 把已经加入staged的文件打上Commit 标记

git commit -m “[descriptive message]”
commit your staged content as a new commit snapshot

5. BRANCH & MERGE

git branch
list your branches. a * will appear next to the currently active branch

git branch [branch-name]
create a new branch at the current commit, while you still still stay in the current branch


// 删除本地的某个分支
git branch -D [branch-name] 

git checkout [branch-name]
switch to another branch and check it out into your working directory
1. 本地有
2. 本地没有,remote repo 有
3. 本地没有,remote repo 显示找不到



// 以当前分支为蓝本,生成一个新的分支

git checkout -b [branch-name]

6. 查看当前分支的commit tree 信息

git log

show all commits in the current branch’s history

git log --graph
* commit bfcbc4b43afde55efd2280b6b8bfb00e0c72296f
| Author: Qian Liang <liangqian@rd.netease.com>
| Date:   Fri Jun 21 15:22:33 2019 +0800
|
|     ali to zhi
|
*   commit 4b8219741ed62faef072c46505b671c4f9f9a691
|\  Merge: ff2ef07 a105f22
| | Author: Qian Liang <liangqian@rd.netease.com>
| | Date:   Fri Jun 21 11:57:18 2019 +0800
| |
| |     Merge branch 'zhiyun-unregistration-merge' into 'master'
| |
| |     Zhiyun unregistration merge
| |
| |     See merge request zhiyun/devsys!87
| |
| * commit a105f22b74e30dfb9164166775a006c164cf7123
| | Author: Ye Wang <wangye@rd.netease.com>
| | Date:   Wed Jun 19 16:43:52 2019 +0800
| |
| |     修改注销userId mask逻辑
| |
| * commit d8d3b34ac741bbe9edfe1df20c30478afacc4a73
| | Author: Ye Wang <wangye@rd.netease.com>
| | Date:   Wed Jun 19 11:56:49 2019 +0800
| |
| |     注销资源包有剩余msg更改
| |

git log --oneline

bfcbc4b ali to zhi
4b82197 Merge branch 'zhiyun-unregistration-merge' into 'master'
a105f22 修改注销userId mask逻辑
d8d3b34 注销资源包有剩余msg更改
15d09da 注销:区分余额>0和资源包还有剩余时的errorcode
999a442 bugfix on account cancel SPA
23ea101 bugfix for unregistration
7f3e417 余额大于0不允许注销


// 显示简易版的log ,并且仅仅显示最近的5行
git log --oneline -6



Charless-MacBook-Pro:devsys charles$ git log --oneline -6
295e59d Revert "minor improvement on front page" this is unnecessary This reverts commit cf3078a6d9ae195435abd2ec78c7920859ea490b.
cf3078a minor improvement on front page
7b2df0f pointing to aidemo
458cfac not used arrow function for IE
29aa82f new receipt img + show table when click another image
2f94433 removed smart in heading

7. SHARE & UPDATE 设置云端地址和拉取最新的代码

设置云端的URL地址

git remote add [alias] [url]
add a git URL as an alias

git remote get-url origin
git@gitlab.corp.youdao.com:zhiyun/devsys.git

git pull
fetch and merge any commits from the tracking remote branch



8. TEMPORARY COMMITS 暂存commits

git stash
Save modified and staged changes

git stash list
list stack-order of stashed file changes

git stash pop
write working from top of stash stack

git stash drop
discard the changes from top of stash stack

9. REWRITE HISTORY GIT rebase

git rebase [branch]
apply any commits of current branch ahead of specified one
          A---B---C topic
         /
    D---E---F---G master
git rebase master
git rebase master topic

                  A'--B'--C' topic
                 /
    D---E---F---G master

10. Git reset

定义:重置ref pointer

用途:重新调整指针的位置

Alt

我们有3个状态 staging ,staged and commited

  1. --hard

重置ref pointer 位置,并且抛弃改变

git reset commitId --hard

2.-- mixed or 不写

重置ref pointer 位置,并且把改变放到放回到working area 工作区,这样git diff 就能直接看出变化啦

git reset commitId --mixed
  1. --soft

重置ref pointer 位置,并且把改变放到放到staged area 已添加区,这样git diff --cached 就能直接看出变化啦

  1. HEAD~5 的使用方法

HEAD 是什麼 ? HEAD 是一個指標,指向某一個分支,通常你可以把 HEAD 當做「目前所在分支」看待

//回到最新的提交版本
git reset head

//回到上一个提交版本
git reset head~1

// 回到n 个版本之前
git reset head~n

  1. 你又后悔了reset才做怎么办?
//查看所有指针变动的记录
git reflog


//回復到的指定commit id 的提交記錄版本
git reset --hard commit_id

git reset --hard ba2f0a4

11. Git revert

原理: 新增一个commit 去对冲掉之前某一个commit的改变

// 原本的样子
Charless-MacBook-Pro:devsys charles$ git log
commit cf3078a6d9ae195435abd2ec78c7920859ea490b
Author: Qian Liang <liangqian@rd.netease.com>
Date:   Fri Sep 20 11:58:20 2019 +0800

    minor improvement on front page

commit 7b2df0f9caad1b5f2201d2850f01be41d6d5e5d0
Author: Qian Liang <liangqian@rd.netease.com>
Date:   Wed Sep 11 15:31:24 2019 +0800

    pointing to aidemo
    
// 进行了回滚的操作  
Charless-MacBook-Pro:devsys charles$ git revert head
[structure-ocr-ui 295e59d] Revert "minor improvement on front page" this is unnecessary This reverts commit cf3078a6d9ae195435abd2ec78c7920859ea490b.
 3 files changed, 59 insertions(+), 27 deletions(-)
 
 
 
// 新的样子
Charless-MacBook-Pro:devsys charles$ git log
commit 295e59de7fb411a1a37ee693c63a527dbdb3c399
Author: Qian Liang <liangqian@rd.netease.com>
Date:   Mon Oct 21 11:48:34 2019 +0800

    Revert "minor improvement on front page"
    this is unnecessary
    This reverts commit cf3078a6d9ae195435abd2ec78c7920859ea490b.

commit cf3078a6d9ae195435abd2ec78c7920859ea490b
Author: Qian Liang <liangqian@rd.netease.com>
Date:   Fri Sep 20 11:58:20 2019 +0800

    minor improvement on front page

commit 7b2df0f9caad1b5f2201d2850f01be41d6d5e5d0
Author: Qian Liang <liangqian@rd.netease.com>
Date:   Wed Sep 11 15:31:24 2019 +0800

    pointing to aidemo