git常见命令使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40012791/article/details/93506476

一、提交代码的流程

1、git commit -am '' 提交你的分支

2、git checkout develop 切换

3、git pull origin develop 最新的代码

4、git merge zxy 你的分支

5、git push origin develop 推送到远程

二、git个人开发的注意事项

1、一般接触到的分支有三个‘线上’、‘master’、‘develop’

①、基于‘master’分支新建分支,本地分支(除了开发内容)要与‘master’保持一致。

②、项目区分分支,每个项目都有基于‘master’新建的分支。

③、在本地分支只能操作,git pull origin master。

④、上线,将自己的分支合并到‘master’。

⑤、直接拉去远程的分支:git fetch origin develop:test

2、常用命令

①、修改本地分支名称,git branch -m 原分支名 新分支名

②、删除本地分支,git branch -D 分支名

       删除远程分支:git branch -a ;查看本地以及远程所有分支;git push origin --delete  分支名;

③、在合并一个分支后发现有很多冲突,如果想放弃这次合并,git merge --abort

④、在master上拉一个分支并切入这个分支,git checkout -b  分支名称

⑤、退回到上一次commit的版本,git log,git reset --hard commit_id(版本的号)

3、问题解决方案

1 git pull遇到错误:error: Your local changes to the following files would be overwritten by merge:

方法1:如果你想保留刚才本地修改的代码,并把git服务器上的代码pull到本地(本地刚才修改的代码将会被暂时封存起来)

git stash
git pull origin master
git stash pop

如此一来,服务器上的代码更新到了本地,而且你本地修改的代码也没有被覆盖,之后使用add,commit,push 命令即可更新本地代码到服务器了。

方法2、如果你想完全地覆盖本地的代码,只保留服务器端代码,则直接回退到上一个版本,再进行pull:

git reset --hard
git pull origin master

2 在git push origin master时出现以下这个问题时:

error: failed to push some refs to '[email protected]:yangchao0718/cocos2d.git
hint: Updates were rejected because the tip of your current branch is behin
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

出现错误的主要原因是github中的README.md文件不在本地代码目录中 可以通过如下命令进行代码合并【注:pull=fetch+merge]

git pull --rebase origin master

git push -u origin master

3 如果出现这样的错误:The file will have its original line endings in your working directory.

解决办法:

git rm -r --cached ./
git config core.autocrlf false
git add ./

4 git出现这样的错误:Git master branch has no upstream branch

$> git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
 
    git push --set-upstream origin master

原因分析:没有将本地的分支与远程仓库的分支进行关联。出现这种情况主要是由于远程仓库太多,且分支较多。在默认情况下,git push时一般会上传到origin下的master分支上,然而当repository和branch过多,而又没有设置关联时,git就会产生疑问,因为它无法判断你的push目标。

解决: 方法一:(远程分支存在的情况才能使用)

# 查看要指向的 repository
git remote -v
 
# 查看所有分支
git branch -a

git push --set-upstream origin master
# master: 远程branch
# oringin: 在clone远程代码时,git为你创建的指向这个远程代码库的标签,它指向repository。

方法二:根据需要,替换origin和master,此方法的好处是即使远程没有你要关联的分支,它也会自动创建一个出来,以实现关联。

git push -u origin master
 

5 出现这个错误:! [rejected] master -> master (fetch first)

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:bboyHan/golang-data.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.

原因分析:没有同步远程的master 解决:

git pull origin master

6 Git出现failed to push some refs to

描述:

$ git push -u origin master
To [email protected]:******/Demo.git
 ! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:******/Demo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方法:

(1).使用强制push的方法:
$ git push -u origin master -f 
这样会使远程修改丢失,一般是不可取的,尤其是多人协作开发的时候。
(2).push前先将远程repository修改pull下来
$ git pull origin master
$ git push -u origin master
(3).若不想merge远程和本地修改,可以先创建新的分支:
$ git branch [name]
然后push
$ git push -u origin [name]

7 Git出现fatal: refusing to merge unrelated histories的时候

描述:这是从远程库pull项目,合并文件发生的异常

解决方案:在pull的时候添加 --allow-unrelated-histories。

$ git pull origin master ----allow-unrelated-histories

这个功能是可以让大家不要把仓库上传错了,如果会加上这个代码,那么就是自己确定了上传。之前很容易就把代码传错了,现在可以看到,如果上传的不是之前的,那么就需要加代码。

8 当Git出现error:src refspec master does not match any 错误时:

描述:在push项目的时候,引发该异常。 原因分析:目录中没有文件,空目录是不能提交上去的,获取没有add、commit文件直接进行push了。 解决方案:

touch README
git add README 
git commit -m 'first commit'
git push origin master

9 git时出现fatal: Authentication failed for 'https://github.com/ ...

描述:使用的https提交,在用SourceTree提交代码时候发生错误,返回的错误提示说:fatal:Authentication failed for'https://github.com/... 解决方案:重新执行Git config命令配置用户名和邮箱即可:

git config -–global user.name "xxx" 
git config –-global user.email "[email protected]"

猜你喜欢

转载自blog.csdn.net/qq_40012791/article/details/93506476