2022-12-08

Git - Zero to One

Local Version Control: You have various files, but these files will only display as one file in your computer. This file will record the address of the other version files you have. (You have various files, but these files will only display as one file in your computer. This file will record the address of the other version files you wrote before.)
Centralized Version Control: For collaborative development, Student A and Student B will both go to a central server, most notably SVN, to get their code. If the central server goes down, then all of the history of the files will no longer exist. If the central server goes down, then all the history files will no longer exist. Computer A and Computer B can pull code from the central VCS Server, the representative product is SVN. But the fault is if the Central VCS Server died, no one can pull code from it.)
notion image
Distributed Version Control (Distributed Version Control): Everyone has their own local repository, the local repository will exist all the history of the version, even if the central repository hangs, the local also have all the history of the version. (Everyone has their own local repository, everyone should commit to their local repository first and then push it to the Central VCS Server. all history versions.)
notion image

How to install Git

SSH

mkdir ~/.ssh ssh-keygen -t rsa -C "sylgg0918@gmail.com" cat id_rsa.pub ssh -T git@github.com -> then press yes

How to use Git easily

  1. Configure your profile: username, configuration
    1. git config --global user.email "[邮箱]" git config --global user.name "[用户名]"
  1. Access to the folder you want to manage
  1. Initialize (Nominate)
    1. git init
  1. Managing
    1. // 添加某一个文件到 git 中, 被 git 管理 git add [filename] // 将当前文件下没有被管理的文件全部被 git 管理 git add .
  1. Generating Versions
    1. git commit -m ‘版本说明’
  1. Viewing the version history
    1. git log
       

Git's Three Partitions

Git has three major partitions, the working directory, the staging area, and the repository.
notion image
Why is there a staging area? Well, it's a way to give users a chance to cool off, so that if we want to git the file at first, but we regret it, we can take it out of the staging area.
notion image

Rolling Back

git reset --hard [版本号]
notion image
You should be aware that once you've rolled back a file, git log will no longer be able to see the version of the file you rolled back. However, it is possible to roll back, so you need to check the version number of the rolled back version via the
// 查看回滚前的版本 git reflog
notion image
Once you've seen the version number before the rollback, you can roll it back with, git reset --hard [version number].

Git Commands

To avoid having to work directly on the mainline, you can pull a branch and merge your work into the mainline after you've worked on the branch.
# 查看当前分支 git branch # 创建新的分支 git branch [分支名] # 切换分支 git checkout [分支名] # 合并分支(先 checkout 到 master 分支, 然后执行命令, 将其他分支合并到 master) git merge [被合并的分支名] # 删除分支 git branch -d [分支名] # 给分支改名 git branch -m [新的分支名] # 重命名远端分支 git branch -m [旧分支名] [新分支名] git push --delete origin [旧分支名] git push origin [新分支名] git branch --set-upstream-to origin/[新分支名] # 删除远端分支 git push origin --delete remoteBranchName
How to resolve conflicts when merging branches.
When a branch merge conflict occurs, you'll get an error message, and you'll need to find the appropriate file. Open it, find the conflict, and fix it manually. Then commit the file.
 

Github

notion image
There are three options here, readme for documentation, .gitignore for ignoring files that need version control, and license for adding a license such as whether or not it can be used commercially.
notion image
The first is to create from scratch without any problems, the second is to push what you have already created.
fatal: unable to access
'https://github.com/sylgg0918@gmail.com/Sugar.git/': The requested URL returned error: 400
 
If you're cloning the remote repository, the branch you've already created will only show a master branch at this point. This doesn't mean that your previous branch is gone, but it's not showing up here, and you can still use git checkout [branch name] to switch back to your previous branch.
# 1. Add an alias for a remote repository git remote add [remote_repository_name] [remote_repository_url] # 2. Push code to a remote repository git push -u [remote_repository_name] [remote_repository_url] # 3. Clone a remote repository git clone [remote_repository_url] # 4. Switch branches git checkout [branch_name]
 

Example 1

