预备工作
配置用户信息:
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
检查配置信息:
git config user.name
git config user.email
git config --list
一、本地常用操作
提交改动:
$ git add all # 提交所有变动
$ git commit -m "update"
$ git push
版本回退:
$ git reflog # 查看历史变更记录
$ git reset --hard HEAD@{n} # n是要回退到的引用位置
# 另一种方式
$ git log
$ git reset --hard commitID
新建并切换到一个新的分支上工作:
$ git checkout -b <dev>
Switched to a new branch 'dev'
更改分支名称:
$ git branch -m <old_branch> <new_branch>
删除分支:
$ git branch --delete <BranchName>
比较分支间差异:
$ git diff branch1 branch2 (filename) #显示两个分支中(某文件)的详细差异
Modify history commits, e.g. 合并commits:
$ git rebase -i commitId #(待合并的最早一次commit的前一个commit的ID)
之后进入rebase内容编辑页面,第一行的commit作为合并后的commit保留不用动,把后边每一行commitID前边的pick改成squash,然后保存退出。之后又进入了commit message编辑页面,改好后保存退出,提示"successfully ...",合并成功。如果rebase编辑时出了问题,可以用git rebase --edit-todo重新编辑,然后键入git rebase --continue重新执行即可。
修改commit注释:https://blog.csdn.net/u013190088/article/details/82026442
使用git stash进行暂存和恢复:https://blog.csdn.net/daguanjia11/article/details/73810577
二、远程多分支操作
从远程仓库克隆特定分支
$ git clone -b <branchname> http://address.git
Disgard all local changes and pull from remote:
$ git reset --hard origin/XXX
将本地分支推送到远程
$ git push http://address.git <branchname>
若要将本地commits强制覆盖到远程(如本地合并了已push的commits,想要与远程同步),则使用--force:
$ git push --force origin
拉取远程指定分支方式如下
$ git pull http://address.git <branchname>
删除推送到远程服务器上的分支
$ git push origin --delete <branchname>
另一种方式:
$ git push http://address.git :<branchname>
当clone了远程的某个分支,需要使用其它分支时,注意此时不能直接使用 git checkout <远程分支名> 来切换,否则会显示"You are in 'detached HEAD' state. You can look around, make experimental..."。可采用如下方式:
$ git branch -a #查看所有分支
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/branchname-1
remotes/origin/branchname-2
$ git checkout branchname-1
Merge Request:在所用的Git平台上选择这一项并提交即可。Source branch是要提交的分支,Target branch是要合并到的分支。
三、问题解决记录
1、mac下忽略.DS_Store
.DS_Store是Mac OS系统目录下自动生成的一种文件,git中可以选择直接删掉或忽略掉该类型文件。设置忽略可在项目目录下进行如下操作:
$ touch .gitignore #如果不存在的话
$ vim .gitignore
将光标移到最后并加入两行
.DS_Store
*/.DS_Store
保存并退出。此外也可以进一步进行全局设置,从而在所有git项目都忽略该类型文件
2、push时遇到报错 error: insufficient permission for adding an object to repository database ./objects
出现该问题是因为在远程服务器该路径下没有写操作权限。可以采用chown -R 777的方式获得权限。
3、当出现repo下又有clone的子repo的情况时,commit时会出错:changes not staged for commit
解决方法:https://blog.csdn.net/logan_LG/article/details/81502260
4、如本地分支版本高于远程分支,则pull远程分支时git会自动进行merge,并弹出一个界面提示“Please enter a commit message to explain why this merge is necessary”,需要进行编辑并保存退出。网上关于这里的介绍基本都是vim的,但ubuntu中debian的默认编辑器是nano,操作方式并不同。退出编辑界面的方式是^X,即ctrl + x。更多nano介绍可以参考 https://blog.csdn.net/vagrant2005/article/details/4458074
5、远程push某分支时提示“ ! [remote rejected] project-name -> project-name (failed to lock) error: failed to push some refs to 'http://****.git'”
很奇怪的一个问题,怀疑是权限问题但也没找到哪里出了问题。最后的解决方法是,新开一个branch,先不做任何修改push到远程,然后再将文件改动并在本地commit,并一次性提交到远程。(只要先本地commit再push就被reject了)
References:
本文详细记录了使用Git进行远程多分支协作的过程,包括本地常用操作如提交、回退、分支管理等,以及远程分支的克隆、推送和问题解决,如权限问题、.DS_Store忽略、commit错误等常见问题的解决方案。

被折叠的 条评论
为什么被折叠?



