Note of Git basic knowledge
1. What is repository, branch.
- Repository: contains files, history, branches
- Branch: The branch of the repository, to grow without affecting other branches, the branch is master, created when the new repository is created.
2. How to delete a branch locally, how to delete a branch remotely
- Delete branch locally: git branch -d <branchname>
- Delete the remote branch: git push origin –delete <branchname>
3. How to push a branch locally to remote under a different name (for example, locally named branch task-1, and want to push to branch task-2 at remote)
- git push origin <localbranch>: <remotebranch>
4. What is git rebase. Distinguish rebase from merge
- What a rebase is: takes all the changes committed in one branch and rerun them on another branch.
- Compare rebase and merge:
- Rebase: – Example: git checkout rebase-branch git rebase master – Principle: Get code from master, then apply each commit from rebase-branch to the new code and create a commit with the same name rebase-branch. Rebasing, existing commits are discarded and re-create the same commits but are actually different. This makes local and remote commit history different. Feature: the commits of the newly created branch will lie seamlessly, and the commits of the rebase-branch will be the latest commits.
- Merge: – Example: git checkout merge-branch git merge master – Principle: git uses the last 2 commits of each branch and then integrates together to form a new commit. Perform a merge, the existing commits are not changed, only creating 1 new integrated commit of the last 2 commits. – Feature: commits of 2 branches are arranged according to commit time.
5. What is git stash
- What is git stash: used when you want to save uncommitted changes. Useful in case you want to change to a different branch that is unfinishing the current branche.
- Syntax git stash save
6. How to remove the status of a few recent commits
- Remove status of a commit: git revert <commit>
7. How to merge several commits into a single commit
- Include the commit using: git rebase –i <commit_name_before_start> or git rebase –i HEAD ~ <commit_number>
8. Distinguish git reset, git reset –hard, git reset –soft
Distinguish git reset, git reset –hard, git reset –soft:
- git reset <commit>: resets the file status of the commit, the file remains unchanged, the pointer returns to the commit location
- git reset –hard <commit>: reset the file status of the commit, the file changed at the time of the commit, the pointer to the commit location
- git reset –soft <commit>: no reset file state of commit, file unchanged, pointer to commit position