notion image
This is a development flow, and we're working from our dev branch at home, but we realize that C7 on the master branch is a merge of C5 and C6, so if we were to use C6, we'd be missing the C5 code. So the first thing we need to do is update our dev branch.
# 1. First, confirm you are on the dev branch git branch # 2. Merge code from the master branch into the dev branch git merge master # 3. Check if you have the latest version of the code git log # 4. Suppose you've developed several new features on the dev branch today and are about to leave work. At this point, you need to push the changes from the dev branch to the remote repository so you can work on them later from home. git add . git commit -m "Code written today" git push origin dev # 5. Once home, open your home computer and pull the code you wrote at work from the remote repository git pull origin dev # 6. After continuing to write code at home, you still need to push the changes to the remote repository git add . git commit -m "Code written while working overtime at home" git push origin dev # 7. The next day, back at the office, continue developing the code git checkout dev git pull origin dev # 8. After a day of development and testing, you're ready to deploy. First, switch to the dev branch, merge dev into the master branch, and then push the changes git checkout dev git merge master git push origin dev

Example 2

If you're working on a feature today, but you're only halfway through. When we left the office, we did a git add and a git commit -m to "write half of the feature" but forgot to push it to the remote. When we got home, we tried to pull down our half of the feature from the remote repository, but it wasn't there. So we had to write something else. Once we were done writing, we did a proper commit.
git add . git commit -m "At home" git push origin dev
The next day, when we arrive at work, we need to update the dev branch on the company computer, and if we've changed a line at home and at work at the same time, we'll get an error saying that there's a conflict. All we need to do is resolve the conflict. The error will say that the file in question has a conflict, so we'll open it up, resolve the conflict, and then commit and push it to the remote repository. When we get home, we can pull the code we wrote at work via git pull.
For the record, git pull origin dev is actually git fetch origin dev and git merge origin/dev, the first of which pulls the dev branch from the remote repository into the local repository, and the second of which merges the repository code into the branch.

Rebase

It's recommended that you don't merge records that have already been pushed to a remote repository, as this can cause problems if the local repository doesn't match the remote.
It will make git's commit logs more concise. The only thing we really care about when developing a feature is the branch we create at the beginning and the branch we create after the feature is finished. The branches we create in between can make the whole process cumbersome, and rebase merges those commits together.
Merge different commits (try to merge commits that have not been committed to the remote repository).
# 将当前记录到特定版本号合并成一次提交 git rebase -i [版本号] # 从 HEAD 开始的 3 条进行合并, 具体几条可以自行选择 git rebase -i HEAD~3
notion image
notion image
Implement branch merging (not merging, but merging).
notion image
#归并之前可以用图形的方式显示 git 的提交记录 git log --graph # 1. 首先切到C3 这次提交所在的分支上, 也就是 dev 分支. git checkout dev # 2. 将 master 分支的东西放到 dev 分支上来 git rebase dev master # 3. 切换到 master 分支 git checkout master # 4. 将 dev 分支合并到 master 分支上 git merge dev
Forgetting to commit code to a remote repository from a company computer
In this case, if we work on another version at home, and then come to the office the next day and do a git pull, we'll get a fork. To make it more aesthetically pleasing, we use git fetch to pull the code to the local repository, and then we merge the two versions with git rebase origin/dev. This way there is no fork.
notion image
If any of these three scenarios conflict during a rebase operation, the first step is to resolve the conflict.
The first thing you need to do is resolve the conflict, and then you'll be prompted for some commands, so just follow them. Next we need to run git rebase --continue
 

Configuration Files

  • Project configuration file: .git/config under the current project.
git config --local user.name 'ArtistS' git config --local user.email 'xxxx@gmail.com' Note: The command git remote add [remote_name] [remote_url] also adds the remote locally (--local) by default.
  • Global configuration file: ~/.gitconfig
git config --global user.name 'ArtistS' git config --global user.email 'xxxx@gmail.com'
  • System configuration file: /etc/.gitconfig
git config --system user.name 'ArtistS' git config --system user.email 'xxxx@gmail.com' Note: Requires root permissions
 

Git Secure Login

  • URL
Original address: https://github.com/ArtistSu/Sugar.git Modified address: https://[username]:password@github.com/ArtistSu/Sugar.git git remote add origin [modified_address]
  • SSH
git config --global user.name 'ArtistS' git config --global user.email 'xxxx@gmail.com' # Generate public and private keys (in the ~/.ssh directory) ssh-keygen -t rsa -C '' # Copy the public key and paste it into Github: Settings → SSH and GPG keys cat ~/.ssh/id_rsa.pub # Configure the SSH address in the local git repository git remote add origin [ssh_address]

gitignore