Git in Practice
Environment Setup
- Operating System: Linux, Windows, MacOS (This tutorial uses Linux as the example)
- Install Git
sudo apt-get update
sudo apt-get install git
git --version

Introduction to Common Commands
Linux creates a directory && and enters it
mkdir -p hellogit
cd hellogit

Set up Git username and email (MUST be set!!!)
git config --global user.name "tungchiahui"
git config --global user.email tungchiahui@gmail.com
Git initialization (making this folder controlled by Git)
git init

Create a file && view all files in the current folder
touch test.md
ls

Edit the file's text content.
sudo apt-get update
sudo apt-get install vim
vim ./test.md

Next, press the Insert key on the keyboard to enter edit mode and type a piece of text.

Press ESC to exit edit mode.

Press Shift + colon, then type wq and press Enter to save and exit.

Query the current file's status.
git status

On branch branch name
Untracked means the file is in the working directory.
Stage the file to the staging area
git add test.md

Commit the file to the local repository.
git commit -m "version1"
#git commit -m "提交的信息(可以理解为是注释)"

View the submitted project version.
git log

These lines mean that user tungchiahui (email: tungchiahui@gmail.com) committed a project version annotated as "version1" to the master branch on Saturday, December 30, 2023 at 00:42:47.
You can try modifying the file content and then commit again.
vim test.md
git add test.md
git commit -m "version2"
git log


How to ignore files you don't want to commit
- First, create a file
.gitignore. - Create another file
top_secret_file_not_to_be_open_sourced.pyand modify its contents. - Check the git status.
touch .gitignore
touch vinci_secret.py
vim vinci_secret.py
git status


- Modify the contents of
.gitignoreby writing the names of files you do not want to be committed into that file.
vim .gitignore
git status


Basic Branch Operations
- The role of a branch: it creates a new repository branch for a project, which is independent from other branches like master. You can perform independent add, commit, diff, and clone operations on it.
- Create a branch
git branch route2
#git branch 分支名
- Enter the branch
git checkout route2
#git checkout 分支名
- Query how many branches there are, and check which branch is currently active.
git branch

The current branch's files will perfectly inherit the files from the master branch.
- Delete the files under this branch.
ls
rm -rf test.md
rm -rf vinci_secret.py
ls

- Commit this branch to the local repository.
git commit -a -m "删库跑路"
#该指令是把git add和git commit指令一起写

- Check whether the branch affects other branches (switch to another branch to see the impact).
git checkout master
ls

It can be seen that the deleted test.md file has reappeared in the route2 branch, but the similarly deleted vinci_sercet.py file has not. This is because that file was excluded by the .gitignore file during the commit and will not be added to the local repository.
- Delete branch
git branch -D route2
#git branch -D 分支名

- Create a branch and switch to it immediately.
git checkout -b route3

Merging branches
- Modify the file content in the route3 branch and commit it.
vim test.md
git commit -am "route3 version3"
#-a -m也可以被省略成-am


- Switch to the master branch.
git checkout master
vim test.md

- Merge the route3 branch into the current branch.
git merge route3
#git merge 被合并的分支名
vim test.md


Merge conflict in branch
- Add a line of content to master and commit it.
vim test.md
git commit -am "master version4"


- Switch to the route3 branch, add content, and commit.
git checkout route3
vim test.md
git commit -am "route3 version4"


- Merge branch
git merge master

A content conflict has occurred. You need to manually resolve the text conflict! Alternatively, other methods can also solve it — you can search Baidu for help.
Remote repository GitHub
- Open the GitHub official website: https://github.com(如果你打不开,请自己找办法打开)
- Register and log in to your account.

- Create a new remote repository


It is recommended to configure this interface in detail, but since this is a demo, only the repository name has been set, and all other options remain at their default values.

If the image above appears, it means the creation was successful.
- Create a file and commit it.


Fill in the file name and file content, then click the green button to submit. You can also fill in a commit comment.


- Clone the remote repository to your local machine.

cd
mkdir repo_floder
cd repo_floder
git clone https://github.com/tungchiahui/test_repo.git

If you encounter an error, please check whether you can access GitHub normally!


