初次运行配置

git config --global user.name "John Doe"  # 用户名称
git config --global user.email johndoe@example.com  # 邮件地址
git config --global core.editor emacs  # 文本编辑器
git config --global merge.tool vimdiff  # 差异分析工具

项目管理

git init  # 在工作目录中初始化新仓库
git clone git://github.com/schacon/grit.git <dir-name>  # 从现有仓库克隆
git status  # 检查当前文件状态
git add  # 跟踪新文件 暂存已修改文件
git diff  # 当前文件和暂存区域快照之间的差异
git diff --cached  # 已经暂存起来的文件和上次提交时的快照之间的差异
git diff --staged  # 同上
git commit -m "Story 182: Fix ..."  # 提交更新
git commit -a  # 跳过使用暂存区域
git rm readme.txt  # 移除文件
git rm --cached readme.txt  # 要移除跟踪但不删除文件
git mv [old] [new]  # 移动文件
git log -p -2  # 查看提交历史


git remote -v  ## 查看当前的远程库(显示地址)
git remote add [shortname] [url]  # 添加远程仓库
git fetch [remote-name]  # 从远程仓库抓取数据
git push [remote-name] [branch-name]  # 推送数据到远程仓库
git remote show [remote-name]  # 查看远程仓库信息
git remote rename [old] [new]  # 远程仓库的重命名
git remote rm [remote]  # 远程仓库的删除

git --amend  # 修改最后一次提交
git reset HEAD <file>...  # 取消已经暂存的文件
git checkout -- <file>...  # 取消对文件的修改

git tag  # 列显已有的标签
git tag -l 'v1.4.2.*'
git tag -a [tag-name] -m 'my version 1.4'  # 新建含附注的标签
git tag -s [tag-name] -m 'my signed 1.5 tag'  # 签署标签
git tag [tag-name]  # 轻量级标签
git tag -v [tag-name]  # 验证标签
git tag -a v1.2 9fceb02  # 后期加注标签
git push origin [tagname]  # 分享标签
git push origin --tags  # 分享所有标签

git config --global alias.co checkout  # 命名别名
git config --global alias.visual '!gitk'  # 运行外部命令

git branch [branch]  # 创建分支
git checkout -b [branch]  # 切换分支
git merge  # 合并分支
git branch -d [branch]  # 删除分支
git branch  # 显示分支
git branch -v  # 显示分支
git branch -merged  # 显示已合并分支
git branch -no-merged  # 显示未合并分支
git branch -d  # 删除分支
git branch -D  # 强制删除分支

git push (远程仓库名) (分支名)  # 推送本地分支
git push [远程名] [本地分支]:[远程分支]  # 推送本地分支
git push [远程名] :[分支名]  # 删除远程分支

常用别名

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage "reset HEAD --"
git comfig --global alias.unstage "restore --staged "
git config --global alias.last "log -1 HEAD"

密码管理

git config --global credential.helper wincred # 使用windows自带的凭证管理器保存密码
最后修改:2023 年 08 月 02 日
如果觉得我的文章对你有用,请随意赞赏