It's my turn to do a technical share this week. After much thought, I decided to choose Git as the theme for this week's technical share. There are two reasons for this. First, I encountered some difficulties when using Git recently, which will be mentioned in detail later. Second, although I use Git every day, there are still some vague areas in terms of basic concepts, and I also want to further understand Git through this share.
Git Principles#
workspace: Workspace (current development location)
git pull
: Fetches the latest code from the remote repository to the workspace =>git fetch
+git merge
git diff
: Shows the modified but unstaged files
index: Staging area
git add
: Submits the modified content in the workspace to the staging area
repository: Local repository
git commit
: Submits the content in the staging area to the local repositorygit fetch
orgit clone
: Fetches/clones code from the remote repository to the local repository
remote repository: Remote repository
git push
: Submits the content in the local repository to the remote repository
Common Questions#
- What does
git push origin master
do?
It pushes the content on the local master
branch to the origin
location and creates a master
branch with the same name at the origin
location.
Full command: git push origin master:master
=> git push <remote hostname> <local branch name>:<remote branch name>
- What is the difference between
git merge
andgit rebase
?
Example: Merge the test
branch into the master
branch
Result of git merge
Result of git rebase
Recent Issues#
- The local project encountered an error and I want to revert to a previous version => Revert the local repository to a previous version
git reset --hard/soft [commit point to revert to]
- Undo a commit that has already been pushed to the repository => Revert the remote repository to a previous version
git reset --hard [commit point to revert to]
git push origin HEAD --force