Git Daily Use

preface

This article will be a periodic summary of my git learning and daily use. I hope it will be convenient for me to refer to it while also helping you 🤭

Post the official documentation so you can learn more about Git


I met the git

  • Git is the most advanced distributed version control system in the world. Git was developed by Linus Torvalds, who is also the creator of The Linus system. To put it simply, git was born when a company took back the author’s right to use their version control system, and then the author developed his own distributed version control system.
  • Based on their previous experience with version control, the authors set several goals for their new system:Fast branch switching speed Small capacity (compression) Simple design Completely distributed Strong support for non-linear development patterns (allowing thousands of parallel development branches) Ability to efficiently manage very large scale projects like the Linux kernel (speed and data volume)Since its inception in 2005, Git has matured to a level of ease of use while remaining true to its original goals. It is extremely fast, ideal for managing large projects, and has an incredibly non-linear branch management system that can handle complex project development requirements.

Basic Concepts (Initialization)

Before working with Git, let’s do some basic configuration

$git config --global user.email $git config --global user.email $git init = $git config --global user.email $git init = $git Ignore some files, ignored files are not traceable (add.) // Note that if the file has been traced, delete the file first, and drag it in, it will take effect. Windows operating system can use the following tool to find. MacOs operating system under the direct global search open relevant files can ' 'Copy the code

Git workflow and regions

  • usegitJust a quick rundowngitThe process of using. First develop the code in the workspace, then use commands to track the code and add it to a place called the staging area, then use commands to annotate your code with relevant information and save it to the local repository. Finally, for code security or for later configurationci / cdPipelining is convenient. We submit the code to a remote repository and a simple code store is now complete.
  • The figure below is drawn according to personal understanding briefgitFlow chart, this diagram mainly containsgit çš„ Four areasAnd some common operations.

  • Workspace: the place where you normally develop the changed code, is the latest content that you see now. Whenever a file has been changed during development, you can check it in this area by using git status to display untracked files. In this area, you can use Git add to add workspace files to the staging area.

  • Staging area: the area where data is temporarily stored. Staging area marks what is managed by Git in the current workspace. This is where you can commit your code to the local repository via git commit.

  • Local repository: This repository is located locally on your computer and holds all submitted data. The code in this repository has been versioned once. Git push can then be pushed to a remote repository.

  • Remote repository: a server used to host code, the contents of which can be modified by collaborative local repositories distributed across multiple locations. When local code is lost, stored code can be pulled from a remote repository. You can also automate CI/CD deployment projects through remote repositories.

Basic commands

git clone

  • Clone the project from the remote Git repository
    $git clone -b branch path $git clone -b branch pathCopy the code

git branch

  • See the branch
    $git branch "newBranch" // Create a branch named newBranch based on the current branch Git branch -d branch deletes a branch forciblyCopy the code

git checkout

  • Switch branch

    $git checkout $git checkout -b newBranch $git checkout CommitHASH -b newBranch $git checkout. $git checkout. // Git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -d: git clean -dCopy the code
  • Git – Briefly describe three ways of ‘code rollback’

git status

  • Display workspace status
    $git status // Can be used to see which files are tracked/not trackedCopy the code

git add

  • Add files to the staging area
    $git add. // This will add all changes from the workspace to the staging areaCopy the code

git commit

  • Store the contents of the staging area to a local repository
    $git commit $git commit $git commit $git commit $git commitCopy the code

git merge

  • Merging branches
    $git merge branch // Merge other branches $git merge --abort // Abort the mergeCopy the code
  • For more on merging branches, check out this post I wrote earlier
  • Merge versus Reabse is the optimal solution

git fetch

  • Pull changes to the remote repository
    $git fetch // get updates for all branches of the remote repositoryCopy the code

git pull

  • Fetching changes from a remote repository and merging remote branches locally is equivalent to git fetch + get merge
    $git pull --reabse origin $git pull --reabse origin Branch (remote branch name):branch(local branch name) // Use the same method as git fetch + git rebaseCopy the code
  • Merge differs from REabse

git push

  • Push local files to a remote repository
    $git pull origin branch // Push current branch code to remote branchCopy the code

git log

  • Viewing the Submission Record

    $git log --graph --oneline $git log --onelineCopy the code
  • Git tips you didn’t know about

