1、安装git
2、设置用户名和email
git config --global user.name “用户名”
git config --global user.email “邮箱”
3、生成key 执行生成公钥和私钥的命令:ssh-keygen -t rsa 并按回车3下
4、cat ~/.ssh/id_rsa.pub 将公钥添加到github,或者gitlab的 Deploy Keys 中
----------------------------------------------------------------------------------------------------------------------------------------
1、获取远程分支到本地
git checkout -b cmsm-v0.0.1 origin/cmsm-v0.0.1
2、提交本地分支到远程仓库
git push origin cmsm-v0.0.1:cmsm-v0.0.1
3、提交本地 v1分支到远程v1分支
git push orgin v1
4、更新远程 v1分支到本地v1分支
git pull orgin v1
5、删除远程分支
git push orgin --delete v3
6、 删除本地分支
git branch -d v3
----------------------------------------------------------------------------------------------------------------------------
1、保存成都分支和成都new分支的差别. Administrator@USER-20161116WV MINGW64 /d/demo/gitproject/front (cmsm-for-chengdu) $ git diff cmsm-for-chengdu-new >./diff.path Administrator@USER-20161116WV MINGW64 /d/demo/gitproject/front (cmsm-for-chengdu) $ ls CentralizedMfront/ CMSystemNW/ diff.path flashPlayer/ npm-debug.log nwjs-win-x64/ Administrator@USER-20161116WV MINGW64 /d/demo/gitproject/front (cmsm-for-chengdu) $ vi diff.path 2、切换到成都new分支并应用这些差别 Administrator@USER-20161116WV MINGW64 /d/demo/gitproject/front (cmsm-for-chengdu) $ git checkout cmsm-for-chengdu-new gSwitched to branch 'cmsm-for-chengdu-new' i Administrator@USER-20161116WV MINGW64 /d/demo/gitproject/front (cmsm-for-chengdu-new) $ git apply --reject --whitespace=fix diff.path 3、则成都new分支和成都分支则无差别 4、此时,merge 成都分支的话,才可以 push当前分支到成都分支 Administrator@USER-20161116WV MINGW64 /d/demo/gitproject/front (cmsm-for-chengdu-new) $ git merge cmsm-for-chengdu Already up-to-date. Administrator@USER-20161116WV MINGW64 /d/demo/gitproject/front (cmsm-for-chengdu-new) $ git rm CMSystemNW/picture/bg.png^C Administrator@USER-20161116WV MINGW64 /d/demo/gitproject/front (cmsm-for-chengdu-new) $ git push origin cmsm-for-chengdu-new:cmsm-for-chengdu
git cherry-pick用于把另一个本地分支的commit修改应用到当前分支。
实际问题
在本地 master 分支上做了一个commit ( 38361a68138140827b31b72f8bbfd88b3705d77a ) , 如何把它放到 本地 old_cc 分支上?
办法之一: 使用 cherry-pick. 根据git 文档:
Apply the changes introduced by some existing commits
就是对已经存在的commit 进行apply (可以理解为再次提交)
简单用法:
git cherry-pick <commit id>
例如:
$ git checkout old_cc
$ git cherry-pick 38361a68
1. 如果顺利,就会正常提交。结果:
Finished one cherry-pick.
# On branch old_cc
# Your branch is ahead of 'origin/old_cc' by 3 commits.
2. 如果在cherry-pick 的过程中出现了冲突
Automatic cherry-pick failed. After resolving the conflicts,
mark the corrected paths with 'git add <paths>' or 'git rm <paths>'
and commit the result with:
git commit -c 15a2b6c61927e5aed6718de89ad9dafba939a90b
就跟普通的冲突一样,手工解决:
执行git status 看哪些文件出现冲突
$ git status
both modified: app/models/user.rb
接着手动解决冲突的文件,然后通过git add把改到添加到索引,最后执行git commit提交修改就好了。
Add 回退的方法:
git add app/controllers/cart_controller.php
git stash --keep-index
git reset
查看 文件修改的记录
1、查看某个文件的提交记录 git log -- filename (git log filename) 2、可以显示该文件每次提交的diff git log -p filename 3. 查看某次提交中的某个文件变化 git show commit-id filename 4.根据commit-id查看某个提交 git show commit-id
1.使用git log -g 找回之前提交的commit 2.使用git branch recover_branch[新分支] commit_id命令用这个commit创建一个分支 此时 recover_branch is your delete branch.