Git common operations

Posted by gilbertwang on Fri, 15 Oct 2021 07:44:09 +0200

Git common operations

to configure

Official documents: https://git-scm.com/docs/git-config

levelParameter valueconfiguration filepriority
Warehouse--local default. git/config of the current project⭐⭐⭐
user--globalHOME/.gitconfig, HOME is the value in the system environment variable⭐⭐
system--systemGit installation directory \ etc\gitconfig

The configuration with high priority will override the configuration with low priority

see

git config --local  -l      //Warehouse level profile
git config --global -l      //User level profile
git config --system --list  //System level profile

Add or modify

Instruction structure: git config -- < level > < section. Key > < value >

# --local is the default parameter
$ git config user.name "testuser"

$ git config --local user.name "testuser"

$ git config --global http.proxy http://127.0.0.1:1080
$ git config --global https.proxy http://127.0.0.1:1080

$ git config --system user.email testuser@126.com

delete

Instruction structure: git config -- < level > -- unset < section. Key >

$ git config --global --unset http.proxy

Commit

Modify comments

Modify local submitted comments that have not been pushed to the remote

$ git commit --amend

Merge submission

Merge several historical commits of local branches

$ git rebase -i HEAD~5
# perhaps
$ git rebase -i b0b2b2b
# All submissions before b0b2b are merged. B0b2b itself is not merged

The editor will pop up after the rebase instruction. At this time, you can decide how to merge historical submissions

At the top is the latest submission

pick fc46668 finish2

pick dda4cfd temp

pick ef867e6 temp

pick b0b2b2b finish1

pick b29cc3d temp

# Rebase ba36014...b29cc3d onto b0b2b2b (5 commands)

#

# Commands:

# p, pick = use commit

# s, squash = use commit, but meld into previous commit

# ... ...

In the editor, the submission starting with pick is what we want to keep. For the submission that we don't want to keep, we can change pick to s

Like below

pick fc46668 finish2

s dda4cfd temp

s ef867e6 temp

pick b0b2b2b finish1

s b29cc3d temp

All s submissions will be merged into the latest pick submission above, which will become the following

commit fc46668 finish2 temp temp

commit b0b2b2b finish1 temp

Note: the last submitted (top) pick must be retained (if it becomes s, which submission will it be merged into)

After this operation, the editor will pop up. Select the comments after merging and submission, and change them at will

Branch

see

See which branches are local

$ git branch

establish

Create a new branch locally, but do not switch over

$ git branch <branch_name>

Create a new branch locally and switch over

$ git checkout -b <branch_name>

delete

Delete local branch

$ git branch -d <branch_name>
$ git branch -D <branch_name>

Delete remote branch

$ git push origin --delete <branch_name>

switch

Switch local branch

$ git checkout <branch_name>

Pull the remote branch code to establish a mapping with a newly created branch locally, and then switch locally to the newly created branch

$ git checkout -b <new_local_branch_name> origin/<remote_branch_name>

Mapping (Association)

View the mapping relationship between local branch and remote branch

$ git branch -vv

Local branch is associated with remote branch

$ git branch --set-upstream-to origin/<remote_branch_name> <local_branch_name>

rename

Rename local branch

# Not renaming the current branch
$ git branch -m <oldname> <newname>

# Rename current branch
$ git branch -m <newname>

track

see

View all files tracked by git under the current branch

$ git ls-files

cancel

Want git to stop tracking changes to a file or directory

# List all the files that will cancel tracking
$ git rm -r -n --cached <file>/<folder>

# Cancel tracking all the files listed in the previous step
$ git rm -r --cached <filename>/<foldername>

add to

Want git to track changes to a file or directory

$ git add <filename>

Topics: git