0. Basics
git init //初始化本地仓库
git add README.md //添加
git add * -f //加入所有项目
git status //检查状态 如果都是绿的 证明成功
git commit -m "first commit"//提交到仓库,并写一行注释,如果需要多行注释的话:
git commit -s //会打开一个vim编辑器,可以写很多行注释
git remote add origin [email protected]:aiden/test.git //连接远程仓库并建了一个名叫:origin的别名
git remote add origin https://github.com/user/Activity.git
OR
git remote add origin ssh://[email protected]:2222/blabla/device.git
git push -u origin master //将本地仓库的东西提交到地址是origin的地址,master分支下
如果你的本地提交次数较多,你可以在push之前合并(rebase)自己的提交,这样远端仓库的文件变动历史更易于阅读。
- Pull a branch "20171201" from remote
git branch -a
git checkout 20171201
git status .
git pull origin 20171201
- Push local changes to remote new branch, merge, and pull
git add .
git commit -m ""
git push origin HEAD:new_branch
Then, merge on github.com
git fetch
git checkout master
git pull origin master
- 查看git操作记录
git log //显示所有提交过的版本信息
git log --pretty=oneline
git reflog //查看所有分支的所有操作记录,包括已经被删除的 commit 记录和 reset 的操作
例如执行 git reset --hard HEAD~1
,退回到上一个版本,用git log
则是看不出来被删除的commitid,用git reflog
则可以看到被删除的commitid,我们就可以恢复到被删除的那个版本。
Ref
1. 取消本地改动,重新从远端pull数据
git reset --hard //取消所有本地改动
git pull //重新从远端pull数据
2. 把当前的改动,另存为一个新的branch
git checkout -b newbranch
git checkout -b newbranch origin/newbranch
3. Show the file list changed in a commit
git diff-tree --no-commit-id --name-only -r
// for example:
git diff-tree --no-commit-id --name-only -r 2fc04fae6c579cd2a9deec9d36acf254c76b939a
4. revert/reset a specific file to a specific commit
从某个branch里取出一个文件
git checkout -- file1 file2 file3
git checkout 506ef4997b0b2fd369550b68883d298f372ef5b6 -- system/core/adb/services.cpp
5. Moving changed files to another branch for check-in
# work on some code
git stash
git checkout correct-branch
git stash pop
6. 给branch改名 Rename a local and remote branch in git
If you have named a branch incorrectly AND pushed this to the remote repository follow these steps before any other developers get a chance to jump on you and give you shit for not correctly following naming conventions.
a. Rename your local branch
If you are on the branch you want to rename:
git branch -m new-name
If you are on a different branch:
git branch -m old-name new-name
b. Delete the old-name remote branch and push the new-name local branch
git push origin :old-name new-name
c. Reset the upstream branch for the new-name local branch.
Switch to the branch and then: git push origin -u new-name
Ref: https://multiplestates.wordpress.com/2015/02/05/rename-a-local-and-remote-branch-in-git/
7. git blame
$git blame --line-porcelain Activity.java |sed -n 's/^author //p'| sort |uniq -c |sort -rn
22586 Aiden Fang
41 chengx0327
https://www.atlassian.com/git/tutorials/inspecting-a-repository/git-blame
https://git-scm.com/docs/git-blame
8. git log -p file
9. 把远程的branch更新到本地
To update the local list of remote branches: git remote update origin --prune
To show all local and remote branches that (local) git knows about: git branch -a
Is
git remote update
the equivalent ofgit fetch
???
Reference
Yes and no.git remote update
fetches from all remotes, not just one.
Without looking at the code to see if remote update is just a shell script (possible) it, basically, runs fetch for each remote.git fetch
can be much more granular.
10. Commit the file, but ignore changes to the file
git update-index --assume-unchanged [ ...]
//to undo:
git update-index --no-assume-unchanged [ ...]
Reference
11. 取消本地改动,重新从远端pull数据
git reset --hard //取消所有本地改动
git pull //重新从远端pull数据
git checkout .
git clean -fd
git pull
12. Finding diff between current and last version
git diff HEAD^ HEAD
git diff commit_id HEAD
Since comparison to HEAD is default you can omit it
git diff @^
git diff HEAD^
git diff commit_id
Warnings: on Windows the
~
character must be used instead of^
.
Ref
13. How to grep (search) committed code in the git history
To search for commit content (i.e., actual lines of source, as opposed to commit messages and the like), what you need to do is:
git grep $(git rev-list --all)
Updates: git rev-list --all | xargs git grep
will work if you run into an "Argument list too long" error
If you want to limit the search to some subtree (for instance "lib/util") you will need to pass that to the rev-list subcommand and grep as well:
git grep $(git rev-list --all -- lib/util) -- lib/util
Ref
14. un-delete
If the deletion has not been committed, the command below will restore the deleted file in the working tree.
$ git checkout --
You can get a list of all the deleted files in the working tree using the command below.
$ git ls-files --deleted
If the deletion has been committed, find the commit where it happened, then recover the file from this commit.
$ git rev-list -n 1 HEAD --
$ git checkout ^ --
In case you are looking for the path of the file to recover, the following command will display a summary of all deleted files.
$ git log --diff-filter=D --summary
Ref
15. fatal: refusing to merge unrelated histories
git pull origin branchname --allow-unrelated-histories
16. git fetch --all
You can fetch one branch from all remotes like this:
git fetch --all
fetch updates local copies of remote branches so this is always safe for your local branches BUT:
fetch
will not update local branches (which track remote branches); If you want to update your local branches you still need to pull every branch.fetch
will not create local branches (which track remote branches), you have to do this manually. If you want to list all remote branches:git branch -a
To update local branches which track remote branches:
git pull --all
However, this can be still insufficient. It will work only for your local branches which track remote branches. To track all remote branches execute this oneliner BEFORE git pull --all
:
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
TL;DR version
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
// it seems that pull fetches all branches from all remotes,
// but I always fetch first just to be sure
Run the first command only if there are remote branches on the server that aren't tracked by your local branches.
P.S. AFAIK git fetch --all
and git remote update
are equivalent.
Ref
17. 查看remote url
git remote -v
18. 修改remote url
方法一:
git remote set-url origin git://new.url.here
方法二:
vi .git/config
文件内容:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = ssh://[email protected]/diffusion/ROKIDGLASSLAUNCHER/rrokidglasslauncher.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
修改[remote "origin"]
下的url
。
19. 删除某个remote URL
Use the git remote rm
command to remove a remote URL from your repository.
$ git remote -v
# View current remotes
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)
> destination https://github.com/FORKER/REPOSITORY.git (fetch)
> destination https://github.com/FORKER/REPOSITORY.git (push)
$ git remote rm destination
# Remove remote
$ git remote -v
# Verify it's gone
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)
Note:
git remote rm
does not delete the remote repository from the server. It simply removes the remote and its references from your local repository.