git clone git@github.com:iwz2099/test.git
cdtesttouch 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 origingit push -u origin --all
# If development is based on the local `case_dev_wanzi` branch, push it to the remote `case_dev` branchgit push origin case_dev_wanzi:case_dev
git status # Check current branch statusgit log # View commit history of current branchgit log -n3 # Show last three commitsgit log -p -2 # Show patch differences for each of the last two commitsgit log --stat # Show summary statistics for each commitgit log --pretty=oneline # Display each commit on a single line; options include short, full, fullergit log --pretty=format:"%h - %an, %ar : %s"git log --pretty=format:"%h %s" --graph # Use ASCII art to visualize branching and merge historygit reflog # Show all operations across all branches (including commits, resets, and deleted commits)git reflog # Show last 10 entriesgit grep -n wanzi # Search for 'wanzi' in commit history and working directorygit grep --count wanzi # Count occurrences of 'wanzi'git clean # Remove untracked files not ignored by .gitignoregit clean -d # Clean entire working directorygit clean -d -n #-n tests cleanup without actually removing files
git branch # List current branchesgit branch -v # Show last commit for each branch# Create local dev branch from remote mastergit checkout -b dev origin/master
git branch --merged # List branches merged into current branchgit branch --no-merged # List branches not yet merged into current branch# Start new feature development: create and switch to new branchgit checkout -b dev-20180720-111111-wanzi
Equivalent to:
git branch r-20180720-111111-wanzi
git checkout r-20180720-111111-wanzi
# Merge completed feature into mastergit checkout master
git merge dev-20180720-111111-wanzi
Git Merge Operations
Create a new local branch issue54 from remote master, then develop on it.
git tag # List current tagsgit tag -l 'wanzi-2018*'# List only tags matching patterngit 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 metadatagit show v1.8-20180720 # View tag details; compare with `git tag` outputgit push origin tag_name # Push specific tag to remotegit push origin --tags # Push all tags to remote at oncegit push origin :tag_name # Delete remote taggit checkout -b dev-20180720-111111-wanzi v1.2.0 # Create new branch from a taggit branch -D dev-20180720-123333-wanzi # Force delete an unmerged branch
# Code added to staging area but not committed yet; revert staginggit reset HEAD . or
git reset HEAD a.txt
# This only affects staging area; working directory remains unchanged unless further actions are takengit checkout -- . # Discard all changes in working directory, revert to last commitgit checkout -- a.txt # Discard changes in a.txt, revert to last commit
git log
git reset --hard <commit_id> # Roll back to specific commitgit reset --hard HEAD^ # Roll back to previous commitgit reset --hard HEAD^^ # Roll back to second-to-last commitgit reset --hard HEAD~4 # Roll back to fourth-to-last commitgit 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: