一.上传本地代码到Github
(1)新建repository
(2)填写相关参数
(3)在本地新建一个项目,然后打开Git Bash Here,依次输入如下命令
1. git init
2. git add --all
3. git commit -m "xxx"
4. git remote add origin [xxx]
5. git pull origin master
6. git push -u origin master
(4)执行完上面的命令,提示如下错误
$ git push -u origin master
To https://github.com/AndryYu/KtHand.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/AndryYu/KtHand.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方法:
1. git pull origin master --allow-unrelated-histories
* (git pull --rebase origin master)
2. 然后手动修改冲突文件
3. git rebase --continue
4. git push -u origin master
二.切换远程分支
git clone只能clone远程库的master分支,无法clone所有分支。
1. 找一个干净的目录xxx
2. cd xxx
3. git clone http://myrepo.xxx.com/project/.git
4. cd project
5. git branch -a //列出所有分支名称如下
remotes/origin/dev
remotes/origin/release
6. git checkout -b dev origin/dev //作用是checkout远程的dev分支,在本地起名为dev分支,并切换到本地的dev分支
7. git checkout -b release origin/release //作用参见上一步解释
8. git checkout dev //切换回dev分支,并开始开发。
三.修改远程仓库地址
1.修改命令
git remote set-url origin [url]
2.先删后加
git remote rm origin
git remote add origin [url]
3.直接修改config文件 //没试过
四.提交代码
1. git add -A
2. git commit -a '提交说明'
3. git pull origin master //拉取远程仓库最新代码
3. git push origin 分支名
五.区别比较
1. git fetch与git pull的区别
git fetch
相当于从远程获取最新版本到本地,不会自动merge.
1. git fetch origin master //首先从远程的origin的master主分支下载最新的版本到origin/master分支上
2. git log -p master..origin/master //然后比较本地的master分支和origin/master分支的差别
3. git merge origin/master //最后进行合并
git pull
相当于从远程获取最新版本并merge到本地。
git pull origin master //上述命令其实相当于git fetch 和 git merge
在实际使用中,git fetch更安全一些。因为在merge前,我们可以查看更新情况,然后再决定是否合并。但是如果只关心冲突的话,可以用git pull,自动合并。