Git本地初始化仓库并提交至远程仓库切换分支的完成流程

一 创建一个本地仓库(Gitee

二 Idea中打开Terminal窗口

三 命令行初始化仓库并切换分支提交代码

//初始化本地库
C:\mark>git init
Initialized empty Git repository in C:/mark/.git/

//将本地代码加入本地暂存区
C:\mark>git add .
warning: LF will be replaced by CRLF in src/main/webapp/WEB-INF/web.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/main/webapp/index.jsp.
The file will have its original line endings in your working directory

//将本地暂存区代码提交到本地代码库
C:\mark>git commit -m "mark project"
[master (root-commit) 489f93a] mark project
 5 files changed, 113 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 README.md
 create mode 100644 pom.xml
 create mode 100644 src/main/webapp/WEB-INF/web.xml
 create mode 100644 src/main/webapp/index.jsp

//添加远程代码库的信息
C:\mark>git remote add origin https://gitee.com/username/superMark.git

//将本地库代码提交到添加的远程库中
C:\mark>git push -u origin master
To https://gitee.com/suername/superMark.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/username/superMark.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.//要求第一次提交到远程需要先pull代码
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

//从添加的远程库中pull代码
C:\mark>git pull
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://gitee.com/username/superMark
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

//再次提交代码到远程库
C:\mark>git push -u origin master
To https://gitee.com/username/superMark.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/username/superMark.git'
hint: Updates were rejected because the tip of your current branch is behind 
//是指本地分支不如远程的新,所以无法更新
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.

//强制将本地master分支覆盖远程的master最新分支,并提代码
//git push -u -f origin master 强制覆盖分支
C:\mark>git push -u -f origin master 
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (11/11), 1.55 KiB | 198.00 KiB/s, done.
Total 11 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/username/superMark.git
 + 8ede76f...489f93a master -> master (forced update)
Branch 'master' set up to track remote branch 'master' from 'origin'.

//本地创建新的分支并切换到新建的分支
C:\mark>git checkout -b v1.0 origin/master
Switched to a new branch 'v1.0'
M       README.md
Branch 'v1.0' set up to track remote branch 'master' from 'origin'.

//查看当前分支信息
C:\mark>git branch
  master
* v1.0

//将本地库新建的分支推送到远程库中,也可以理解为将当前所在的本地分支推送到远程仓库中,
此时远程库会自动产生一个与该分支同名的分支
C:\mark>git push origin HEAD -u
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
remote: Create a pull request for 'v1.0' on Gitee by visiting:
remote:     https://gitee.com/suername/superMark/pull/new/username:v1.0...username:master
To https://gitee.com/username/superMark.git
 * [new branch]      HEAD -> v1.0
Branch 'v1.0' set up to track remote branch 'v1.0' from 'origin'.

代码提交结果

远程分支结果:

猜你喜欢

转载自blog.csdn.net/calm_encode/article/details/106627276