GIT:A boon for open-source developers who need to collaborate on code.

Local Git Config

Create the Encrypted connection

  • Generate the SSH key
    1
    ssh-keygen -t rsa -C "[Email on Github]"
  • Put the public key(.pub) to the GitHub settings
  • Verify the connection’s available

Initial the Git configuration

1
2
git config --global user.name  "name" //你的GitHub登陆名
git config --global user.email "123@126.com" //你的GitHub注册邮箱

(Optional) Git configurations

Git proxy settings

1
2
3
4
5
6
7
8
9
10
# View all of the proxy settings
git config --global https.proxy

# Set the proxy
git config --global http.proxy [http://127.0.0.1:1080](http://127.0.0.1:1080/)
git config --global https.proxy [https://127.0.0.1:1080](https://127.0.0.1:1080/)

# Cancel the proxy settings
git config --global --unset http.proxy
git config --global --unset https.proxy

Ignore the files

  • Add the filename which should be ignored to the .gitignore file.

However, the files’ names will also be exposed in the .gitignore file. Therefore, if we want to ignore the files, we should add the file name(or the pathname) to the .git/info/exclude file.

Normal develop

Concept

image.png

Workflow

If you want to upload some codes to Github, you need to create a repository on Github before you use the git commands below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Simplist git upload flow
git init
echo "Something" >> README.md
git add README.md
git commit -m "something"
git remote add origin [ssh@your.repo]
git branch -M main # -M specifies the branch is a main branch
git push -u origin main # -u makes the after git push no need to specify the specific remote address

# View the git log
git log

# View the git status
git status

Open Source software developing flow

Git Flow

image.png

git branch

1
2
3
4
5
6
7
8
9
10
11
# Checkout all of the branches
git branch

# Delte a branch
git branch -d [branch_name]

# Switching the current branch
git checkout [branch_name]

# Create a new branch and Switch to it
git checkout -b [branch_name]

git commit

1
2
3
4
# Add a commit about this changing
git commit "[something]"
# Edit the added commit contents but hasn't pushed to the remote
git commit --amend

git rebase

1
2
3
4
5
# Rebase the current branch to the other branch's lastest version
git rebase [branch_name]

# If there's a conflict when you're branching, continue the operation(you should solve the conflict first)
git rebase --continue

git merge

1
2
3
4
5
# Merge another branch's lastest version to the current branch
git merge [branch_name]

# combine all of the commits to one commit, and merge it to the current branch
git merge --squash [branch_name]

git revert

1
2
# Revert a single operation by commit_id
git revert [commit_id]

git reset

1
2
3
4
5
6
7
8
9
# Reset your local git to a history point
git reset [file]|[history point]
git reset --soft [file]|[history point]
git reset --mixed [file]|[history point] # Defalut
git reset --hard [file]|[history point]

# Reset the local git to the previous version, every ^ means a previous version
git reset HEAD^
git reset HEAD~1

git tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# View all of the tags
git tag -n

# Use git log --decorate to confirm the tag's belonging
git log --decorate

# Create a tag to a commit at your local git
git tag [tag_name]

# Create a tag containing the content
git tag -am [content] [tag_name]

# Push the tags
git push origin --tags # Push all of the tags
git push origin tag [tag_name] # Push a certain tag

git restore

1
2
3
4
5
# Recover a file but not adding to the stage
git restore [file_name] # or we can use "git checkout [file_name]"

# Revoke the file adding from the stage
git restore --stage [file_name]

git cherry-pick

1
2
# Merge another branch's certain commit to the current branch
git cherry-pick [commit_id]|[branch_name]

Tips

The difference between merge/rebase/squash

  • The common merge could lead to the situation
    image.png

If we use different parameters rather than the common one.
image.png

--squash could combine all of the commits to be a single one, which makes the git timeline looks better.

  • Use the rebase as possible as we can(except the public branch)

    Though the merge is more convenient to use, however, if we always use squash to merge the branches, the author of the main branch will be full of the name of the maintenance personnel

image.png

  • merge should be used at the main branch, and rebase should be used at the feature branch

The difference between revert/reset

  • The public branch can not tolerate the “Rollback” thing, therefore the public branch mustn’t use the reset command. We can use the revert instead of it.
  • revert will add a new commit when we use it to revoke an operation.
  • When we use the reset command to roll back to the previous git point, it is necessary to use the push -f to force push our local git to the remote side.
  • The remote git side can not tolerate any differences between the remote and the local git, so whether we use the reset or the rebase command, we should push -f.

GIT:A boon for open-source developers who need to collaborate on code.

https://resek4.github.io/2023/01/10/Git/

Author

Resek4

Posted on

2023-01-10

Updated on

2023-03-22

Licensed under

Comments