git-repo

Basic cover in the development of the use of Git commands, to meet the daily needs.


directory

  • configuration
  • Generate SSH_Key
  • Initialize the local repository
  • File status
  • The log
  • cloning
  • See the branch
  • Switch branch
  • Create a branch
  • Delete the branch
  • Rename branches
  • The code merge
  • The staging
  • delete
  • submit
  • push
  • Pull up the latest content
  • View file changes
  • Rollback version
  • undo
  • The label
  • Rebase
  • Git Flow
  • The child module
  • help
  • other

configuration

View the global configuration list
git config -l
View the list of local configurations
git config --local --list

# check global username/mailbox
git config --global --get user.name
git config --global --get user.email

Set global username/email
git config --global user.name "xiejiahe"
git config --global user.email "[email protected]"

Set the local current workspace repository username/mailbox
git config --local user.name "xiejiahe"
git config --local user.email "[email protected]"

Set the default text editor to emacs
git config --global core.editor emacs

# Set the default differentiation analysis tool to Vimdiff
git config --global merge.tool vimdiff
Copy the code

Generate SSH_Key

# 1 paste the following command and replace it with your GitHub email address
ssh-keygen -t rsa -b 4096 -C "[email protected]"

# 2. When prompted to Enter the file in which you want to save the key, press Enter. Accept the default file location.
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

# 3. At the prompt, type a security password.
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
Copy the code

Finally, you need to add the generated SSH Key to the SSH config

# 1. Edit
vim ~/.ssh/config

# 2. Paste the following into the config file
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa
Copy the code

Initialize the warehouse

Git init Creates an empty Git repository or reinitializes an existing repository

Git will be generated in the current directory
git init

# Create in quiet mode, will print only error or warning messages
git init -q

Create a bare repository. We don't usually use this command
git init --bare
Copy the code

File status

Check file status completely
git status

Output in short format
git status -s

# Ignore submodules
git status --ignore-submodules
Copy the code

The log

View the complete history of submissions
git log

Commit message
git log2 -Diff = diff = diff
git log -p -2

# Search keywords
git log -S Java

Only merge logs are displayed
git log --merges

# View logging by graph, --oneline optional
git log --graph --oneline

# list committer contributions, only author and contribution numbers will be printed
git shortlog -sn

# Sort by number of committed contributions and print message
git shortlog -n

Use the mailbox format to view the contribution degree
git shortlog -e

View the history of changes to the readme. md file, including the time, author, and content
git blame README.md
Copy the code

cloning

# the HTTPS protocol
git clone https://github.com/xjh22222228/git-manual.git

# SSH protocol
git clone [email protected]:xjh22222228/git-manual.git

-b specifies the branch name
git clone -b master https://github.com/xjh22222228/git-manual.git

Recursive cloning, useful if the project contains submodules
git clone --recursive [email protected]:xjh22222228/git-manual.git

The clone depth is 1, the historical records will not be cloned, this can save the cloning time
git clone --depth=1 https://github.com/xjh22222228/git-manual.git
Copy the code

See the branch

# View all branches
git branch --all

# View local branches
git branch

# Look at the remote branch
git branch -r
Copy the code

Switch branch

# 2 ways, switch to the Master branch
git checkout master
git switch master

Switch to the previous branch
git checkout -

Switch the remote branch
git checkout -t origin/dev
Copy the code

Create a branch

Create the develop branch
git branch develop

Create the Develop branch and switch
git checkout -b develop



Create an empty branch that does not inherit from its parent, and the history is empty. Generally at least 4 steps are required
git checkout --orphan develop
This step is optional if you really want to create a branch without any files
git rm -rf .
Add and commit, otherwise the branch is hidden (note that a file must remain in the current workspace before performing this step, otherwise it cannot commit)
git add -A && git commit -m "Submit"
Push to remote
git push --set-upstream origin develop
Copy the code

Delete the branch

# delete local branch
git branch -d <branchName>

# delete remote branch
git branch -d -r origin/<branchName>
git push origin :<branchName>
Copy the code

Rename branches

Rename the current branch
git branch -m <branchName>
Copy the code

The code merge

Merge feature/v1.0.0 branch code into DevelopGit Checkout develop Git Merge feature/v1.0.0# or one stepGit merge feature/v1.0.0 developCopy the code

The staging

# save all temporarily
git add -A

Save a file temporarily
git add ./README.md

Add all files in the current directory
git add .

# Save a list of files temporarily
git add 1.txt 2.txt ...
Copy the code

delete

Reverse git add

# Delete the 1.txt file
git rm 1.txt
Copy the code

submit

# -m Submitted information
git commit -m "changes log"

# commit and display diff changes
git commit -v

The -m parameter must be specified
git commit --allow-empty-message

