_Quick Start for Git

Keywords: Programming git ssh

Git is divided into 3 sections, workspace, staging area, local library

(ix) Resources

Additional information (concise): https://www.yiibai.com/git

Other data (more complex): https://www.liaoxuefeng.com/w...

Git Quick Start: https://www.bilibili.com/vide...

Git Advances: https://www.bilibili.com/vide...

Installation

  1. Official download: https://git-scm.com/
  2. Installation: next()
  3. Configuration Basic Information
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

# example
git config --global user.name "SunSeekerX"
git config --global user.email "1647800606@qq.com"

Common Instructions

instructions Explain
Initialization
git init Turn the local directory into a git local repository (execute once)
git remote add [remote address alias] [remote warehouse address] Associate your local warehouse with a remote warehouse (usually by associating an address)
Common operations
git status View local warehouse file status
git add -A Add all changed files from the entire workspace to the staging area
git commit -m 'Commit message' Place the staging area file in the local warehouse, followed by -m where this change is noted
git pull [remote address alias] [remote warehouse branch] Merge updates from remote warehouses (must be merged before push)
Git push-u [remote address alias] [remote warehouse branch] Push the commit of the local current branch to the remote specified branch, (-u specifies the remote address as default, then git push can be used without any parameters)
Create ssh key
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" Create an ssh key, return directly, the file exists, delete the directory directly without using it, and copy the ssh key directly if it is useful
clip < ~/.ssh/id_rsa.pub Put the key on the clipboard

_Git command line operations

(_) Local library operations

(viii) Basic operations

Basic Operational Commands

0_Initialization
git init
# Initialized empty Git repository in C:/Users/SunSeekerX/Desktop/git/.git/
1_Status View
git status
# View the status of workspaces, staging areas
2 Add Files to Staging Area
git add [file name]
git add . # Add all changed files (the object of the action is all changes in the current directory,.Indicates the current directory.)
git add -A # Add all changed files (the object of action is all changes in the entire workspace, regardless of the current directory.)
git add -u # Add all changed files (the object of action is the change that has been tracked in the same workspace, regardless of the current directory.)
3_Remove staging area files
git rm --cached [file name]
4_Staging area submitted to local library
git commit -m 'commit message'
5_View submission records
git log
# Print detailed log information
# (HEAD -> master) HEAD is pointer to current version
# 1. Multi-screen control
# 1.1: Page down
# 1.2: Page up
# 1.3: Exit
git log --pretty=oneline
# commit information is displayed in one line, hash value submitted is fully displayed. (commit version information prior to fallback version cannot be displayed)
git log --oneline
# commit information is displayed in one line, and hash value submitted is only partially displayed. (commit version information prior to fallback version cannot be displayed)
git reflog
# Displays commit information on a single line and the number of steps the HEAD pointer needs to move. (You can display commit version information before the fallback version)
# The first seven characters show hash
# HEAD@{1}: Show the number of steps to move within braces
6_version forward and backward
# Essential: Move the HEAD pointer to a specified commit version

### Based on Index Value [Recommended]
git reset --hard [hash]

### Use the ^ symbol: you can only go back
git reset --hard HEAD^
# One ^ step back, n steps back

### Use ~symbol: can only go back
git reset --hard HEAD~n
# I n dicates a step back


####### reset parameter interpretation
--soft 
# 1 Move HEAD pointer only in local library
--mixed 
# 1. The HEAD pointer will be moved in the local library
# 2. Reset the cache
--hard
# 1. The HEAD pointer will be moved in the local library
# 2. Reset the cache
# 3. Reset Workspace
7_Compare file differences
git diff
# Compare all files, workspace files and staging areas
git diff [file name]
# Compare workspace files with staging areas
git diff [Historical Versions in Local Library] [file name]
# Compare workspace files with historical versions in local libraries

(viii) Branch management

Explain

Introduction: In the process of version control, multiple lines are used to advance multiple tasks simultaneously.