Work scene

Regret medicine

  • In our daily development, we occasionally accidentally submitted the wrong code, respectively in the workspace, staging area, version library and other scenarios above. Let’s see, what’s a good solution

    $git restore filePath (passage ten) $git restore filePath (passage ten $git commit $git commit $git commit $git commit $git commit $git commit $git commit The changed commitHASH value in the log is updatedCopy the code
  • This is done in the workspace, staging area, and commit object. How can we fix the error in the local/remote repository

  • Git rollback rollback git rollback rollback git rollback rollback

Git rebase optimizes your commit record

  • Git merge results in a fork and a commit record. Git rebase is a solution to the problem of too many forks and redundant commits

    $git rebase branch // Merge another branch and create a base change $git rebase continue // rebase Git rebase -i HEAD~x git rebase -i commitHash $git rebase -i commitHash // You can also use this command for the next to the current HEAD merge of the specified commitHashCopy the code
  • This is done in the workspace, staging area, and commit object. How can we fix the error in the local/remote repository

  • Git rollback rollback git rollback rollback git rollback rollback

Git cherry-pick select merge

  • Imagine a scenario where you want to add functionality from a newly developed version to a stable version. You can use the cherry-pick command to extract the commit associated with this feature and merge it into the branch of the stable version.

    $git cherry-pick commitHASH filePath $git cherry-pick commitHASH filePath $git cherry-pick commitHASH filePathCopy the code

Compare file version differences

  • In our daily work, we sometimes accidentally put the wrong code in a function code block, and find that the project doesn’t work, and we don’t know what changes we made. In this case, we want to take a look at the current file code and history. How can we do this without the help of plug-ins?
  • The current file workspace code is compared to the current file history commit record

    Git diff commitHASH filePath $git diff commitHASH filePath Compare the current workspace code with the current file of the historical commit record.Copy the code
  • Compares the current file staging area code with the current file history commit record

    $git diff --cached commitHASH filePath // Compare the current workspace code with the current file of the historical commit record.Copy the code
  • The current file version library code is compared with historical commit records

    $git diff commitHASH commitHASH2 filePath $git diff commitHASH commitHASH2 filePathCopy the code
  • Note: Remove filePath from the above comparison to compare the difference of the whole file under the current project

Code the staging

  • In our daily work, sometimes when we are developing feature A, the product tells you that feature B is urgent, but the code at this time does not constitute a commit. What should we do
  • Add: stores files that are currently tracked in the staging area but do not constitute a commit

    $git stash // Stores the unfinished changes on a stack where you can reapply them at any timeCopy the code
  • Search: View the temporary indexes that have been stored

    $git stash list $git stash listCopy the code
  • Use: Get the contents of the storage according to stash index

    $git stash apply stash@{index} //Copy the code
  • Delete: Removes stored content

    $git stash drop Stash @{index} : // Drop the stash@{index} store and remove the stash from the listCopy the code

With an alias

  • Git doesn’t automatically infer the command you want when you type part of it on a daily basis. If you don’t want to enter the full git command every time, you can easily set an alias for each command using the Git config file.

    $git config --global alias.ci commit $git config --global alias $git ci -m "XXX XXX XXX..." // You can configure multiple aliases at once, directly modify the configuration file. Gitconfig.Copy the code

tagging

  • We see a lot of this in open source projects.

  • Git tag $git tag $git tag $git tag $git tag $git tag Git checkout -b newBranch $git push origin tag $git push origin tag $ Git tag -d tag // Delete the tagCopy the code

Remote warehouse address

  • In daily development, the address stored in git repository will inevitably change the domain name/IP, etc. At this time, if the remote address changes, our local repository also needs to generate a link again
    $git remote add origin url $git remote set-url origin url $git remote add origin urlCopy the code

conclusion

  • Finally, I have completed a milestone of documenting Git myself. The above steps are used many times in my daily work. Rational use of Git will make their own development get twice the result with half the effort, I hope I can record and share at the same time, to different degrees to the students harvest, you see the difference, welcome to the comments section of communication and progress 🤭.

reference

  • How do I use Git in my work

  • Git commands used in daily work

Other Git related shares by author

  • The author’s Git column