①Create a warehouse locally and upload the code to the remote warehouse||②Clone the code of a remote warehouse for the first time, create a local branch and push the branch to the operation flow record of the remote warehouse

Operation 1: Create a warehouse locally and upload the code to the remote warehouse

#1、创建一个空的仓库文件夹并进去该文件夹
mkdir MySelfProjTest
cd MySelfProjTest/
#2、将文件夹初始化为一个git仓库
git init
#3、添加文件或编写代码
echo "selfproj">selfproj.txt
...
...
#4、添加文件并提交
git add .
git commit -m "selfproj"
#5、给远程仓库起别名,将链接https://github.com/xxx...标记为别名origin
git remote add origin [email protected]:dong-zhaocheng/self-test.git
#6、向远程仓库origin推送代码:将本地分支推送到远程的指定分支
#   git push -u origin 本地分支名:远程分支名 
#   git push <远程主机名> 本地分支名:远程分支名
git push -u origin master:master
# 此时远程仓库的master分支就已经同步了本地仓库的master分支了,结果如下图所示:
# 解释-u参数:
# 用于将本地仓库的master分支与远程仓库origin的master分支进行绑定,
# 之后我们再进行推送操作时,就可以只用git push了,推荐看下文末的参考文献2.

insert image description here


Operation 2: Clone the code of a remote warehouse for the first time, create a local branch and push the branch to the remote warehouse

#1、克隆远程仓库代码:clone用于本地没有任何线上仓库中的代码,运行命令会将线上仓库的代码全部拷贝到本地
#   注意:clone命令会自动将远程仓库的url定义为别名origin
	git clone 远程仓库
#2、查看所有分支
	git branch
#3、创建新的分支
	git branch new-branch-name
#4、切换到要关注的分支
	git checkout 分支名
#5、在新的分支进行开发或修改工作
	....
	....
	....
#6、将新的本地分支推送到远程仓库/远程主机
	git push origin 本地分支名:远程分支名 #可以直接推送成功

insert image description here
insert image description here

If you want the command to be less troublesome when pushing , you can set the upstream mapping branch of the current branch (that is, the default push branch of the current branch)

If not set, git pushan error will be reported directly, so you need to set the default upstream branch of the current branch first

git push --set-upstream origin new-dzc:new-dzc #如果两个分支名字相同则可以省略后面的分支名
#该命令的意思是设置当前分支的上游分支为origin主机的new-dzc分支

#当然也可以使用上文所述的 -u 参数
git push -u origin new-dzc:new-dzc
or
git push -u origin new-dzc

After setting, you can directly git pushgo to the corresponding branch of the remote warehouse in the current branch

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-wtzEExXZ-1690039111427) (C:\Users\DongZhaoCheng\AppData\Roaming\Typora\typora-user-images\ image-20230722205032577.png)]insert image description here

Also note:
git pull origin dev
#等价于下面的两条命令
git fetch origin dev	#将远程仓库的版本拿到本地版本库但是不合并
git merge origin/dev	#将已拿到本地版本库的版本合并到工作区

git pull origin devThe command is equivalent to

git pull https://github.com/xxx devOrder

The way to set the remote warehouse alias has been pointed out above

Related references:

①git-basic-use of remote warehouse

②The meaning of git push -u parameter


If this article is useful to you, you can like and bookmark this article. It will be much easier to find next time you use it. It would be great
if you can follow the author. The author will continue to learn, output, and share! Thank you for your encouragement!

Guess you like

Origin blog.csdn.net/qq_40459977/article/details/131873840