Git 常用命令
2017年2月7日 · 1485 字 · 3 分钟
Git (/ɡɪt/) is a version control system (VCS) for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for software development, but it can be used to keep track of changes in any files. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows.
Git
Git 是一个非常优秀的工具,但是要完全爱上它,却也不容易,毕竟不是人人都内接受 命令行
的。
命令行的记忆只能靠经常使用,自然而然的记住它。有时候一些指令没有记住也没关系,只要你能找到它就行。
为了防止自己遗忘,我将一些我遇到的命令记录下来。
常用命令
### Tell Git Who you are
### 这里 --global 参数代表修改全局配置信息,
### 如果删除,则只修改当前仓库的配置信息。
git config --global user.name "ban"
git config --global user.name ### varify
git config --global user.email ban@gmail.com
git config --global user.email ### varify
### Create a new local repository
### 新建本地仓库
git init
### Create a working copy of a local repository
git clone /path/to/repository
### Check out a remote repository
git clone username@host:/path/to/repository
### Add files
git add <filename>
git add *
### Commit
git commit -m "Commit message"
git commit -a
### Push
git push origin master
### Status
git status
### Connect to a remote repository
git remote add origin <server>
### list all currently configured remote repositories
git remote -v
### Create a new branch and switch to it
git checkout -b <branch name>
### Switch from one branch to another
git checkout <branch name>
### List all the branches in your repo,
### and also tell you what branch you're currently in
git branch
### Delete the feature branch
git branch -d <branch name>
### Push the branch to your remote repository;
### so other can use it
git push origion <branch name>
### Push all branches to your remote repository
git push --all origion
### Delete a branch on your remote repository
git push origion :<branch name>
### Fetch and merge changes on the remote
### server to your working directory
### #########################################################################
### What are the differences between git pull and git fetch?
### In the simplest terms, git pull does a git fetch followed by a git merge.
### #########################################################################
git pull
### To merge a different branch into your active branch
git merge <branch name>
### ######################################################
### 查看 Diff ,还是用一些 GUI 工具比较好。
### ######################################################
### View all the merge conflicts:
git diff
### View the conflicts against the base file:
git diff --base <file name>
### Preview changes, before merging:
git diff <source branch> <targetbranch>
### #######################################################
### TAG
### #######################################################
### You can use tagging to mark a significant changeset,
### such as a release
git tag 1.0.0 <commit ID>
### Commit ID is the leading characters of the changeset ID, up to 10,
### but must be unique. Get the ID using:
git log
### Push all tags to remote repository:
git push --tags origion
### If you mess up, you can replace the changes in your working tree
### with the last content in head:
### Changes already add to the index, as well as new files, will be kept.
git checkout --<filename> ### 这条命令没试过
### Instead, to drop all your local changes and commits,
### fetch the lastes history form the server and point
### your local master branch at it, do this:
git fetch origin
git reset --hard origion/master
### Search the working directory for `foo()`:
git grep "foo()"
git cherry-pick commitSha
## a - b - c - d Main
## \
## e - f - g Feature
##
git checkout main
git cherry-pick f
## a - b - c - d - f Main
## \
## e - f - g Feature
还有一些不常使用的命令
天朝人民代理是刚需
git config --global http.proxy http://127.0.0.1:1080
git config --global http.proxy ### varify
git config --global https.proxy https://127.0.0.1:1080
git config --global https.proxy ### varify
## 如果代理填错了,删除代理
git config --global --unset http.proxy
git config --global --unset https.proxy
修改 Commit 中的 author 信息。
曾经有个 Github 仓库,因为 author 信息不同,没有“打卡”数据。
### 先修改本地数据
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
### 然后统一上库
git push --force --tags origin 'refs/heads/*'
How to stop tracking and ignore changes to a file
如果一个文件被 “add” 过,之后不想跟踪了,添加该文件名到 .gitignore
中,这样做是没有用的。正确的姿势是:
git rm --cached [abc.txt]
git add .
## add "abc.txt" in .gitignore if necessary
git commit -m "untrack abc.txt"
Remove the remote repository from local repository
git remote rm origin
git remote -v ## verify
List files in local git repo
### lists all of the already committed files being tracked by your git repo.
git ls-tree --full-tree -r HEAD
git ls-tree HEAD
### This lists all of the files in the repository, including those that are only staged but not yet committed.
git ls-files
Git refusing to merge unrelated histories
The default behavior has changed since git 2.9:
“git merge” used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project. The command has been taught not to allow this by default, with an escape hatch “–allow-unrelated-histories” option to be used in a rare event that merges histories of two projects that started their lives independently.
See the git release changelog for more information.
You can use --allow-unrelated-histories
to force the merge to happen.
git pull --allow-unrelated-histories
How do I force “git pull” to overwrite local files?
git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master
写在最后
Git 的命令很多,有些用多了就记住了,有些只要混个眼熟就行。
UPDATE 17/02/15
## List all branches, local and remote
git branch -av
## Rever everything to the last commit
git reset --hard
## Tag the current commit
git tag my_tag
## ignore mode change 777
git config core.fileMode false
UPDATE 18/06/27
## 修改多个文件后,没有 ‘add .’ 操作之前,放弃所有修改
git checkout .
## file/dir 恢复到 HEAD 状态
git checkout HEAD file/dir
UPDATE 18/07/19
git status
输出中文乱码:
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: _posts/FAQ-ERROR.md
modified: "_posts/linux-\345\256\236\347\224\250\345\221\275\344\273\244.md"
修改 core.quotepath
参数即可。
git config --global core.quotepath false
UPDATE 18/08/06
在 clone
或 push
中出现类似卡死的现象。
git config --global http.postBuffer 1048576000
上述操作不是万能药,但值得一试。详见-stackoverflow