简介
首先告诉 Git 你是谁;姓名和邮箱会记录在你创建的每一个提交中。git config --global user.name "Your Name"git config --global user.email "you@example.com"
日常工作流程
变更分两步进行:add将变更放入暂存区,commit将暂存的变更转为永久快照。status显示当前状态。git statusgit add [file]git add .git commit -m"describe the change"
与远程仓库交换提交。pull是fetch(下载)加merge(合并)的一步操作。分支
分支是指向提交的轻量指针。switch切换分支,-c创建分支;checkout是较老的命令,两者都能做。git branchgit branch -agit switch [branchName]git switch -c [newBranch]git checkout -b [newBranch]
在本地和远程删除分支。-d拒绝删除未合并的工作;-D强制删除。git branch -d [branchName]git branch -D [branchName]git push origin --delete [branchName]
查看历史
git loggit log --oneline--graph--allgit show [commitHash]git blame [fileName]
单独使用diff显示未暂存的变更;--staged显示下一次提交将包含的内容。git diffgit diff --stagedgit diff [branch1]..[branch2]
撤销操作
丢弃文件的未提交变更,或将其从暂存区撤出但不丢失变更。git restore [fileName]git restore --staged [fileName]
修正最近一次的提交(修改信息或补漏文件),或通过创建新的反向提交安全地撤销一次提交。git commit --amendgit revert HEAD
reset将分支指针回退。--soft保留变更在暂存区,--hard彻底丢弃。git reset --soft HEAD~1git reset --hard HEAD~1
revert在共享分支上是安全的,因为它只添加提交。reset --hard和--amend会重写历史:避免对已推送的提交使用。停止跟踪已提交但后来加入 .gitignore 的文件。gitrm-r--cached .git add .git commit -m"remove ignored files"
暂存(Stashing)
将未提交的工作暂存一旁,获得干净的工作目录,之后可随时恢复。-u包含未跟踪文件。git stash -ugit stash listgit stash popgit stash apply
pop应用储藏并将其从列表中移除;apply保留储藏,适用于将同一份变更应用到多个分支。标签
标签标记特定提交,通常用于发布版本。附注标签(-a)存储作者、日期和说明信息。git tag [tagName]git tag -a v1.0 -m"release 1.0"git tag -d [tagName]git push --tags
标签默认不会推送。使用--tags或 git push origin [tagName] 显式推送。远程仓库
远程仓库是仓库另一副本的命名 URL;origin是主远程仓库的惯例名称。git remote -vgit remote add [remoteName] [remoteURL]git fetch [remoteName]git pull [remoteName] [branchName]
push -u将本地分支关联到远程分支,之后git push和git pull无需参数即可工作。git push -u [remoteName] [branchName]
重写历史
rebase将你的提交在另一个分支之上重放,以获得线性历史;cherry-pick将单个提交复制到当前分支。git rebase [branchName]git rebase -i HEAD~3git cherry-pick [commitHash]
交互式 rebase(-i)允许你重新排序、压缩和修改提交信息。与reset一样,永远不要 rebase 他人可能已经拉取的提交。获取帮助
git help [command]git help -g