Git Common Commands

After the demise of the self-centralized SVN, distributed Git has become an important front-end code version management tool. Skilled use and understanding of its common commands can improve our development efficiency. Here is an enumeration of some of the git commands I often use.

1. Locally clone codes from the remote repository

git clone repo_url
Copy the code

2. View all local branches that contain a commit

git branch --contains commitId
Copy the code

3. View the submission record

git log 
Copy the code

4. View submission records – One-line display

git log --pretty=oneline
Copy the code

5. View the modification records of a file

git blame filename
Copy the code

7. Save all changes to the local file and add the changes to the staging area

git add .
Copy the code

8. Commit changes to the current branch

git commit -m "description"
Copy the code

9. Modify the remarks of the latest commit

git commit --amend
Copy the code

10. Commit the changes to the remote repository

git push
Copy the code

11. Delete the remote branch

git push origin --delete branchName
Copy the code

12. Change the current directory to a Git managable repository and initialize the Git repository

git init
Copy the code

13. Look at hidden.git files

ls -ah
Copy the code

14. Roll back to the previous version. Replace HEAD^ with commit_id of a certain version to roll back to the corresponding version

git reset --hard HEAD^
Copy the code

15. Cancel the add

git reset HEAD^
Copy the code

16. Undo the commit and keep the changes

git reset commitId
Copy the code

17. Undo the commit and do not retain the changes

git reset --hard commitId
Copy the code

18. Undo a COMMIT (adds one more commit record than reset)

git revert commitId
Copy the code

19. View the current status

git status
Copy the code

20. Display status in a short format

git status -s
Copy the code

21. Switch to a branch

git checkout branchName
Copy the code

22. Undo all changes to the file in the workspace

git checkout --readme.txt
Copy the code

23. Delete a file

git rm a.txt
Copy the code

24. Migrate remote code base (commit to new Git address after change)

Git remote set-url <new git repository url> git remote -vCopy the code

25. View the current branch to display all local branches

gti branch 
Copy the code

26. View all branches, both local and remote

git branch -a
Copy the code

27. View all remote branches

git branch -r
Copy the code

28. Pull the specified remote branch and create a branch

git checkout -b dev origin/dev 
Copy the code

29. Restore the files deleted in the local workspace

git chekcout test.js
Copy the code

30. Name indicates the branch name, and the changes are submitted to the corresponding remote branch

git push origin name
Copy the code

31. View the remote submission address

git remote -v 
Copy the code

32. Delete the remote submission address

git remote rm originName
Copy the code

33. Add the remote submission address

git remote add origin url
Copy the code

34. Cache code

After adding the code to commit, commit the unadded changes to a cache. At this time, you can pull the remote code, and then push the local changes

git stash 
Copy the code

35. View the list in the Stash cache

git stash list
Copy the code

36. Restore the latest cache to the local directory

git stash pop
Copy the code

37. Clear records in the cache

git stash clear
Copy the code

38. Temporarily store Untracked Files

git stash -u 
Copy the code

39. Stash Fill in the description note

Git Stash Save "Notes"Copy the code

40. Stash the latest stash

git stash drop
Copy the code

41. Select commit from target branch to current branch

git cherry-pick commitId
Copy the code

42. Add a repository source

git remote add originName originUrl
Copy the code

43. Delete a repository source

git remote remove originName
Copy the code

44. Send the local branchA branch to the branchA branch with the remote originName

git push originName branchA:branchA
Copy the code

45. Check the git global configuration list. Change global to local to check the configuration list of the current project

git config --global --list
Copy the code

46. View the user name used when the current project is submitted, and follow the name you want to configure to complete the configuration

git config user.name
Copy the code

47. Check the email address used when submitting the current project, and follow up with the email address you want to configure to complete the configuration

git config user.email
Copy the code

Merge the specified branch into the current branch

git merge branchName
Copy the code

49. Pull all branch changes of remote repository back to local without merging (used for creating local remote existing but no local branch)

git fetch
Copy the code

50. View all tags and create a new tag after the tag name

git tag
Copy the code

51. Merge multiple Commit entries after commitId into one commit

git rebase -i commitId
Copy the code

52. Change the local branch name

git branch -m oldName newName
Copy the code

53. Look at the differences between the two branches

Git diff branchA branchB --stat git log branchA ^branchB --statCopy the code

54. View all branches that contain a commit

git branch --contains commitId
Copy the code

The current mainstream editor VScode supports most git shortcuts. It depends on your preference to use the command line or interface button, but it is important to understand Git temporary areas, branches, and diff principles (the minimum edit distance of a string).