Git common commands

Git common commands

New code base

git clone

Create a new Git repository in the current directory

$ git init

Create a new directory and initialize it as a Git repository

$ git init [project-name]

Download a project and its entire code history

$ git clone [url]

configure git config

Git's setting file is .gitconfig, which can be in the user's home directory (global configuration) or in the project directory (project configuration).

Display the current Git configuration

$ git config --list

Edit the Git configuration file

$ git config -e [--global]

Set user information when submitting code

$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

Remote sync git remote pull push fetch

#Download all changes from the remote repository

$ git fetch [remote]

show all remote repositories

$ git remote -v

Display information about a remote repository

$ git remote show [remote]

Add a new remote repository and name it

$ git remote add [shortname] [url]

Fetch changes from remote repo and merge with local branch

$ git pull [remote] [branch]

Upload the local specified branch to the remote warehouse

$ git push [remote] [branch]

Force push the current branch to the remote repository, even if there are conflicts

$ git push [remote] --force

push all branches to remote repository

$ git push [remote] --all

add git add

Add the specified file to the staging area

$ git add [file1] [file2] ...

Add the specified directory to the staging area, including subdirectories

$ git add [dir]

Add all files in the current directory to the staging area

$ git add .

Before adding each change, it will ask for confirmation

For multiple changes to the same file, multiple submissions can be implemented

$ git add -p

code commit git commit

Submit the staging area to the warehouse area

$ git commit -m [message]

Submit the specified file in the staging area to the warehouse area

$ git commit [file1] [file2] ... -m [message]

Submit the changes in the workspace since the last commit, directly to the warehouse area

$ git commit -a

Show all diff information when submitting

$ git commit -v

Use a new commit, replacing the previous commit

If the code does not have any new changes, it is used to rewrite the commit information of the last commit

$ git commit --amend -m [message]

Redo the last commit and include the new changes in the specified file

$ git commit --amend [file1] [file2] ...

View commit information git reflog/log

Show the last few commits of the current branch

$ git reflog

Display the version history of the current branch

 $ git log

Display the commit history, and the files changed with each commit

$ git log --stat

Search submission history, by keyword

$ git log -S [keyword]

Display all changes after a commit, each commit occupies one line

$ git log [tag] HEAD --pretty=format:%s

#Display all changes after a commit, its "commit description" must match the search criteria

$ git log [tag] HEAD --grep feature

Displays the version history of a file, including file renames

$ git log --follow [file]
$ git whatchanged [file]

Displays every diff related to the specified file

$ git log -p [file]

Show last 5 commits

$ git log -5 --pretty --oneline

Show all users who have submitted, sorted by number of submissions

$ git shortlog -sn

Displays who modified the specified file and when

$ git blame [file]

Compare git status/diff

Show files with changes

$ git status

Show the difference between the staging area and the working area

$ git diff

Show the difference between the staging area and the last commit

$ git diff --cached [file]

Show differences between the workspace and the latest commit of the current branch

$ git diff HEAD

Show differences between two commits

$ git diff [first-branch]...[second-branch]

Show how many lines of code you wrote today

$ git diff --shortstat "@{0 day ago}"

View git show

Display metadata and content changes for a commit

$ git show [commit]

Show files that changed from a commit

$ git show --name-only [commit]

Displays the content of a file at a commit

$ git show [commit]:[filename]

modify git mv

Rename the file and put this renamed into the staging area

$ git mv [file-original] [file-renamed]

remove git rm

Delete the workspace file and put this deletion into the staging area

$ git rm [file1] [file2] ...

Stop tracking the specified file, but the file will remain in the workspace

$ git rm --cached [file]

Undo recovery git checkout /reset

Restore the specified file in the staging area to the workspace

$ git checkout [file]

Restore the specified file of a commit to the staging area and work area

$ git checkout [commit] [file]

Restore all files in the staging area to the workspace

$ git checkout .

Reset the specified file in the staging area, which is consistent with the last commit, but the workspace remains unchanged

$ git reset [file]

Reset the staging area and workspace to be consistent with the last commit

$ git reset --hard

Reset the pointer of the current branch to the specified commit, and reset the staging area at the same time, but the work area remains unchanged

$ git reset [commit]

#Reset the HEAD of the current branch to the specified commit, and reset the staging area and work area at the same time, which is consistent with the specified commit

$ git reset --hard [commit]

Reset the current HEAD to the specified commit, but keep the staging area and working area unchanged

$ git reset --keep [commit]

Create a new commit to revoke the specified commit

All changes in the latter will be offset by the former and applied to the current branch

$ git revert [commit]

Temporarily remove git stash

Temporarily remove uncommitted changes and move in later

$ git stash
$ git stash pop

List all local branches

$ git branch

List all remote branches

$ git branch -r

List all local and remote branches

$ git branch -a

Create a new branch, but still stay in the current branch

$ git branch [branch-name]

Create a new branch and switch to that branch

$ git checkout -b [branch]

Create a new branch pointing to the specified commit

$ git branch [branch] [commit]

Create a new branch and establish a tracking relationship with the specified remote branch

$ git branch --track [branch] [remote-branch]

Switch to the specified branch and update the workspace

$ git checkout [branch-name]

switch to previous branch

$ git checkout -

Establish a tracking relationship between the existing branch and the specified remote branch

$ git branch --set-upstream [branch] [remote-branch]

Merge the specified branch into the current branch

$ git merge [branch]

Select a commit to merge into the current branch

$ git cherry-pick [commit]

delete branch

$ git branch -d [branch-name]

delete remote branch

$ git push origin --delete [branch-name]

$ git branch -dr [remote/branch]

tag git tag

list all tag s

$ git tag

Create a new tag in the current commit

$ git tag [tag]

Create a new tag at the specified commit

$ git tag [tag] [commit]

delete local tag

$ git tag -d [tag]

delete remote tag

$ git push origin :refs/tags/[tagName]

View tag information

$ git show [tag]

Submit the specified tag

$ git push [remote] [tag]

submit all tag s

$ git push [remote] --tags

Create a new branch pointing to a tag

$ git checkout -b [branch] [tag]

other

Generate a release-ready tarball

$ git archive

Tags: Java Linux git github

Posted by mr_tron on Mon, 16 May 2022 23:48:59 +0300