Git Global Settings:

1
2
git config --global user.name "wanzi"
git config --global user.email "iwz2099@163.com"

Git Commit Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
git clone git@github.com:iwz2099/test.git
cd test
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
# The above command pushes the local master branch to the remote origin, and -u sets the default remote branch as origin. After this, you can use `git push` without any arguments.

# Push all local branches to origin
git push -u origin --all  

# If development is based on the local `case_dev_wanzi` branch, push it to the remote `case_dev` branch
git push origin case_dev_wanzi:case_dev

Git Query and Cleanup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
git status   # Check current branch status
git log      # View commit history of current branch
git log -n3  # Show last three commits
git log -p -2  # Show patch differences for each of the last two commits
git log --stat  # Show summary statistics for each commit
git log --pretty=oneline # Display each commit on a single line; options include short, full, fuller
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h %s" --graph  # Use ASCII art to visualize branching and merge history
git reflog # Show all operations across all branches (including commits, resets, and deleted commits)
git reflog # Show last 10 entries
git grep -n wanzi  # Search for 'wanzi' in commit history and working directory
git grep --count wanzi  # Count occurrences of 'wanzi'
git clean  # Remove untracked files not ignored by .gitignore
git clean -d  # Clean entire working directory
git clean -d -n  #-n tests cleanup without actually removing files

Git Branch Operations:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
git branch # List current branches
git branch -v # Show last commit for each branch
# Create local dev branch from remote master
git checkout -b dev origin/master 

git branch --merged # List branches merged into current branch
git branch --no-merged # List branches not yet merged into current branch

# Start new feature development: create and switch to new branch
git checkout -b dev-20180720-111111-wanzi
Equivalent to:
git branch r-20180720-111111-wanzi
git checkout r-20180720-111111-wanzi

# Merge completed feature into master
git checkout master
git merge dev-20180720-111111-wanzi

Git Merge Operations

Create a new local branch issue54 from remote master, then develop on it.

1
2
git remote add origin git@github.com:iwz2099/test.git
git checkout -b issue54 origin/master

After feature completion, merge changes back to remote branch.

1
2
3
4
5
git fetch origin    # Fetch updates from remote
git merge origin/master  # Merge remote master into current branch
git push origin master  # Push code to remote master

git log --no-merges issue54..origin/master  # View commits in master not yet merged into issue54

Git Tag Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
git tag   # List current tags
git tag -l 'wanzi-2018*'   # List only tags matching pattern
git tag v1.8-20180720   # Create a lightweight tag (temporary use)
git tag -a v1.8-20180720 -m 'wanzi version 1.8'   # Create annotated tag with message; stores metadata
git show v1.8-20180720  # View tag details; compare with `git tag` output
git push origin tag_name    # Push specific tag to remote
git push origin --tags      # Push all tags to remote at once
git push origin :tag_name   # Delete remote tag
git checkout -b dev-20180720-111111-wanzi v1.2.0  # Create new branch from a tag
git branch -D dev-20180720-123333-wanzi   # Force delete an unmerged branch

Git Multi-Developer Collaboration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Developer A creates feature1 branch from remote master
git checkout -b feature1 origin/master

# Developer B creates feature11 branch from remote master
git checkout -b feature11 origin/master

# Developer A finishes first, pushes to remote feature1
# Developer B later finishes, wants to integrate A's changes into feature1
git fetch origin
git merge origin/feature1
git push -u origin feature11:feature1

Git Unstage Files and Undo Changes

1
2
3
4
5
6
7
# Code added to staging area but not committed yet; revert staging
git reset HEAD .  or
git reset HEAD a.txt
# This only affects staging area; working directory remains unchanged unless further actions are taken

git checkout -- .    # Discard all changes in working directory, revert to last commit
git checkout -- a.txt # Discard changes in a.txt, revert to last commit

Git Rollback to Previous Commit

1
2
3
4
5
6
7
git log
git reset --hard <commit_id>  # Roll back to specific commit
git reset --hard HEAD^  # Roll back to previous commit
git reset --hard HEAD^^ # Roll back to second-to-last commit
git reset --hard HEAD~4 # Roll back to fourth-to-last commit

git push origin HEAD --force # Force push to overwrite remote history (use with caution if you have project admin rights)

Git Rollback Master to a Specific Tag Version

  • Create a temporary rollback branch from the release tag, then force-push it to remote master:
1
git checkout -b rollback_20180720 r-20180720-111225-wanzi
  • In GitLab project settings, disable protection on the master branch

  • Force push the rollback branch to remote master (this will reset master to the specified tag version):

1
git push origin rollback_20180720:master -f

Git Other Operations

  • Fix commit message before pushing
1
git commit --amend --only -m 'fix: fix a bug'  # Amend commit message
  • Correct author name/email before pushing
1
git commit --amend --author="wanzi <iwz2099@163.com>"