Benefit: During the development of each branch, if one branch fails to develop, it will not affect the other branches. Delete and restart.

Initialize git repository to create a master branch by default

Other branch habits are named feature_[name]

The main branch has bug s to fix, it is customary to open the branch for hot_fix to fix, and fix pull-request to master branch

Because the main branch generally runs 24 hours uninterrupted

graph LR
    start(project) -- branch --- hot_Fix((hot_Fix))
    start --branch--> master((master))
    start --branch--> feature_theme((feature_theme))
    start --branch--> feature_game((feature_game))
    
    master --new_branch--> hot_Fix
    hot_Fix --fix_bug--> hot_Fix_commit_A
    hot_Fix_commit_A --merge--> master_commit_A
    
    master --commit--> master_commit_A
    
    feature_theme --success_commit--> theme_commit_A
    theme_commit_A --merge--> master_commit_A
    feature_theme --fail--> delete_branch
    
    feature_game --success_commit--> game_commit_A
    game_commit_A --merge--> master_commit_A
    feature_game --fail--> delete_branch
0_View all current branches
git branch -v
1_Create a new branch
git branch [branch name]
2_Switch Branch
git checkout [branch name]
3_Merge Branches
# 1. Switch to the branch that accepts the change (merged, new content added)
git checkout [branch name]
# 2. Merge Branches
 git merge [branch with new content]
4 Resolve conflicts
# Find Conflicting Files to Resolve Conflicts
# Add Files to Resolve Modifications
# commit

(_) Remote library operations

If the local initial warehouse needs to have a remote warehouse address added, the cloned warehouse does not.

You can use http or ssh

http needs account name and password, SSH needs to configure ssh-key

(viii) Basic operations - local initial warehouse

0_View the current warehouse remote address
git remote -v
1_Add remote warehouse address
git remote add origin [remote address]
2_Merge Remote Warehouse Code
git pull [Remote address alias ( origin)] [Remote Warehouse Branch]
# Equivalent to fetch + merge

# If fatal: refusing to merge unrelated histories appears
# Actually, the problem is because two things are not related at all git Library, one local library, one remote library, and then local to push to remote, remote# Duan feels this local library is not related to himself, so he tells us it cannot be merged
# Specific method, one method is to pull down the code from the remote library, put the code to join locally into the remote library, download it to the local library, and submit it it# Go, because in this case, the library you are based on is the remote library, which is an update
# Second method:
# Use this mandatory method
git pull origin master --allow-unrelated-histories 
#  Force merge of two unrelated branches
3_Push Code
git push [remote address alias (origin)] [remote warehouse branch]
4 Grab warehouse contents
git fetch [remote address alias (origin)] [remote warehouse branch]
5 Switch to Grab Branch
git checkout [remote address alias (origin)]/[remote warehouse branch]
6 Merge Remote Capture Content
git merge [remote address alias (origin)]/[remote warehouse branch]

(viii) Basic operations - cloning remote warehouses

Other operations are the same as basic operations - local initial warehouse

0_Clone Remote Warehouse
git clone [remote warehouse address]

(viii) Other operations

0_View current project profile
git config --local -l
1_View and edit the current project profile
git config --local -e 
# Equivalent to
git config -e 
#[remote "origin"]
#       url = https://gitee.com/SunSeekerX/test.git
#       fetch = +refs/heads/*:refs/remotes/origin/*
# Address and grab information for remote address `origin'is configured here
# [branch "master"]
#       remote = origin
#       merge = refs/heads/master
# The default pull and push remote addresses and merge branch names are configured here
2 Undo all current changes in the local workspace
git checkout -- .

Caution

0_win10 Switch Account

Using http on win10, win10 logs user names and passwords and needs to switch accounts to

Control Panel\All Control Panel ItemsCredential Manager finds its own account name and password to delete, and then it can re-enter the user name and password

1_Set ssh-key

Baidu Git Configuration ssh key

Fork and pull request

slightly

Posted by Rodney H. on Tue, 03 Sep 2019 15:07:52 -0700