Theme NexT works best with JavaScript enabled
0%

Git Advance

Use the git more advanced

Background

Since I have started my graduate student life, I found there are some new needs to be proficiency with git. New needs as followed:

  • How to connect differenct remote repository : Both company and lab have requirements for code security and privacy. A lot of times, their codes is not allowed to open source. For this purpose, they build their own repository of code management. So the new demand for me is to how to push or pull my local git code to the differenct repository.
  • Checkout/Merge : Thanks to HIT-SZ and Tuling-Robot Company, they assined me a new computer when I worked there. When I am in the company and lab, I would like to use the desktop computer to code some code. While I am in other place, like dormitory and classroom, I prefer to use my laptop computer. So I need a synchronization tool to synchronize my code which might be coded in different computer. Because of its’ own characteristic, git looks perfect. But only when I am on top of some advanced operation, like checkout and merge, I can use git to satisfy my new demand.
  • How to use VSCode to git : VSCode is a really powerful software. Because of the lightweight, VSCode becomes my most commonly used software. And VSCode can extend git functionality by adding plug-in, it’s convenient.

How to connect differenct remote repository ?

References

Clear Mind

When people push code from local repository to remote repository or pull code from remote repository to local, they would say “I git my code”. In a way, we can think of git process as a data transfer between localhost and remote server. Essentially, git is a process of network communication. The communcation there is more like https rather than http, because https has a network layer called SSL, where it implements encryption algorithm.

Encryption algorithm can be divied into 2 classes, which is symmetic encrytion and asymmetic encrytion. For symmetic encryption, the same key is used for both encrytion and decryption. For asymmetic encryption, it uses public-private key encryption mechanism, which means the sender uses the private key for encryption while receiver uses the public key to decrypt.

Git communication process uses public-private key encryption mechanism. So when our local repository need to connect to different remote repository(etc. like n), we only need to generate n different public-private key pair.Meanwhile, the local repository need a ID to identify itself. In fact, the “ID” is user.name and user.email.

Practice-01 – SSH with code-hoster

First : Config your local ID

If you have configed the local ID, you can skip this step

1
2
3
# git config [option] field value
git config --global user.name "your_name"
git config --global user.email "your_email"

the change you make will save in the C:\user\<user_name>,you can modify the file to achieve the same purpose as the command line operation.

Second: Generate differenct public key for different remote repository

1
2
3
4
# Suppose the 2 repositories are github and gitee
ssh-keygen -t rsa -C '<github email>' -f ~/.ssh/id_rsa_github
ssh-keygen -t rsa -C '<gitee email>' -f ~/.ssh/id_rsa_gitee
# In Win10, the "~" means "C:\Users\a"

Third: Add public key to the website interface of the remote repository

In github, for example:

In gitee, for example:

Forth: Edit config file

The config file will guide the localhost choose a correct private key to connect with the remote repository.

file path: ~/.ssh/config

1
2
3
4
5
6
7
8
9
10
11
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile C:\Users\a\.ssh\id_rsa_gitee

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile C:\Users\a\.ssh\id_rsa_github

validation effect:

1
2
ssh -T git@github.com
ssh -T git@gitee.com

Practice-02: Host a project to a different repository

Suppose I have a local repository named TestGit with the following directories:

Step1: Create remote repositories on different hosting platforms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 0.precondition: cd the project direction

# 1.init local git repository
git init

# 2.set remote repository address
# git remote add <remote> <url>
git remote add origin git@github.com:Llunch4w/TestGit.git
git remote add gitee git@gitee.com:llunch4w/test-git.git

# 2.1 verify remote repository whether added successfully
git remote -v

# 2.2 (optional) you can rename remote
# git remote rename <old_remote_name> <new_remote_name>
git remote rename origin github

Step3-01: Push local code to remote repository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 0. If there are some files in the remote repository, the first thing is "pull"
# git pull <remote_name> <branch_name>
git pull github main

# (optional) If you don't want to explicitly specify the brach every operation, you can set a link between remote and local
# git branch --set-upstream-to=<remote_name>/<remote_branch> <local_branch>
git branch --set-upstream-to=github/main main
# After the relevance, you can pull directly without explicitly specify the branch
# like: git pull <remote_name>
git pull github

# 1. Submit all of the changes from workspace to the index
git add .

# (optional) After every operation you made, you can use "git status" to check file state
# git status [<filename>](if you omit the arg,console will show all files which are needed to be committed)
git status

# 2. Commit the content from index to local repository
git commit -m "<your comment about this commit>"

# 3. Push the content from local repository to remote repository
# git push <remote_name> [<remote_branch>](you can omit it when you finished the relevance)
git push github main

# (optional) The default branch in local is "master", while github choose "main" as the default branch
# There might be some errors there. you can change the branch name in local to avoid the mistake.
git branch -m master main

# (optional) If you don't want to link to a remote repository,you can remove the relevance
# git remote remove <remote_name>
git remote remove github

Checkout/Merge

Only a short while ago, I don’t think I will need to use git commands like checkout and merge someday.Therefore, I have been lack of motivation to study relevant knowledges. But today, I need.

There is a Git Cheat Book for you to find out how each command is used.

Despite the title of the section is checkout and merge, but we are not just talking about these 2 operations.

References

Advanced git add

1
2
3
4
5
6
7
8
9
10
11
12
# Ususally use
# Add all the changed files in current folder
git add .

# Add all files in current file
git add -A

# Add sole file
git add <file_name>

# you can choose change or not when query
git add -p

Checkout(Branch)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Create a local branch
git checkout -b <branch_name>

# change to another branch
git checkout <branch_name>

# push local branch to remote
git push <remote_name> <branch_name>

# delete local branch(fail will occurre when the branch is not merged)
git branch -d <branch_name>

# view all branches(including local branches and remote branches)
git branch -a

# view branch optinal
git branch -a [--merged/--no-merged]

# view local branches
git branch

# view remote branches
git branch -r

Merge

1
2
# merge <branch_name> to current branch
git merge <branch_name>

Reback

1
2
3
4
5
6
7
8
9
# change to the newest code
git reset HEAD
# change the specific file to the newest code
git reset HEAD --<file_name>

# change to the specific commit version
git reset <commit_ID>
# change the specific file to the newest code
git reset <commit_ID> --<file_name>

Log

1
2
3
4
5
6
7
8
9
10
11
# log changes made by someone, -l will log the commit ID
git blame <file_name> [-l]

# log all the commit
git log

# log the brief introduction of commit list
git log --oneline

# log with the time restriction
git log --since=yesterday

How to use VSCode to git

References