1、创建仓库
$ git init
2、添加修改后的文件到本地库
$ git add .
3、把文件提交到仓库
$ git commit -m "备注"
4、本地库和远端库关联,"origin"在Git中是一个约定俗成的远程仓库别名
git remote add origin [url]
5、git修改远程仓库地址
可直接改:git remote origin set-url [url]
或先删除后重新关联:git remote rm origin
6、创建develop分支并切换分支,并将该分支与远程仓库的"origin/develop"分支进行关联
git checkout -b develop origin/develop
7、上传本地当前分支代码到develop分支或master分支
git push origin develop
git push origin master
8、拉取远端代码
git pull
9、clone远端代码
git clone [url]
10、查看所有分支的所有操作记录
git reflog
11、回退
git reset --hard commitid
12、删除暂存区
或分支
上的文件, 但本地又需要使用, 只是不希望这个文件被版本控制, 可以使用
git rm --cached file_path
13、冲突解决方法
git stash// 暂存当前状态
git pull
git stash pop// 恢复暂存区和工作区进度
14、分支操作
创建分支: $ git branch mybranch
切换分支: $ git checkout mybranch
创建并切换分支: $ git checkout -b mybranch
15、长期记住密码
git config --global credential.helper store
16、查看分支关联
git branch -vv
17、本地分支和远程分支建立联系
git branch --set-upstream-to=origin/develop develop
18、忽略某个文件的一行代码(例如替换本地app.js里面的localhost:8000成远端test.qingchunpai.net/chunshunzhuxue)
1)在工程的根目录下创建/打开一个.gitattributes文件(会被提交到本地或者远程仓库),或者在根目录下创建/.git/info/attributes(不会被提交到本地或者远程仓库)
2)在第一步的文件(两者任选其一)中添加如下,用于定义有内容需要被过滤/忽略的文件:*.js filter=app
3)$ git config --global filter.app.smudge 'sed "s#test.qingchunpai.net/chunshunzhuxue#localhost:8000#g"'
$ git config --global filter.app.clean 'sed "s#localhost:8000#test.qingchunpai.net/chunshunzhuxue#g"'
参考资料:
替换:https://juejin.im/entry/57a4aa7ac4c971005a158662
忽略:https://cnodejs.org/topic/54f854e54e70e4022cd933a5
linux sed命令就是这么简单:https://www.cnblogs.com/wangqiguo/p/6718512.html#_label0
19、.gitignore规则不生效的解决办法,该操作之前注意数据保存,嘿嘿
把某些目录或文件加入忽略规则后发现并未生效,原因是.gitignore只能忽略那些原来没有被追踪的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。那么解决方法就是先把本地缓存删除(改变成未被追踪状态),然后再提交:
git rm -r --cached .
git add .
git commit -m 'update .gitignore'