- Modify the file content.
cd test_repo
vim test1.md



- Check which remote repositories this local repository is connected to.
git remote -v

- Push the local repository to the remote repository.
git push

Here you need to enter your GitHub username and password. The username is your GitHub username, but the password is your GitHub user token. The steps to generate a token are as follows:




Note can be filled in arbitrarily; for the expiration date, select "No expiration date."
Check all the boxes below that can be checked — these are the permissions.

Copy the token and paste it into the password field (the token will be hidden when typed in the terminal; just press Enter after entering it).

- View the remote repository

The file has been modified!
- Modify the content of the remote repository.

- Pull the remote repository content to the local repository.
git fetch

- View the differences between the local and remote repositories.
git diff origin/main
#git diff 远程仓库名/分支名

- If the content is fine, you can sync the remote repository to the workspace.
git pull

- View the project modification history.
git log

使用Pull Request合并代码
Pull Request简称PR,用于请求将一个分支中的修改合并到另一个分支。PR可以让项目成员在合并前查看代码变化、留言讨论和进行审查,因此团队协作时通常不会直接将未审查的代码push到main分支。
- 从main分支创建并进入一个功能分支
git checkout main
git pull origin main
git checkout -b feature/pr-demo
feature/pr-demo是本次示例的分支名,实际开发时应使用能表达修改内容的名称。
- 修改文件并提交到本地仓库
vim test1.md
按下insert按键,然后随便输入点正文.
For example
TEST11111

然后按下ESC,再输入:wq进行保存.
git add test1.md
git commit -m "修改test1文件"
- 将功能分支推送到Github
git push -u origin feature/pr-demo
-u会建立本地分支与远程分支的跟踪关系,之后在该分支上可以直接使用git push。

- 在Github仓库页面创建PR
推送新分支后,Github仓库页面通常会显示Compare & pull request按钮,点击该按钮。如果没有显示,也可以进入仓库的Pull requests页面,点击New pull request。

确认两个分支选择正确:
base: main:修改最终要合并到的目标分支。
compare: feature/pr-demo:包含本次修改的来源分支。
PR只能在两个不同的分支之间创建。确认下方显示的提交和文件变化没有问题后,填写PR标题和说明,再点击Create pull request。

- 审查并继续修改PR
创建PR后,可以在Files changed页面查看本次修改,也可以邀请其他项目成员进行Review。


如果审查后需要继续修改,不需要重新创建PR。只需在同一个功能分支上继续提交并push,新的提交会自动加入当前PR。
vim test1.md
按下insert按键,然后修改下正文.
For example
TEST222

然后按下ESC,再输入:wq进行保存.
git add test1.md
git commit -m "根据Review意见修改"
git push
你会发现又多了一个commit如下图.

点击Files changed页面查看本次修改:

- 合并PR
审查通过且代码没有冲突时,拥有仓库写入权限的成员可以点击Merge pull request,再点击Confirm merge完成合并。仓库如果设置了审查或自动测试规则,需要先满足这些条件才能合并。


- 同步main分支并删除已合并的分支
合并成功后,可以在PR页面点击Delete branch删除Github上的功能分支。然后返回终端,更新本地main分支并删除本地功能分支。


git checkout main
git pull origin main
git branch -d feature/pr-demo

向没有写入权限的仓库提交PR
如果想向其他人的开源项目贡献代码,通常没有权限直接将分支push到对方的仓库。此时需要先在Github上点击Fork,将该仓库复制到自己的账号下。
Fork完成后,克隆自己的仓库并记录原仓库地址:
git clone https://github.com/你的Github用户名/仓库名.git
cd 仓库名
git remote add upstream https://github.com/原仓库用户名/仓库名.git
git remote -v
其中origin指向自己Fork出来的仓库,upstream指向原仓库。接下来仍然需要创建功能分支,不要直接在main分支上修改。
git checkout -b feature/pr-demo
# 修改文件后提交
git add .
git commit -m "添加示例功能"
git push -u origin feature/pr-demo
推送后在Github上创建PR,并特别确认:
base repository是对方的原仓库,base是对方要接收修改的分支。
head repository是自己Fork的仓库,compare是自己刚刚推送的功能分支。
之后填写标题和修改说明,点击Create pull request即可向原项目提交PR。
VScode + Git
What is VScode?
VScode is a text editor, similar to editors like Vim, but it has a graphical interface, a wide variety of useful plugins, and can integrate a compiler environment. Ultimately, it can be customized by users into an IDE like Visual Studio.
VScode official website:
https://code.visualstudio.com/
Create a folder and open VSCode in that folder.

