本文主要罗列一些工作中常用的 git 命令,希望这些命令能对您有所帮助。
git reset --hard FETCH_HEAD
git checkout a.c
git rm --cached <added_file_to_undo>
git reset --soft commitID 只是删除了commitId之后的commit记录,但是代码改动仍然存在
git reset --hard commitID 彻底的回到CommitID13:39时候的版本,之后的改动不存在了
git stash 将本地修改暂存起来
git stash pop 弹出暂存的修改
git status -uno
在 .gitignore 中添加不想被 git staus 看到的文件或目录
首先通过 git log查看你之前的提交码
git log | more
commit 4adb3f0ecd9dbc79bd09666d88f8c2520305c001 Author: xxxxxx Date: Thu Jan 25 11:51:45 2018 +0800
摘取 commit 码的前 7位,执行下面的命令
git checkout 4adb3f0
git reset --hard 4adb3f0
git checkout master
git log --stat | more
git show <commit id> [<filename>]
git branch -a
git branch -r
git remote -v
git checkout -b new_branch
git --set-upstream origin new_branch
git push origin new_branch
git pull -p
有些情况下,通过 git status
能查到某些文件有变化,但使用 git diff
却看不到修改的内容。可以使用下面的面试查看变化。
在使用 git diff 时,常常发现有很多不方便的地方。因为git diff 默认使用 patch 方式展示代码的不同。如果想看修改后代码的上下文就比较麻烦了(比如代码 review)。
其实 git 已经提供了扩展功能。可经将它的默认 diff 工具修改为vimdiff。配置如下:
vim ~/.gitconfig
[difftool]
prompt = false
[diff]
vimdiff
tool = vimdiff
[difftool "vimdiff"]
path = /usr/bin/vimdiff
//为git打tag, 第一次需要在前面加一个v
git tag "v1.0.0"
//将tag推送到远程仓库
git push --tags
git merge --no-ff
git checkout tag_name
git log --author=“author”
在使用Git 的时候,经常会遇到需要频繁输入密码的情况,每次git push 和 git pull 都要求输入用户名和密码,如果提交频繁的话就十分不方便。
可以使用下面的方法,只需要第一次输入用户名和密码,以后都不用再输入了。
vim ~/.gitconfig
[credential]
helper = store
git config --global user.name "newName"
git config --global user.email "newEmail"
git commit --amend --author="userName <userEmail>"
注意不能缺少< >
此指令仅能更新最近的一次commit的用户名邮箱
git apply --stat file.patch
git apply --check file.patch
git am --signoff < a_file.patch
git commit -m ""
git push
git clone --recursive https://xxx.git
git remote set-url origin https://xxx.git