This is the first day of my participation in the August More text Challenge. For details, see: August More Text Challenge

preface

Common Git instructions, while sorting out the side of review, but also by the way get new instructions. By reviewing the past we learn the new 😉

Basic configuration

1 View and configure user information

  • Configure the user name:git config --global user.name "yourname"
  • Configure the user email:git config --global user.email "[email protected]"
  • View user information:git config --global --list
  • View user name and email:git config user.name ,git config user.email

2 Configure SSH 🔑

After configuring SSH, use the SSH address to clone the project without entering the account password. Once configured, forever used. 😉

Check whether SSH exists

  • cd ~/.ssh To find.sshfolder
  • ls 或 llTo check whether the interface existsid_rsa.pub
  • cat id_rsa.pubTo obtain the secret key

If SSH doesn’t exist, then —

Ssh-keygen -t rsa -c “[email protected]

To add a secret key to github, click your avatar → Settings → SSH and GPG keys → New SSH key → Paste the ssh-RSA string → Confirm → OK! 🎉

On the detailed operation and SSH, refer to: www.liaoxuefeng.com/wiki/896043…


Basic instructions

1. Warehouse Management

  • Create git repository locally:git init
  • Local repository associated with remote repository:git remote add origin [email protected]Note: Added on the first push after association-uParameters, namely:git push -u origin masterFunction:-u (--up-stream) is used to establish a local branch and a remote branch association, forming a pipe, then can simplify the command, directlygit push
  • Clone the remote repository:git clone [email protected]
  • View remote warehouse address:git remote -v
  • View remote warehouse and branch information:git remote show origin

2 Branch Management

  • View the current branch:git branch
  • View all branches:git branch -a
  • Create a branch:git branch xxx
  • Switch branches:git checkout xxx
  • Create and switch branches:git checkout -b xxx(Same as executing the above two commands in sequence)
  • Delete branch:git branch -d xxx
  • Forcibly delete unmerged branches:git branch -D xxx
  • Clean up remote deleted branches locally:git remote prune
  • Delete remote branch (not default branch) :git push origin --delete xxx
  • Merge branches:git merge xxx
  • Rebase:git rebase xxx
    • parameter--continue: When a conflict is encountered, continue after the conflict is resolved
    • parameter--skip: Skip the rebase operation

3 Code Management

  • Pull and push

    • View the comparison between the local and remote branches:git status
    • Pull the new code:git pull
    • addThe specifiedModify to the staging area:git add <file>
    • addallModify to the staging area:git add .
    • To commit changes to the staging area:Git commit -m 'comments'
    • Push to remote Warehouse:git push
    • View the code for this modification:git diff
    • Extract a commit:git cherry-pick <commitId>
  • Undo modify

    • Undo modifications to files specified in the workspace:git restore <file> 或 git checkout -- <file>
    • Undo the staging areaThe specifiedFile modification:git restore --staged <file> 或 git reset HEAD <file>
    • Undo the staging areaallFile modification:git reset HEAD
    • Back to the previous commit:git reset HEAD^
    • Fallback to specify COMMIT:git reset <commitId>
    • Roll back a commit:git revert <commitId>(Add a new COMMIT to cancel out the previous commit)
  • Dirty code

    • Storing dirty code:git stash
    • View the dirty code list:git stash list
    • Take out theA recentStash:git stash pop
    • Take out theThe specifiedStash:git stash pop@{0}
    • deleteThe specifiedStash:git stash drop stash@{0}
  • Code Management Schematic

  • Version rollback diagram

4 Label Management

  • Print all labels:git tag
  • Print a label that matches the re:git tag -l test**(to get totestBranch at the beginning)
  • Create a local label:Git tag test - 1.0.0
  • Creating a local label with remarks:Git tag -a test-1.0.0 -m "git tag"
  • Push label to remote warehouse:Git push origin test - 1.0.0
  • pushallLabel to remote warehouse:git push origin --tags
  • Deleting a local label:Git tag - d test - 1.0.0
  • Delete remote warehouse label:Git push Origin --delete test-1.0.0

5 Git Log

In the process of organizing articles, I discovered that Git Log has many ways to play, such as — Pretty supports custom output styles, -s can query logs by content, and there is a Git Shortlog, which can even submit records by staff and sort them. It’s more powerful than I could have ever imagined.

  • Branch contrast:git log master.. dev(Find commit with more dev than master)
  • To view local commit records:git log.git log dev
  • To view remote commit records:git log origin.git log origin/dev
  • Find submission records by content:git log -S "Hello World"
  • View the commit times for each user and comment:git shortlog
    • parameter-s: Omit the comment and return a single count
    • parameter-n: The output is decremented by the number of commits

Optional parameters

parameter instructions
--oneline Formatted, one-line display, more concise and clear display
--stat Displays the number of files that are added or deleted each time
--graph Graphical display of the submission history
--author Specify a user
--pretty=format Customize the output format
--since=<date> / --after=<date> After the specified date
--until=<date> / --before=<date> Before the specified date

Use the sample

Git log –author=”tangjinzhou” –after=”2021/6/1″ –before=”2021/6/30″ –oneline

Custom output format: git log – author = “tangjinzhou” – pretty = format: “h AD | | % % % s”

Parameter description of user-defined format

parameter instructions
%H The complete hash of the commit object
%h A short hash string of the submit object
%T The complete hash string of the tree object
%t A short hash string of a tree object
%P The complete hash of the parent object
%p A short hash string of the parent object
%an The name of the author
%ae The email address of the author
%ad Author revision date (available
%ar Author revision date, in terms of how long ago
%cn The name of the committer
%ce The email address of the submitter
%cd Submission date
%cr Date of submission, shown in terms of how long ago
%s Submit instructions

Git Log: git-scm.com/docs/git-lo…


Refer to the link

  • Git_Docs_pretty-formats
  • Git Tutorial by Xuefeng Liao
  • How do I use git’s cloud front end team at work
  • A Git Memo Guide for the first four years of a job — While You’re still young