User Information Configuration

Configure the user name and mailbox globally
git congfig user.name "xxxx" --global
git config user.email "[email protected]" -global

View the configuration information of different scopes
git config --list --local
git config --list --global
git config --list --system
Copy the code

Initialize the warehouse

Initialize the repository in an existing project
cd <project-path>
git init

Create a new project and initialize the repository
git init <project-path>
Copy the code

Commit changes

Check git status
git status

Commit changes to a file
git add <file-name>
Commit all changes that have been tracked
git add -u
Commit all changes
git add --all
git add .
# granulate submit changes
git add -p

Commit changes to the repository
git commit -m "<message>"
Commit all tracked changes directly to the repository
git commit -a -m "<message>"
git commit -am "<message>"
# Modify the last commit
git commit --amend
git commit --amend --no-edit
Copy the code

View submission History

See the difference between workspace and staging
git diff
See the difference between a file workspace and the staging area
git diff -- <file-name>
See the difference between the staging area and the previous commit
git diff --cached
To show the difference between the workspace and the last commit
git diff HEAD
Use to show the difference between two submissions
git diff <commit-id> <commit-id>

# view all submissions
git log
# to view the last n submissions
git log -<number>
Display each log submission and each change
git log -p
Show a brief summary of each log committed and each change made
git log --stat
Display a line of shorthand information
git log --oneline
# Display with simple graphics
git log --graph

Display the last committed change
git show
Used to show changes for a specific commit
git show <commit-id>
The file name used to display the modification for a particular commit
git show --name-only <commit-id>

Print the commit ID of the most recent operation
git reflog
Copy the code

File delete & secure rename & ignore

Delete files from workspace and remove trace of a file from repository
git rm <file-name>
# remove trace only for the file, but keep the file in the workspace
git rm --cached <file-name>

# Secure rename
git mv <old-name> <new-name>

# Delete files that are not tracked and not ignored by.gitignore
git clean
# See which files will be deleted, but not actually deleted
git clean -n
Copy the code

Undo the changes and roll back the code

Undo a file modification in the workspace
git checkout <file-name>
Delete all files
git checkout .
Restore the specified files of a COMMIT to the staging area and workspace
git checkout <commit-id> <file-name>

Undo current staging changes
git reset <file-name>
# Version rollback
# only move the HEAD pointer in the warehouse, workspace and staging changes remain unchanged
git reset --soft <commit-id>
# move the position of the 'HEAD' pointer and use the rollback to version to reset the staging area, leaving the workspace changes unchanged
git reset --mixed <commit-id>
# move the 'HEAD' pointer position and reset the workspace and staging area using the fallback to version, keeping it consistent with the specified commit
git reset --hard <commit-id>

# reset modify
New commit resets the target commit
git revert <commit-id>
# resets the target commit, but instead of creating a new commit, changes the workspace and staging area
git revert -n <commit-id>
# reset multiple submissions (not including start-id, but including end-id)git revet <start-id>... <end-id># Continue undo after revert encounters a conflict and resolves it
git revert --continue
# disable Revert, but keep the current result
git revert --quit
# to cancel revert
git revert --abort
Copy the code

branch

# list all local branches
git branch
# list all remote branches (I'll explain what remote branches are later)
git branch -r
List all local and remote branches
git branch -a
View branch details
git branch -v

Create a new branch
git branch <branch-name>
Create a new branch and switch branches
git checkout -b <branch-name>
# Switch branches
git checkout <branch-name>
# Switch to the previous branch
git checkout -
# delete branches (for merged branches)
git branch -d <branch-name>
# delete branch (whether merge or not)
git branch -D <branch-name>

# merge branches
git merge <branch-name>
# do not merge in fast-forward mode
git merge --no-ff <branch-name>

Select a COMMIT and merge it into the current branch
git cherry-pick <commit-id>
Select the latest commit for the specified branch
git cherry-pick <branch-name>
# select multiple consecutive commits (open left, close right, not start-commit)git cherry-pick <start-comm-id>... Git cherry-pick <start-commid-id>^... <end-commit > git cherry-pick <start-commid-id>^... <end-commit-id>After resolving the conflict, proceed to the next cherry-pick
git cherry-pick --continue
Exit the operation and keep the current progress
git cherry-pick --quit
# Undo this operation
git cherry-pick --abort
Copy the code

On the Tag

# view all tags
git tag
Filter the corresponding tag
git tag -l <tag-name>
View all tags on a commit
git tag --points-at <commit-id>
# view a tag
git show <tag-name>
View all tags and their corresponding commit
git show-ref --tags

New tag #
git tag <tag-name>
Create a new tag in the specified commit
git tag <tag-name> <commit-id>
Add a tag and message
git tag -a <tag-name> -m <message>
# remove the tag
git tag -d <tag-name>
Copy the code

For emergency stopper, use Stash

# new stash
git stash
# specify the messge information you want
git stash save <message>
Add untracked files to the staging
git stash -u
Add all files to staging (even if they are ignored by Git)
git stash -a

View temporary history
git stash list
View a subspecific temporary store
git show stash@{<number>}

Retrieve the most recent temporary store
git stash apply
Fetch target staging
git stash apply <number>
Retrieve the most recent staging and delete the staging record
git stash pop

Delete the latest temporary save
git stash drop
Delete target staging
git stash drop <number>
# Empty history
git stash clear
Copy the code

rebase

# merge branches
git rebase <branch-name>
You can interactively manipulate all commits up to the commi-id (excluding commits pointed to by commit-id)
git rebase -i <commit-id
Copy the code

Remote warehouse

# pull code and use the default remote repository name
git clone <url>
# pull code, custom local warehouse name
git clone <url> <new-name>

Add remote repository
git remote add <remote-name> <remote-url>
# check remote storage information
git remote -v
Delete the remote repository
git remote remove <remote-name>
Rename the remote operation name
git remote rename <old-remote-name> <new-remote-name>

# Push branch to specific remote repository, if only one remote repository can be omitted, the first push needs to add the '-u' parameter
git push <remote-name> <branch-name>
# push the specified tag to the remote end
git push <remote-name> <tag-name>
# Push all tags to remote end
git push <remote-name> --tags

# pull update
git fetch
Git fetch && git merge
git pull
Use rebase to pull updates
git pull --rebase
Copy the code

More articles

  • Git is easier than you think — practice
  • Git is simpler than you think – Principles