第 2 節

Git in Practice

0瀏覽次數0訪問次數--跳出率--平均停留

Environment Setup

  1. Operating System: Linux, Windows, MacOS (This tutorial uses Linux as the example)
  2. 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

  1. First, create a file .gitignore.
  2. Create another file top_secret_file_not_to_be_open_sourced.py and modify its contents.
  3. Check the git status.
touch .gitignore
touch vinci_secret.py
vim vinci_secret.py
git status

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

Basic Branch Operations

  1. 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.
  2. Create a branch
git branch route2
#git branch 分支名
  1. Enter the branch
git checkout route2
#git checkout 分支名
  1. 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.

  1. Delete the files under this branch.
ls
rm -rf test.md
rm -rf vinci_secret.py
ls

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

  1. 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 test.md file, which was deleted in the route2 branch, has been restored. However, the vinci_secret.py file, which was also deleted, has not been restored. This is because the file was blocked by the .gitignore file during the commit and was never submitted to the local repository.

  1. Delete branch
git branch -D route2
#git branch -D 分支名

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

Merging branches

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

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

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

Merge conflict in branch

  1. Add a line of content to master and commit it.
vim test.md
git commit -am "master version4"

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

  1. 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

  1. Open the GitHub official website: https://github.com(如果你打不开,请自己找办法打开)
  2. Register and log in to your account.

  1. 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.

  1. 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.

  1. 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!

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

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

  1. 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).

  1. View the remote repository

The file has been modified!

  1. Modify the content of the remote repository.

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

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

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

  1. View the project modification history.
git log

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/

Tutorial: Setting Up a C++ Environment on Linux

Linux C++ Compilation Environment Setup

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

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

  1. Open the GitHub repository to view it.

音乐页