git instruction usage

Posted by vishi83 on Fri, 24 Dec 2021 23:10:06 +0100

git installation - Windows Based

(1) Download the installer directly from Git's official website, and then install it according to the default option.

After installation, find "Git" - > "Git Bash" in the start menu, and something similar to the command line window pops up, which indicates that Git installation is successful!

(2) View / modify user name and email address

  • View user name, email address and password:
git config user.name
git config user.email
  • Modify user name, email address and password:
git config --global user.name "username"
git config --global user.email "email"
git config user.password "123456"

Run in the project, create a new file directory, right-click to open Git Bash Here, open git's console window, and pull the latest project code by git clone 'git's project address'

(3) Execution instruction of git

When git is used to manage the project, it is necessary to distinguish the submission process between each state and each area

Workspace, Index / Stage, Repository

The normal process of using git in the project

Changes made in the project directory are saved to the workspace. After confirming the changes, add to and then commit

Directory of the workspace Git is git's version library.

  • git branch -a view all remote branches
  • git checkout dev switches to the dev branch for development
  • git status to check for changes
  • git add . Adding the modified file is actually adding the file modification to the temporary storage area;
    git add . // Submit all modifications
    git add README.md
    git add index.html
    
  • git commit -m 'submission description' - submitting changes is actually submitting all the contents of the staging area to the current branch.
    Writing of classification prefix for submission Description:
    • feature: new features
    • Fix: bug fix
    • docs: document modification
    • style: format modification
    • refactor: generate code refactoring
    • Test: Test addition, test refactoring, and no change in production code
    • chore: build task update, package manager configuration, etc. there is no change in production code
  • git pull pulls the content on the branch
    If there are modifications on the branch, the common pull cannot be down, or it is modified on the wrong branch! Can:
    • git stash puts changes into the staging area - you can pull or check out branches
    • git stash pop releases the contents of the staging area - conflict resolution
  • git push submit branch, enter * *: wq * * in English mode, and then press enter

Merge branch

Determine the content of the workspace and the submitted content, and then switch the git checkout master
Switch to the master branch, pull the latest content, and execute git merge dev to merge

git checkout master // Switch to main branch
git pull  // Function of pulling main branch
git merge dev // In case of conflict, commit after handling the local conflict
git push

View historical submission version record - git log

Display: from the latest to the farthest submission log, press enter to view the longer branches, and enter the English letter q to highlight them.
If you are too dazzled by too much output information, you can add the – pretty=oneline parameter

git log
git log --pretty=oneline

git reset command version fallback

First, Git must know which version the current version is
Use HEAD to represent the current version, that is, the latest submission. HEAD ^ indicates the previous version, and HEAD ^ ^ indicates the previous version. HEAD~100 indicates up to 100 versions
– hard parameter
The fallback method includes HEAD method and id version number fallback method. The version number can be written completely or the first 7 digits

git reset --hard HEAD^
git reset --hard feac7aaa698d8187afa2d077e7b24fc6c11dece2
git reset --hard feac7aa

Git reset head ^ < File > undo the modification of the staging area and put it back into the workspace

After commit, if you want to undo the commit modification, do not undo git add - return the code to the staging area
After the undo operation is completed, the previous commit state is restored

git reset HEAD^ <file> // Undo changes to a file individually
git reset --soft HEAD^ // Revoke the entire commit

Undo commit and undo git add Keep all change codes

git reset --mixed HEAD^
git reset HEAD^ // The effect of both is the same

Without reservation, delete the workspace, change the code, undo commit, and undo git add

git reset --hard  // Delete all local modifications

Create warehouse locally and submit to remote - create associated remote branch

// Currently, you want to create - test on the branch you want
git checkout -b my-test  // Create the local branch of my test under the current branch and switch to the new branch (- b)
git push origin my-test  // Push my test branch to remote
git branch --set-upstream-to=origin/my-test //Associate the local branch my test to the remote branch my test   
git branch -a //View remote branches 

Local / remote warehouse version fallback

git log //View the historical submission record, select the version number to be fallback, and a string of characters after commit
git reset --hard 8622e0f10...... // Fallback local version (local version)
git push -f //Force fallback to the current version (remote warehouse)

Remove local branch

// You need to delete the current branch after switching to another branch - test
git branch -d my-test
// Force delete:
git branch -D my-test
// Delete remote branch:
git push origin --delete [branch_name]

git fetch / / update all branches of this

Topics: git