Git is an open source distributed version control software designed for efficient and fast version management of projects from the very small to the very large. Git is everywhere in our daily software development. I’ve combed through the common commands in Git, as shown below.

Add/delete files

Adds the specified file to the staging area

git add [file1] [file2] …

Adds the specified directory to the staging area, including subdirectories

git add [dir]

Adds all files from the current directory to the staging area

git add .

Confirmation is required before each change is added

Multiple changes to the same file can be committed in batches

git add -p

Delete the workspace file and place the deletion in the staging area

git rm [file1] [file2] …

Stops tracking the specified file, but it remains in the workspace

git rm –cached [file]

Rename the file and place the rename in the staging area

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

Submit code

Submit temporary storage area to warehouse area

git commit -m [message]

Submit documents designated for the staging area to the warehouse area

git commit [file1] [file2] … -m [message]

Commit the changes to the workspace since the last commit, directly to the warehouse

git commit -a

Display all diff information when committing

git commit -v

Use a new COMMIT instead of the previous commit

If there are no new changes to the code, it is used to overwrite the commit information from the last COMMIT

git commit –amend -m [message]

Redo the last COMMIT and include new changes to the specified file

git commit –amend [file1] [file2] …

Branch management

See the branch

The git branch command lists all branches with an asterisk (*) before the current branch

git branch

View all remote branches

git branch -r

View all branches, local and remote

git branch -a

Delete the branch

Delete the merged branches. Deleting a branch is deleting a pointer

git branch -d <branchname>

If the

branch has not been merged, the changes will be lost if the branch is deleted. To force the deletion, add -d

git branch -D <branchname>

Delete the remote branch and push it to the server

git branch -d -r <branchname>

Deleting a Remote Branch

git push origin :<branchname>

Deleting a Remote Branch

git push origin –delete <branchname>

Create/switch branches

Creating a branch creates a pointer to the current COMMIT

git branch <branchname>

Switch to the dev branch

git checkout <branchname>

Create a new branch: dev, and switch to dev.

git checkout -b

Git branch dev git checkout dev git branch dev git branch dev

Rename branches

Rename the local branch

git branch -m <oldbranch> <newbranch>

Rename the remote branch

  1. Example Delete the remote branch to be modified
  2. Push the local new branch to the remote server

Merging branches

Once code on the dev branch meets the criteria to go live, it is merged into the Master branch

Git checkout master (dev); git checkout master (dev); git checkout master (dev

git merge dev

Yan together

Git rebase merges the current branch with mater. This operation is similar to merge the current branch (for example, dev), but has a different commit history. The rebase operation has a cleaner log.

git rebase master

Label management

List all tags

git tag

Create a new tag for the current commit

git tag [tag]

Create a new tag before specifying commit

git tag [tag] [commit]

Deleting a Local Tag

git tag -d [tag]

Deleting a Remote Tag

git push origin :refs/tags/[tagName]

Viewing tag Information

git show [tag]

Submit the specified tag

git push [remote] [tag]

Note: the default remote code base for [remote] is Origin

Submit all tags

git push [remote] –tags

Create a new branch that points to a tag

git checkout -b [branch] [tag]

If you want to edit the code under a tag, you need to pull the code corresponding to the tag snapshot onto a branch. Git checkout -b new_branch v1.0

Viewing Log Information

The changed file is displayed

git status

Displays commit logs from most recent to farthest

git log

View the last three commits

git log -3

Simplify the display of log output. The COMMIT ID is long

git log –pretty=oneline

See git branch merge diagram

git log –graph

See git branch merge diagram – Simplified

git log –graph –decorate –oneline -100

Remote synchronization

Download all changes to the remote repository

git fetch [remote]

Display all remote warehouses

git remote -v

Displays information about a remote repository

git remote show [remote]

Add a new remote repository and name it

git remote add [shortname] [url]

Retrieve the changes from the remote repository and merge them with the local branch

git pull [remote] [branch]

Upload the local specified branch to the remote repository

git push [remote] [branch]

Push current branch to remote repository forcibly, even if there are conflicts

git push [remote] –force

Push all branches to the remote repository

git push [remote] –all

undo

Restores the specified file from the staging area to the workspace

git checkout [file]

Restores the specified files of a COMMIT to the staging and workspace

git checkout [commit] [file]

Restores all files from the staging area to the workspace

git checkout .

Resets the specified file in the staging area, same as the last commit, but with the same workspace

git reset [file]

Reset the staging area and workspace as the last COMMIT

git reset –hard

Reset the pointer to the current branch to specify COMMIT, and reset the staging area, but the workspace remains the same

git reset [commit]

Reset the HEAD of the current branch to specify a COMMIT, and reset the staging area and workspace to match the specified COMMIT

git reset –hard [commit]

Reset the current HEAD to specify commit, but leave the staging area and workspace unchanged

git reset –keep [commit]

Uncommitted changes are temporarily removed and moved in later

git stash

git stash pop

My other Git related articles

  • Git command summary in daily work