# Overwrite the last submission to ensure that the current workspace has not changed
git commit --amend -m "New Submission information"
Copy the code

push

# Push content to main branch
git push -u origin master

Local branch: remote branch
git push origin <branchName>:<branchName>

# short, default push current branch
git push

-f = --force
git push -f
Copy the code

Pull up the latest content

# recommended, because automatic merging will not be done
git fetch origin master

Git fetch git merge
git pull

Remote branch name: local branch name
git pull origin master:master

# if you want to merge with the current local branch, the < local branch name > after the colon can not be written
git pull origin master
Copy the code

View file changes

View all file changes
git diff

# View changes to specific files
git diff README.md

Git log to check the changes made to a certain version
git diff d68a1ef2407283516e8e4cb675b434505e39dc54

Check the history of a file
git log README.md
git show d68a1ef2407283516e8e4cb675b434505e39dc54 README.md
Copy the code

Rollback version

Roll back the previous version
git reset --hard HEAD^

Roll back the previous two versions
git reset --hard HEAD^^

Git log will see the commit ID
git reset --hard 'commit id'

The rollback version is not saved in git log, use it if you want to view it
git reflog
Copy the code

undo

Undo all files in the current directory
git checkout -- .

Undo specified file modifications
git checkout -- README.md

Specify./ readme. md to return the file from the staging area to the workspace
git reset HEAD ./README.md

# undo the commit and return to the workspace
git reset <commit_id>

Undo commit and undo the changes at the same time
git reset --hard <commit_id>
Copy the code

The label

# list all local tags
git tag

List all remote tags
git ls-remote --tags origin

# Find tags according to a specific pattern, '*' template search
git tag -l "V1.0.0 *"

Create a tag with annotations
git tag -aV1.1.0 -m"Label Description"

# Create a lightweight tag without any parametersGit tag v1.1.0If you forget to tag, you can check the commit ID in git log
git log
git tag -aV1.1.0 < commit_id ># push to remote, default is only local creationGit push origin v1.1.0# Push all tags to remote at once
git push origin --tags

You will need to run Git Push Origin v1.1.0 again to delete the remote tag
git tag -d v1.1.0

Delete the remote tagGit push Origin --delete v1.1.0# Check tagsGit checkout v1.1.0# view details about a local tagGit show v1.1.0Copy the code

Rebase

Git rebase can merge multiple commit records into one

The operation has been committed for the last 4 times
git rebase -i HEAD~4
Or commit_id
git rebase -i e88835de905ad396f61a0dc8c040a8ac8a34f3f8


Drop git rebase
git rebase --abort

The command is used to continue after conflicts are resolved
git rebase --continue
Copy the code

Git rebase merges multiple commits into one

GitFlow

Git Flow is not a built-in command and needs to be installed separately

Initialize Each repository must be initialized once

Usually enter to complete the default Settings
git flow init
Copy the code

function

# Enable new featuresGit flow feature start v1.1.0# Push to remote, which is a necessary step in teamworkGit flow feature publish v1.1.0The complete function merges the current branch into Develop and then deletes the branch back to DevelopGit flow feature Finish v1.1.0Copy the code

patch

Hotfix is a patch for the master

# Open a new hotfixGit flow hotfix start v1.1.0_hotifxPush to remoteGit flow hotfix publish v1.1.0_hotifxComplete the new hotfix, merge the current branch into Master and Develop, then delete the branch and go back to DevelopGit flow hotfix Finish v1.1.0_hotifxCopy the code

release

# open a new releaseGit flow release start v1.1.0Push to remoteGit flow release publish v1.1.0Merge the current branch into Master and Develop, delete the current branch and go back to DevelopGit Flow Release Finish v1.1.0Copy the code

Git flow schema


The child module

See git SubModule tutorial for details

# Add submodules
git submodule add https://github.com/xjh22222228/git-manual.git

There are two ways to update
# One step
git submodule update --remote
Or go to the submodule project and pull
git pull

The submodule branch points to the detached head
git submodule foreach -q --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master)'

Common indicates the name of a submodule. You need to delete three submodules
git submodule deinit <common>
Clear the submodule cache
git rm --cached common
# Submit code and push
git commit -am "Remove a submodule" && git push
Copy the code

help

Print all git commands in detail
git help

Print all git commands
git help -a

List all configurable variables
git help -c
Copy the code

other

# Check git version
git --version

Check the remote warehouse address
git remote -v

Remember to submit your account password
git config --global credential.helper store

Delete git usernames and passwords
# windows
git credential-manager uninstall
# mac linux
git config --global credential.helper ""
# or
git config --global --unset credential.helper

Clear the local Git cache
git rm -r --cached .
Copy the code

If you feel good, you can send a Star to encourage the author git-manual