Create a new file


Initialize Git (similar to the git init command)


U stands for untracked status, meaning the file is still in the working directory.
Stage the file (similar to git add)

To cancel staging, click the minus sign (this does not cancel).

A is "added," meaning it is in the staging area.
Submit (similar to commit)

Enter the commit message and click the checkmark to commit.
Create a new branch and switch to it.


Modified content

Display M as modified status
View historical version comparison (click on file)

Submit

Switch back to the main branch.


Merge branch



Push to the remote GitHub repository
- 在下面这个界面找到
Publish to Github,可能会弹出来点击Sign In,然后按提示在浏览器中完成Github授权。

- Please provide the Markdown fragment you'd like me to translate.


- Open the GitHub repository to view it.


使用VScode图形化提交Pull Request
本节演示如何使用VScode创建、审查和合并Pull Request,整个过程不需要在终端输入Git指令。
注意:如果要使用Pull Request,功能分支提交完成后不要执行上文的“切换回main分支”和“合并分支”操作。应当将功能分支保持独立,通过PR审查后再合并到main分支。
- 安装Github Pull Requests扩展
打开VScode左侧的扩展页面,搜索GitHub Pull Requests,安装由Github发布的GitHub Pull Requests扩展。

- 从main创建功能分支
先点击VScode左下角的当前分支名,切换到main分支。在源代码管理页面点击选择Pull,保证本地main分支与Github上的内容一致。


再次点击左下角的分支名,选择Create new branch from...,选择main作为起点,然后输入一个能表达修改内容的分支名,例如feature/pr-demo。创建后VScode会自动切换到该分支。





- 修改文件并提交
在功能分支中修改文件并保存,然后打开源代码管理页面:

- 点击文件右侧的
+,将修改加入暂存区。
- 在上方输入提交信息,例如
Add PR demo。
- 点击
Commit完成本地提交。


commit后变为

- 在VScode中创建PR
打开左侧的Github Pull Requests页面,点击Create Pull Request。也可以按Ctrl+Shift+P打开命令面板,执行GitHub Pull Requests: Create Pull Request。

- 填写PR信息
在Create Pull Request页面中检查以下内容:
Base Repository:接收修改的Github仓库。
Base Branch:要合并进入的目标分支,通常为main。
- 来源分支:当前的功能分支,例如
feature/pr-demo。
Title:简洁描述本次修改。
Description:说明修改原因、主要内容和验证方法。
也可以根据需要添加Reviewers、Assignees和Labels。检查无误后点击Create提交PR;如果修改还没有完成,可以选择Create Draft创建草稿PR。

如果当前分支还没有推送到Github,VScode会提示发布分支。选择publish branch即可将当前功能分支推送到远程仓库。

- 审查并修改PR
创建成功后,VScode会进入PR的Review Mode。在这里可以查看提交记录和文件差异、留言、邀请审查,也可以继续修改代码。
可以查看合并差异:


如果需要根据审查意见修改代码,保持在当前功能分支上,然后继续使用源代码管理页面进行Commit.也就是可以继续修改.

同样先add再commit本次修改,可以命名为Add PR demo2.

最后Push。新的提交会自动加入当前PR,不需要重新创建PR。

可以看到如图,两次commit都被PR包含了.

也可以看到文件差异里,两次提交都被可以被合并到main了.

- 合并PR并清理分支
审查通过且自动检查完成后,拥有仓库写入权限的成员可以在PR详情页点击Merge完成合并。


合并完成后,VScode会提示删除远程和本地的功能分支。确认PR已经正确合并后再进行删除,最后切换回main分支并点击Pull获取最新代码。


紧接着PR会被自动关闭.
