编程知识 cdmana.com

Git common commands

Git Common commands

Git Is an open source distributed version control system , Agile for efficient handling of any small or large project ,Git What I am good at is the version management of program code , The source code of the program for differential version management , The code base takes up very little space , so to speak Git It is the most advanced distributed version control system in the world .

Working position

  • Workspace: work area .
  • Index/Stage: Temporary storage area .
  • Repository: Local repository .
  • Remote: Remote warehouse .

To configure

  • git config --list: Show current Git To configure .
  • git config -e [--global]: edit Git The configuration file .
  • git config [--global] user.name "[name]": With name Configure a single property for example .

establish

  • git clone ssh://user@domain.com:22/resources.git: Clone remote warehouse .
  • git init: Initialize local git Warehouse , New warehouse , Usually use --bare Option to initialize the remote repository .

Add or delete files

  • git add [file1] [file2] ...: Add the specified file to the staging area .
  • git add [dir]: Add the specified directory to the staging area , Include subdirectories .
  • git add .: Add all files from the current directory to the staging area .
  • git add -p: Add each change before , Will ask for confirmation , For multiple changes in the same file , It can be submitted in batches .
  • git rm [file1] [file2] ...: Delete workspace file , And put the deletion in the staging area .
  • git rm --cached [file]: Delete files from the staging area , The workspace doesn't change .
  • git mv [file-original] [file-renamed]: Rename file , And put it in the temporary storage area .

Submit code

  • git commit -m [message]: Submit staging area to warehouse area .
  • git commit [file1] [file2] ... -m [message]: Submit the specified files of staging area to the warehouse area .
  • git commit -a: Submit workspace since last commit The change after , Go straight to the warehouse area .
  • git commit -v: Show all... On submission diff Information .
  • git commit --amend -m [message]: Use a new one commit, Instead of the last submission , If there are no new changes in the code , To rewrite the last time commit The submission of .
  • git commit --amend [file1] [file2] ...: Redo the last time commit, And include new changes to specified documents .

Branch

  • git branch: List all local branches .
  • git branch -r: List all remote branches .
  • git branch -a: List all local and remote branches .
  • git branch [branch-name]: Create a new branch , But still in the current branch .
  • git checkout -b [branch]: Create a new branch , And switch to the branch .
  • git branch [branch] [commit]: Create a new branch , Point to designated commit.
  • git branch --track [branch] [remote-branch]: Create a new branch , Establish a trace relationship with the specified remote branch .
  • git checkout [branch-name]: Switch to the specified branch , And update the workspace .
  • git checkout -: Switch to the previous branch .
  • git branch --set-upstream [branch] [remote-branch]: Establish a tracking relationship , Between the existing branch and the specified remote branch .
  • git merge [branch]: Merge the specified branch to the current branch .
  • git cherry-pick [commit]: Choose one commit, Merge into the current branch .
  • git branch -d [branch-name]: Delete the branch .
  • git push origin --delete [branch-name]: Delete remote branch .

label

  • git tag: List all tag.
  • git tag [tag]: Create a new one tag At present commit.
  • git tag [tag] [commit]: Create a new one tag In the specified commit.
  • git tag -d [tag]: Delete local tag.
  • git push origin :refs/tags/[tagName]: Delete remote tag.
  • git show [tag]: see tag Information .
  • git push [remote] [tag]: Submit designation tag.
  • git push [remote] --tags: Submit all tag.
  • git checkout -b [branch] [tag]: Create a new branch , Point to something tag.

Check the information

  • git status: Show changed documents .
  • git log: Displays the version history of the current branch .
  • git log --stat: Show commit history , And every time commit Changed documents .
  • git log -S [keyword]: Search submission history , According to the key .
  • git log [tag] HEAD --pretty=format:%s: Show a certain commit All the changes since , Every commit Occupy a line .
  • git log [tag] HEAD --grep feature: Show a certain commit All the changes since , The submission instructions must meet the search criteria .
  • git log --follow [file]: Show version history of a file , Include file rename .
  • git log -p [file]: Displays each time the specified file is related to diff.
  • git log -5 --pretty --oneline: Show past 5 Submission .
  • git shortlog -sn: Show all submitted users , Sort by number of submissions .
  • git blame [file]: Shows who modified the specified file at what time .
  • git diff: Show differences between staging area and workspace .
  • git diff --cached [file]: Show staging area and previous commit The difference of .
  • git diff HEAD: Show workspace and current branch up to date commit Differences between .
  • git diff [first-branch]...[second-branch]: Show the difference between two submissions .
  • git diff --shortstat "@{0 day ago}": Show how many lines of code you wrote today .
  • git show [commit]: Show metadata and content changes for a submission .
  • git show --name-only [commit]: Show files that have changed in a certain submission .
  • git show [commit]:[filename]: Show when a submission , The contents of a document .
  • git reflog: Shows the most recent commits for the current branch .

revoke

  • git checkout [file]: Recover the specified files from the staging area to the workspace .
  • git checkout [commit] [file]: To restore something commit To the staging area and workspace .
  • git checkout .: Recover all files from staging area to workspace .
  • git reset [file]: Reset the specified file in the staging area , With the last time commit bring into correspondence with , But the workspace doesn't change .
  • git reset --hard: Reset staging area and workspace , With the last time commit bring into correspondence with .
  • git reset [commit]: Resets the pointer of the current branch to the specified commit, Also reset staging area , But the workspace doesn't change .
  • git reset --hard [commit]: Resets the... Of the current branch HEAD For a given commit, Reset staging area and workspace at the same time , And specify commit Agreement .
  • git reset --keep [commit]: Reset current HEAD For a given commit, But keep staging area and workspace unchanged
  • git revert [commit]: Create a new one commit, Used to revoke a designation commit, All changes in the latter will be offset by the former , And apply to the current branch .
  • git stashgit stash pop: Temporarily remove uncommitted changes , Move in later .

Remote synchronization

  • git fetch [remote]: Download all changes to remote warehouse .
  • git remote -v: Show all remote warehouses .
  • git remote show [remote]: Show information about a remote warehouse .
  • git remote add [shortname] [url]: Add a new remote warehouse , And name .
  • git pull [remote] [branch]: Change to retrieve remote warehouse , And merge with local branches .
  • git push [remote] [branch]: Upload local designated branch to remote warehouse .
  • git push [remote] --force: Force current branch to remote warehouse , Even if there are conflicts .
  • git push [remote] --all: Push all branches to remote warehouse .

A daily topic

https://github.com/WindrunnerMax/EveryDay

Reference resources

https://blog.csdn.net/mine_song/article/details/70770467
https://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
https://www.liaoxuefeng.com/wiki/896043488029600/896067008724000

版权声明
本文为[WindrunnerMax]所创,转载请带上原文链接,感谢

Scroll to Top