Git basics: local warehouse creation and remote connection

1. Create a local git warehouse and connect to the remote warehouse

1.1 Set git basic configuration

git config --global user.name 用户名
git config --global user.email 用户邮箱
#查看配置
git config --list
#本地初始化git仓库
git init

 

 Note: After initialization, you must commit once before you can perform the following operations. The commit operation is as follows

touch readme.md
git add .&git commit -m valid

It is worth noting that if there is no file to commit under the git project , then it is useless to execute this way, so I touched a file above, and it is estimated that the initial master object is empty and cannot create a new branch based on the master. The reason is that the default master branch of the newly created git warehouse will not be actually established until the first effective commit, otherwise it will be like you declared an object but did not initialize it.

Note: Even if it shows that you are in the master branch after git init, but in fact you did not find the master branch after git branch -a, it may be the reason for the cache, you can git checkout master to switch to the master branch, and the problem can be solved

1.2 Connect the local warehouse to the remote warehouse

Before connecting the local warehouse to the remote warehouse, you first need to create a study warehouse on your own GitHub

Then associate the local study warehouse with the remote warehouse (please note that a local warehouse can be associated with multiple remote warehouses)

#这里使用ssh连接为例,当然也可以使用http连接
git remote add origin [email protected]:ReturnTmp/study.git 
#查看远程仓库fetch和push地址
git remote -v
#最后查看本地分支和远程分支关联情况
git branch -vv

Note: A new branch dev is created remotely, but I use the command git branch -r locally to view all remote branches. Without this new branch, we need to update the cache of the remote warehouse in the local warehouse, using git fetch origin Or git remote update origin --prune command to update, and then check git branch -r, you can see the update

1.3 The process of adding, deleting, checking, and modifying  submitted files in the warehouse ( workspace —> temporary storage area —> git warehouse) 

#查看文件工作区修改的状态
git status -s

#把工作区的文件放到暂存区
git add .  //将本目录下全部文件放入暂存区
git add 文件名 //放单个文件或文件夹
git add -A  //一次性的把仓库中的文件进行上传到暂存区

#暂存区的文件添加到git仓库
git commit -m 消息

#查看commit日志
git log

Detailed command of git log: Detailed explanation of git log command 

Blogs about branches: git basic commands: detailed explanation of branch commands - linux Xiaobai's Blog - CSDN Blog

Submit Simplified Operations

git commit -am '修改hello文件'
#等价于git add hello && git commit -m '修改hello文件'

Note: The prerequisite for use is that the hello file has been added, that is, it is in the tranced state 

undo add operation

You can use the command     git reset HEAD directly

This is the overall return to the last operation

Green text becomes red text (cancel add)

If a file is rolled back to the last operation:   git reset HEAD file name

The red word becomes nothing (cancel without add modification)

git checkout --file

Since a local warehouse can be associated with multiple remote warehouses, multiple "aliases" can be set to point to different remote warehouses (such as a GitHub, a GitLab, and a Gitee), and then conveniently and quickly pull Get the code of a remote warehouse or push the code to a remote warehouse.

# 添加 github 别名
$ git remote add github [email protected]:toFrankie/repo-demo.git

# 添加 gitlab 别名
$ git remote add gitlab [email protected]:toFrankie/repo-demo.git

# 添加 gitee 别名
$ git remote add gitee [email protected]:toFrankie/repo-demo.git
  • View the remote warehouse information associated with the local warehouse, you can view it in  .git/config the file or through  git remote -v the command.

After adding the remote branch, you can use the following command to associate the local branch:

git branch --set-upstream-to=origin/master master (the front origin/master is the remote branch name, and the back master is the local branch name)

Modify the remote branch

Generally, I am used to delete first and then add, as follows

git remote rm origin                 //删除现在的仓库地址
git remote add origin url           //url为你要改的新地址

Related information: Read Git carefully | Let you understand origin, HEAD, FETCH_HEAD related content - short book 

Guess you like

Origin blog.csdn.net/m0_63748493/article/details/125510551