除了 pull 和 push 你应该知道 git 的操作

git 仓库简单示意图

| 工作区(Working) | 暂存区(Staging) | 版本库(Local repo)| 远程仓库(Remote repo)|

    |---- git add ---->|--- git commit --->|---- git push --->|
    |----------- git commit -am  --------->|---- git push --->|
    |<- git checkout --|<--- git reset ----|<--- git pull ----|
    |<--------- git reset --hard HEAD -----|<--- git pull ----|

与远程分支建立追踪关系(tracking)

git branch --set-upstream master origin/master

设置 rebase 让历史变得清晰

git config --global branch.autosetuprebase always # 所有分支
git config branch.master.rebase true # 指定分支

查看 git 配置信息

git config --list

禁止终端显示中文文件名为 \xxx\xxx 形式

git config --global core.quotepath false

更美观的输出 log

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative -10
git log

查看简要的 log

git log --oneline --5

强制更新,覆盖本地修改

git fetch --all
git reset --hard origin/master

操作撤销(还没有 push)

git reset --soft 3ce07 # 回滚到多个提交之前,但保留没有提交的改变(不回滚工作区和缓存区)
git reset --hard HEAD # 回滚到最近一次的提交(会回滚工作区和缓存区)

操作撤销 (已经 push)

git revert c011e # 用一个新提交来消除一个历史提交所做的修改

版本撤销

git log
git reset --hard e54dd31

修改 commit 注释

git commit --amend -m "Fixes bug #42"

版本恢复

git reflog
git reset --hard c80ae4f

隐藏工作区

git stash # 暂存改动(默认备注)
git stash save "暂存头部样式修改" # 暂存改动,自定义备注
git stash list # 查看暂存的改动列表
git stash pop --index stash@{0} # 释放指定暂存项
git stash pop # 释放全部暂存的改动
git stash drop # 删除暂存的改动

查看修改日志

git log --author=yida # 查看特定成员的更新记录
git log --grep="等待页面" # 按关键字搜索更新记录
git log ./package.json # 查看指定文件的更改日志

打 Tag 操作

git tag v0.9 # 在 HEAD 打"轻 tag"
git tag -am "xxx" v0.9 # 在 HEAD 打"注解 tag"
git tag v0.9 a032c # 在指定提交打"轻 tag"
git tag -a v0.1 -m "xxx" a032c # 在指定提交打"注解 tag"
git tag # 查看打过的 tag
git tag -n # 查看打过的 tag 及注解
git push origin v1.0 # 将 tag 推送到远程
git push origin --tags # 推送全部尚未推送到远程的 tag
git tag -d v0.9 # 删除本地 tag
git push origin :refs/tags/v0.9 # 从远程删除 tag

基于某个 tag 创建新分支

假设在你的主分支上有一个 tag 为 v1.0 , 主分支的名字为 master:

git branch new_branch v1.0 # 以 tag v1.0 创建新的分支 newbranch
git checkout new_branch # 切换到分支 newbranch
git push origin new_branch # 把本地 newbranch 分支提交到远程仓库

添加忽略文件

echo '*~' >> .gitignore
git add .gitignore

添加全局忽略文件

git config --global core.excludesfile ~/.gitignore_global
vim ~/.gitignore_global

加入要忽略的文件名

Git 使用问题

问题1:怎样设置 git 操作免输账号密码?

1、在主文件夹下创建文件 .git-credentials, 用vim编辑:

cd ~
touch .git-credentials
vim .git-credentials

2、在 .git-credentials 添加 https://{username}:{password}@github.com,例如:

https://yida:pass123@github.com

3、在终端下执行命令:

git config --global credential.helper store

4、查看到 ~/.gitconfig 文件新加入一项配置:

[credential]
helper = store

以后 git 操作就不再需要密码验证

问题2:怎样设置 git 忽略已经被提交的文件?

git update-index --assume-unchanged .ftpconfig # 忽略跟踪 .ftpconfig
git update-index --no-assume-unchanged .ftpconfig # 恢复跟踪 .ftpconfig
git ls-files -v | grep -e "^[hsmrck]" # 查看当前被忽略、已经纳入版本库管理的文件

问题3: 如何把在本地 A 分支上的一个 commit(7b31b7)放到本地 B 分支上?

git checkout B
git cherry-pick 7b31b7

问题4. 查看谁弄乱了文件 index.php 的代码?

git blame index.php

问题5. 怎样恢复丢失的提交?

git reflog # 查看该所有操作日志 -- 例如,丢失的提交信息如下:
# 794b305 HEAD@{24}: rebase: 修改开户状态相关
git branch recover-branch 794b305 # 在丢失的 commit(794b305) 上创建一个新分支, 即可恢复此次提交
标签:GIT 发布于:2019-11-04 23:07:43