.gitignore not ignoring .idea path 无法忽略某些目录或文件
因为这些文件已经被添加到repo中了,最好在第一次提交前,把不需要的文件添加到.gitignore
.gitignore only ignores newly added (untracked) files.
If you have files that have already been added to the repository, all their changes will be tracked as usual, even if they are matched by .gitignore rules.
To remove that folder from the repository (without deleting it from disk), do:
git rm --cached -r .idea
然后再向远端推一次,远端上的相应文件也消除了
常见用法
git config --global user.name "sunchenguang"
git config --global user.email "809200299@qq.com"
git remote remove origin
git remote set-url origin git://new.url.here
git config --global alias.ci "commit -v"
//移除某个alias
git config --global --unset alias.XXX
//直接编辑git global config
git config --global --edit
git config --global core.autocrlf false
新repo推送
…or create a new repository on the command line
echo "# blog" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:sunchenguang/blog.git
git push -u origin master
…or push an existing repository from the command line
git remote add origin git@github.com:sunchenguang/blog.git
git push -u origin master
查看远程分支
git pull // 保持代码最新
git branch -r
拉取远程分支并创建本地分支
git checkout -b <本地分支名> origin/<远程分支名>
使用该方式会在本地分支,并自动切换到该本地分支。
合并分支
git merge <指定分支>
git merge命令用于合并指定分支到当前分支。会创建一个额外的merge commit
git rebase old_branch 也是做合并分支,不会产生一个额外的merge commit
删除分支
git branch -d <指定分支>
切换分支
git checkout <指定分支>
stash暂时隐藏修改
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。
git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。
合并两个git仓库
If you want to merge project-a into project-b:
cd path/to/project-b
git remote add project-a path/to/project-a
git fetch project-a
git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge
git remote remove project-a
This method worked pretty well for me, it’s shorter and in my opinion a lot cleaner.
Note: The –allow-unrelated-histories parameter only exists since git >= 2.9.
丢弃本地untracked修改或未提交unstaged的修改
git clean -df
git checkout -- .
git clean
removes all untracked files (warning: while it won’t delete ignored files mentioned directly in .gitignore, it may delete ignored files residing in folders) and git checkout
clears all unstaged changes.
移除某个提交 commit
http://stackoverflow.com/ques…
git revert --strategy resolve <commit>
是可用的,会创建一个revert提交
There are four ways of doing so:
//Clean way, reverting but keep in log the revert:
git revert --strategy resolve <commit>
//Harsh way, remove altogether only the last commit:
git reset --soft "HEAD^"
//Rebase (show the log of the last 5 commits and delete the lines you don't want, or reorder, or squash multiple commits in one, or do anything else you want, this is a very versatile tool):
git rebase -i HEAD~5
//And if a mistake is done:
git rebase --abort
//Quick rebase: remove only a specific commit using its id:
git rebase --onto commit-id^ commit-id
//Alternatives: you could also try:
git cherry-pick commit-id
//Yet another alternative:
git revert --no-commit
//As a last resort, if you need full freedom of history editing (eg, because git don't allow you to edit what you want to), you can use this very fast open source application: reposurgeon.
回滚git项目到某个提交
https://stackoverflow.com/que…
//本地回滚到某个提交
git reset --hard <tag/branch/commit id>
git reset without the --hard option resets the commit history, but not the files. With the --hard option the files in working tree are also reset. (credited user)
If you wish to commit that state, so remote repository also points to rolled back commit do:
//强制更新远端
git push <reponame> -f (credited user)
Unlink of file Failed. Should I try again?
https://stackoverflow.com/que…
I had this issue and solved it by the command : git gc
The above command remove temp and unnecessary files. (Garbage collector.)
合并时遇到冲突想取消操作,恢复index
git merge --abort
Convert shallow clone to full clone
The below command (git version 1.8.3) will convert the shallow clone to regular one
git fetch --unshallow
Then, to get access to all the branches on origin (thanks @Peter in the comments)
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
将已有的本地git仓库关联一个远程仓库
要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git
;==给本地仓库添加一个源,名称为origin, 如果已存在,换个名字==
关联后,使用命令git push -u origin master
==第一次推送master分支的所有内容;,origin是源的名称,master是分支名,它不会一次把所有分支推上去,一次推一个分支,推别的分支改个名称即可==。
此后,每次本地提交后,只要有必要,就可以使用命令git push origin master
推送最新修改;
关联本地分支和远端分支
release/0.1是分支名
git branch --set-upstream-to=origin/release/0.1 release/0.1
使用git submodule来管理子模块
https://segmentfault.com/a/11…
更新submodule
在父项目目录下运行
git submodule foreach git pull
在submodule目录下更新
cd pod-library
git pull
解决git clone速度慢,配置代理
https://www.zhihu.com/questio…
请注意,这里指的是https协议,git clone https://www.github.com/xxxx/xxxx.git
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
不推荐直接用全局代理, 建议只对github进行代理,对国内的仓库不影响
git config --global http.https://github.com.proxy https://127.0.0.1:1080
git config --global https.https://github.com.proxy https://127.0.0.1:1080
如果在输入这条命令之前,已经输入全局代理的话,可以输入进行取消
git config --global --unset http.proxy
git config --global --unset https.proxy
附上socks5代理的方法。
git config --global http.https://github.com.proxy socks5://127.0.0.1:1086
git config --global https.https://github.com.proxy socks5://127.0.0.1:1086
对于SSH协议,依然无效。
git clone git@github.com:xxxxxx/xxxxxx.git
这是个很有效的配置,修改后速度有质的提升, ==并没有==
git config --global http.postBuffer 524288000