1. Workflow of Git#
- Workspace: Working directory
- Index / Stage: Staging area
- Repository: Local repository
- Remote: Remote repository
2. Initial configuration of email and username#
git config --global user.name "XXX"
git config --global user.email "XXXX-XXX"
If two-factor authentication is enabled, the password should be set as the token value
git config --global user.password "token value"
3. Getting started#
3.1. Creating a repository & initializing a management repository#
git init
After executing the command, a .git folder will appear in the directory
3.2. Basic use of git#
git add readme.txt - Add a file to the staging area
git commit -m "Note" - Commit the file to the local repository
git status - View the commit status
git log - View the commit history
git log --pretty=oneline - View a concise commit history
git reflog - Get the version number (first 6 letters of the commit hash)
3.2.1. Rollback operation#
git reset --hard HEAD^ - Roll back to the previous version
git reset --hard HEAD^^ - Roll back to the version before the previous version
git reset --hard HEAD~n - Roll back to the specified n versions
git reset --hard version number - Roll back to the specified version number
3.2.2. Undoing modifications#
Same effect as rollback operation
git checkout -- readme.txt - Undo the modifications in the working directory of the readme file (equivalent to returning to the staging area state)
git restore readme.txt - This is the current operation with the same effect as above
3.2.3. Recovery of deleted files in the working directory that have not been committed#
git checkout -- a.txt
git checkout a.txt
git restore a.txt
The above operations can restore a.txt after executing git add a.txt, but not git commit, and then executing rm a.txt
3.2.4. Remote connection operation#
Create a new repository on GitHub
git remote add origin remote repository url - Connect to the remote repository
git branch -M main - Rename the current branch to main (-M indicates forced change)
git push -u origin main - Synchronize the main branch to the remote repository (since the repository is empty for the first time, the -u parameter is added, and it is not necessary to add it later)
3.2.5. Branch operations#
git branch - View branches
git branch name - Create a branch
git checkout name - Switch to a branch
git checkout -b name - Create + switch to a branch
git merge name - Merge the name branch into the current branch
git merge --no-ff -m "Note" dev branch - Cancel the Fast forward merge strategy
git branch -d name - Delete a branch
git branch -M(-m) oldname newname - Change the branch name
3.2.6. Bug branch#
git stash - Hide the working directory
git stash list - View hidden working directories
git stash apply - Restore the working directory
git stash drop - Delete the working directory
git stash pop - Restore and delete the working directory
3.2.7. Collaboration with multiple people#
git remote - View remote repository information
git remote -v - View detailed information of remote repositories
git push origin dev - Push the dev branch to the remote repository
git checkout -b dev origin/dev - Create a local dev branch from the remote origin
3.3. Explanation of some options#
-d --delete: Delete
-D: Shortcut for --delete --force
-f --force: Force
-m --move: Move or rename
-M: Shortcut for --move --force
-r --remote: Remote
-a --all: All