【20230531】Git commands and related use of Github

1 Caching GitHub credentials in Git

  1. Download the corresponding one according to the systemGitHub CLI 2.25.1

2. On the github homepage, click on the user avatar and select settings->Developer Settings->Personal access tokens to apply for a key (mainly divided into Fine-grained tokens and Tokens classic) gh needs to use Tokens classic. (Authentication can be performed through the web page, so there is no need to apply for a key in advance)

  1. Enter the following content on the command line and proceed to the next step as required.
gh auth login

2 configuration

Git's settings file is .gitconfig, which can be in the user's home directory (global configuration) or in the project directory (project configuration). The configuration file can be viewed in the user directory eg. /home/lieryang/.gitconfig

# 显示当前的Git配置
git config --list
# 编辑Git配置文件
git config -e [--global]
# 设置提交代码时的用户信息
git config --global user.name "[name]"
git config --global user.email "[email address]"

# 代理设置
# socks
git config --global http.proxy 'socks5://127.0.0.1:1080' 
git config --global https.proxy 'socks5://127.0.0.1:1080'
# http
git config --global http.proxy http://127.0.0.1:7890 
git config --global https.proxy https://127.0.0.1:7890

# 只对github.com使用代理,其他仓库不走代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:7890
git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
# 取消github代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

# 颜色设置
git config --global color.ui true           # git status等命令自动着色
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto
git config --global --unset http.proxy     # remove  proxy configuration on git

3 Add/delete files

# 添加指定文件到暂存区
git add [file1] [file2] ...
# 添加指定目录到暂存区,包括子目录
git add [dir]
# 添加当前目录的所有文件到暂存区
git add .
# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
git add -p
# 删除工作区文件,并且将这次删除放入暂存区
git rm [file1] [file2] ...
# 停止追踪指定文件,但该文件会保留在工作区
git rm --cached [file]
# 改名文件,并且将这个改名放入暂存区
git mv [file-original] [file-renamed]

4 Upload the code to the warehouse

# 提交暂存区到仓库区
git commit -m [message]
# 提交暂存区的指定文件到仓库区
git commit [file1] [file2] ... -m [message]
# 提交工作区自上次commit之后的变化,直接到仓库区
git commit -a
# 提交时显示所有diff信息
git commit -v
# 将add和commit合为一步
git commit -am 'message'
# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
git commit --amend -m [message]
# 重做上一次commit,并包括指定文件的新变化
git commit --amend [file1] [file2] ...

5 Branch related commands

git branch # 查看本地分支,此命令会列出所有分支,当前分支前面会标一个*号
git branch -a  # 查看本地和远程分支
git branch 分支名 # 创建分支,仅仅保存本地,远程还需要push
git checkout 分支名 # 切换分支
git checkout -b 分支名 # 创建和切换分支
git checkout -d 分支名 # 删除本地分支
git push origin --delete 分支名 # 删除远程分支

6 tag tag management

git tag # 查看所有标签
git tag 标签名 # 创建tag
git push origin 标签名 # 因为创建的标签都只存储在本地,需要使用该命令推送标签到远程

7 Create a new code base (local + remote)

Remote warehouse creation is mainly performed through the gh command. There are two ways. Directly create a new remote repository (github repository), and then synchronize the remote repository to the local one. The other is to create a local warehouse first and then synchronize it to the remote warehouse.
无论那一种方式都可以添加origin
Please add image description

7.1 Method 1

Please add image description

7.2 Method 2

Please add image description

8 Submit files to the remote warehouse

Regardless of the above method to create a remote warehouse, after the creation is completed by default:

  1. The origin has been set and can be viewed by command git remote -v
  2. As long as no file is submitted, the branch is not created (the command git commit -m "notes" will create the master branch)

8.1 Add files to the warehouse area

# 添加当前目录的所有文件到暂存区
git add .
git commit -m "注释内容"
# 只有提交文件到仓库区,本地会自动创建master分支
# 可以使用 git branch 查看本地分支
# git branch -a 查看本地和远程分支
# -M 用于移动/重命名分支。 
# 将当前分支重命名为 "main"。如果在当前仓库中已经存在名为 "main" 的分支,那么该命令将强制重命名当前分支并覆盖现有的 "main" 分支。
git branch -M main 
# git remote -v 查看远程仓库URL和命名
# 下面命令的作用是将远程仓库的 URL 添加到你的 Git 存储库中,并将其命名为 "origin"
# 方式一创建,系统会自动添加
git remote add origin https://github.com/LieryangStack/Gtk4-Study-Note.git
# -u 参数的作用是将本地分支与远程分支进行关联,并将其设置为 "upstream"
git push -u origin main

8.2 Notes on the -u option

In the git push -u origin main command, the -u parameter is used to associate the local branch with the remote branch and set it to "upstream". Doing so will provide convenience in future push operations.

The specific functions are as follows:

  1. Push to remote branch: The git push command is used to push local commits to the remote repository. In this command, origin is the name of the remote repository and main is the name of the local branch to be pushed.
  2. Associate local branches with remote branches: The -u (or --set-upstream) parameter is used to associate local branches with remote branches. In this example, the main branch will be associated with the origin/main remote branch. This way, Git will remember that origin/main is the "upstream" of the main branch, which is the default tracking branch.
  3. Default push and pull settings: By setting "upstream" you can omit the name of the remote branch in future push operations. 例如,之后运行 git push,Git 将自动推送到 origin/main,而无需再次指定远程分支的名称。此外,在拉取操作中,git pull 命令也会自动拉取与当前分支关联的远程分支的最新更改.
  4. By using the -u parameter, you can easily establish the association between the local branch and the remote branch, and set the default push and pull behavior. This makes it easier for you to perform daily push and pull operations without having to specify the name of the remote branch every time.

9 questions

9.1 No relevant history

git pull origin main

Tips below:

fatal: refusing to merge unrelated histories

Solution

git pull origin main --allow-unrelated-histories

1.GIT is like a door
2.Git common commands, summarized very comprehensively!
3.GitHub CLI command line tool (gh)
4.git branches and versions

Guess you like

Origin blog.csdn.net/Creationyang/article/details/129754574