Git common operation summary

Posted by FunkyELF on Fri, 31 Dec 2021 08:24:32 +0100

The summary of common operations in GIT learning process is convenient for query when forgetting

User name and mail global configuration

    git config --global user.name "user name" 
    
    git config --global user.email "User mailbox"
    
    git config --global -l //View the above configuration
  1. The first thing after installing git is to configure the user name and user mailbox with the above two commands, which is equivalent to the identity of the local computer
  2. It can not be the real user name and mailbox. For example, the user name and mailbox are Tom and Tom@gmail.com
  3. The – global parameter indicates that all warehouses in the machine will adopt this configuration
  4. After the configuration is successful, a message will appear in the user directory of disk C gitconfig file, which saves the above configuration

Local commit operation

    git init //New local warehouse
    
    git add [filename] //Workspace file add to staging area

    git add . //add all workspace files to the staging area (except those with ignore set)

    git commit -m "message"//commit to local library

    git status //View file submission status
  1. After using git init operation, a local file will appear Git hidden folder contains warehouse information and cannot be deleted
  2. It is recommended to fill in the symbolic submission information when executing git commit to facilitate accurate version fallback

view log

    git log //View log details

    git log --pretty=oneline // One record per line

    git log --oneline // One record per line is more concise than the previous method

    git reflog //One record per line shows the number of steps required to move to a version
  1. Commonly used are git log --oneline and git reflog
  2. Viewing the log can be used in conjunction with the git reset command for version control

version control

    git reset --hard [Hash value] //Accurately advance or retreat to a version according to the hash value displayed in the log

    git reset --hard HEAD^ //Version fallback

    git reset --hard HEAD~n//Version fallback n steps
    
    -- soft Parameters are only moved in the local library HEAD Pointer
    --mixed Parameters moved in local library HEAD Pointer reset staging area
    --hard Parameter local library move HEAD Pointer, reset staging area, reset workspace
  1. The most commonly used of the three parameters is – hard, which realizes the local library and the "advance and retreat" of the temporary storage area and the work area“
  2. git reset --hard HEAD ^ ^ back two steps, (several steps back)
  3. ^And ~ can only implement version fallback relative to the HEAD pointer, and cannot advance the version

ssh public key

	cd ~ //Enter the current user directory
	
	ssh-keygen -t rsa -C "User mailbox" //Generate ssh public key
  1. Enter the ssh keygen - t RSA - C "user mailbox" command and press enter multiple times. The ssh public key will be generated using the default configuration
  2. The generated public key is in the user directory ssh folder
  3. Select Add public key on code cloud or GitHub and copy ID_ rsa. Paste the contents of pub into the public key column and name the public key
  4. Project managers can use SSH url to clone. By default, there is no login account and password when pull ing and push ing, which is more convenient

Connect local warehouse to remote warehouse

    git init //Initialize a local warehouse

    git remote add origin https://gitee... // Connect to the remote warehouse (you can replace origin with another name)

    git push -u origin master //Submit to the main branch of the remote warehouse (the first submission plus the - u parameter)

    git push origin master -f // Force push

    git remote -v  //View remote warehouse information

    git remote rm origin //Delete origin alias
  1. git remote add origin https://gitee ... is equivalent to taking an alias for the remote warehouse as origin
  2. If the remote warehouse is completely empty, you can upload files to the remote warehouse directly by git push -u origin master
  3. If the git push -u origin master command reports an error, try to execute the git pull --rebase origin master command, and then push again

File comparison

    git diff [file name] //Workspace and staging files comparison

    git diff //Comparison of all files in workspace and staging area

    git diff HEAD [file name] //Compare all files in staging area and local library

    git diff HEAD^ [file name] //The staging area is compared with the previous version file of the local library

    git diff [Hash value] [fileneme] //Use the hash index value to compare files with a version of the local library

git branch

    git branch [filename] //Create new branch

    git checkout -b [branchName] //Create and switch to a new branch

    git branch -v //View branch information

    git checkout [filename] //Switch branch

    //Steps of merging branches: first switch to the branch you want to merge to (usually the main branch); Then use the following command
    git merge [Branch name to merge]
  1. By default, there are only master primary branches

git branch conflict modification

    //If there is a conflict during the merge process, first open the conflicting file, remove the special symbol, modify it to satisfaction, and then add and commit
    git add [filename]

    git commit -m "resolve the conflict"//Note that the file name cannot be added later

Code pull

    git fetch origin master // Pull the remote warehouse master branch code to the local without merging
    
    git checkout origin/master //Switch to the origin/master branch to view the remote warehouse file fetch ed locally
    
    git checkout master //Switch to the local warehouse master branch
    
    git merge origin/master //Merge the contents of the origin/master branch of the remote fetch with the local master branch
    
    git pull origin master //Pull the remote warehouse content to the local in one step
  1. git pull is equivalent to git fetch + git merge

Other operations

git clone https://gitee... //  Clone the original remote warehouse to local

clear //Clear screen

cat [filename] //view file contents

Topics: git