A List of Common Git Commands
In the article How to Use GitHub without Writing a Single Line of Code where we introduced how to use GitHub Desktop to operate GitHub, the graphical interface is very friendly for friends who are not familiar with commands. However, sometimes it is more convenient to directly run Git commands in a code editor or terminal, so this article will introduce some commonly used commands to help you level up your Git skills.
1 Configuring Git
First, you need to let Git know who you are, so set your username:
git config –-global user.name
Set your email:
git config --global user.email
2 Setting up Git repository
Create a new blank repository:
git init
Clone a repository to the current folder:
git clone <repo URL>
Display the list of remote repositories:
git remote -v
Delete a remote repository:
git remote rm <remote repository name>
Fetch the latest changes from the server, but do not merge them into the current working document:
git fetch
Fetch and merge the latest changes from the server into the current working document:
git pull
3 Managing changes to files
Add all changes to the cache:
git add <file name>
Remove a file and do not keep its modification history:
git rm <file name>
Recover a deleted file and prepare it for update submission:
git checkout <deleted file name>
Display the status of modified files:
git status
Some files are ignored by version control because they are unnecessary for the project, such as some cache and log files. Use this command to view the list of ignored files:
git ls-files –other –ignored –exclude-standard
Display the changes of all files in the current directory:
git diff
4 Git commands related to submitting changes
Submit the changes and comment the content:
git commit -m "<commit content>"
Switch to the state of a certain submission:
git checkout <commit>
Undo all changes made in a certain submission:
git reset –hard <commit>
Undo changes in the current working folder:
git reset –hard Head
Display modification history:
git log
Stash current changes and use later:
git stash
Retrieve previously stashed files:
git stash pop
Clear stashed files:
git stash drop
Create a tag to mark the current file version:
git tag <tag version>
Push changes to origin:
git push
Push changes to another branch:
git push <current branch>:<branch to push to>
5 Git branch operations
Display all branches:
git branch
Create a new branch and switch to the new branch:
git checkout -b <branch name>
Switch to a new branch:
git checkout <branch name>
Delete a branch:
git branch -d <branch name>
Merge another branch into the current branch:
git merge <branch name>
Fetch a branch from a remote repository:
git fetch remote <branch name>
Check the differences between two branches:
git diff <source branch> <destination branch>
6 Git Tips
Are the myriad of commands making you dizzy? Try it out in your code editor and terminal! Finally, summarize some tips for using Git in your daily life to help you reduce errors and improve efficiency:
- Before you start work, check the current status of origin first. Your teammates may have updated the files, make sure you work on the latest files.
- Make sure to complete sufficient testing before submitting updates to ensure your updates are valid.
- Submit updates in a timely manner to allow your teammates to synchronize their progress.
- Clearly describe the comments in the changes and submissions, describe the difficult parts in detail.
- Make full use of branching to make changes. It is not recommended to make changes directly on the master branch to avoid unexpected errors.