Git|git 操作指令小抄
信息
2024年9月20日 · ·
Global Git configuration is stored in $HOME/.gitconfig (git config --help)
git command --help
- Notation
$id
,represent either a commit id, branch or a tag name;$file
,arbitrary file name;$branch
,arbitrary branch name;
@[toc]
1. Git Concepts
master
,default development branch;origin
,default upstreamrepository;HEAD
,current branch;HEAD^
,parent of HEAD;HEAD~4
,the great-great grandparent of HEAD;
2. Create
# from existing data
git init
git add .
git add *
git add <filename>
# remove/delete
git rm <filename>
# Save the current state.
git stash
# Return to the saved state.
git stash pop
# from existing repo
git clone ~/existing/repo ~/new/repo
git clone username@host:/path/to/repository
git clone git://host.org/project.git
git clone ssh://you@host.org/proj.git
# Pull the Repo through the access token
# clone with access_token
git clone https://oauth2:access_token@github.com/username/xxx.git
# clone without access_token
git clone https://github.com/xxx/xxx.git
3. Show
# Files changed in working directory
git status
# Changes to tracked files
git diff
# What changed between $ID1 and $ID2
git diff $id1 $id2
# History of changes
git log
# History of changes for file with diffs
git log -p $file $dir/ec/tory/
# View branch merge graph.
git log --graph --pretty=oneline --abbrev-commit
# In regular mode, merging branches will reveal the merge history.
git merge --no-ff -m "merge with no-ff" dev
# Who changed what and when in a file
git blame $file
# A commit identified by $ID
git show $id
# A specific file from a specific $ID
git show $id:$file
# All local branches
git branch
# View command history.
git reflog
4. Revert
# Return to the last committed state (cannot undo a hard reset)
git reset --hard
# Traveling between versions in history, the version pointed to by HEAD is the current version.
git reset --hard commit_id
# Revert the last commit (create a new commit)
git revert HEAD
# Revert specific commit (create a new commit)
git revert $id
# Fix the last commit (after editing the broken files)
git commit -a --amend
# Checkout the $id version of a file
git checkout $id $file
# replace working copy with latest from HEAD
# Discard modifications in the working directory.
git checkout -- <filename>
放弃所有更改
# 确认当前的修改;
git status
# 将所有修改的文件从暂存区中移除;
git reset HEAD .
# 将所有修改的文件恢复到最新的提交状态;
git checkout .