配置 | config

撤销与回滚 | undo and rollback

git commit之前 | before git commit

git add之前 | before git add

也就是撤销工作区的更改。

➜ git:(develop)git status
On branch develop
Your branch is up to date with 'origin/develop'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

运行git checkout -- <file>或者git checkout -- .(全部变动)

➜ git:(develop) git checkout -- README.md
➜ git:(develop) git status
On branch develop
Your branch is up to date with 'origin/develop'.

nothing to commit, working tree clean

git add之后 | after git add

也就是撤销暂存区的更改。

➜  testLanguagesProject git:(develop)git status
On branch develop
Your branch is up to date with 'origin/develop'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md

运行git reset HEAD <file>git reset HEAD(全部变动)

➜ git:(develop)git reset HEAD README.md
Unstaged changes after reset:
M       README.md
➜ git:(develop)git status
On branch develop
Your branch is up to date with 'origin/develop'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

git commit之后 | after git commit

修改git commit信息

  • 修改最后一次提交 git commit --amend
  • 修改多个提交说明git rebase -i HEAD~3

修改git commit时间

# get now time
date -R  
# make commit_time following format of now time and commit
git commit --amend  --date="commit_time"
# or commit with now time
git commit --amend --date="$(date -R)"
git commit --amend --date=`date -R`

撤回某个commit

git revert commitId

回滚到某个commit

撤销commit(版本库),保留暂存区,保留工作区更改:

git reset --soft commitId

撤销commit(版本库),撤销暂存区,保留工作区更改:

git reset --mixed commitId
或
git reset commitId

撤销commit(版本库),撤销暂存区,撤销工作区更改:

git reset --hard commitId

Case problem

# disable ignore case
git config --global core.ignorecase false

将文件/文件夹名称由myFile改为myfile,运行下面的命令后再push,否则remote仍然会存在修改之前的文件/文件夹。

$ git rm -r --cached myFile

Submodule

Add submodule

git submodule add git@github.com:user/subRepo.git subRepo

Update submodule

# in parent directory
git submodule foreach git pull
# or in submodule directory
cd subRepo/
git pull

Clone a project with submodule

git clone git@github.com:user/parentRepo.git --recursive
# or
git clone git@github.com:user/parentRepo.git
git submodule init
git submodule update

Delete submodule

git rm -r --cached subRepo
rm -rf subRepo
rm -rf .git/modules/subRepo
# manually delete submodule info in these two files
vim .gitmodules
vim .git/config

Tags

# Add tag
git tag -a tagName -m tagMessage
# List tags
git tag
# Push tag
git push origin tagName
# Push tags
git push origin --tags
# Delete local tag
git tag -d tagName
# Delete remote tag
git push origin :refs/tags/tagName
# Rename a tag
git tag newTag oldTag

Command saving your life

git reflog

Git 命令速查表[∞]


Refer:
[1]git 修改上次git commit的时间
[∞]⽅糖⼩⽩课·版本管理和Git⼊⻔.Git 命令速查表

Related:
Merge Two Branches From Two Git Projects
Permanently Delete Files From Git History

 TOC