Basic about git
Category
- What is the repository , branch .
- How to delete a branch on the local side, how to delete a remote branch
- How to push a branch locally to the remote under a different name (For example, local name branch is task # 1 , and want to push to branch task # 2 at the remote)
- What is git rebase . Differentiate rebase with merge
- What is git stash
- How to remove the status of a few recent commits
- How to include several commits into a single commit
- Differentiating git reset , git reset -hard , git reset -soft
content
What is the repository, branch?
Repository is simply understood as a place that contains all the information needed to maintain and manage revisions and history of the entire project.
All data of Repository are contained in the folder you are working in hidden folder named .git
Git's Repository is classified into 2 types: remote repository and local repository .
- Remote repository: Is a repository to share between multiple people and arrange on a dedicated server.
- Local repository: Is a repository located on my own machine, for a user to use.
Branch is the branch of the repository:
- A branch is similar to a workspace: used to record revision history.
- Independent branches: developing the features of a branch will not affect other branches.
- Branches can merge together. This operation is called merge .
- The default branch is master .
Types of branches:
- Branch local: is the branch stored locally (of course). It can be linked to a branch on the remote or not.
Displays the branch on the local branch and uses the git branch command - Branch remote: is the branch stored in remote. Branch can be fetched to local but does not create a branch in the local.
Simply put, you can download the branch at remote, but don't create a local branch with the same name and of course won't link it to a local branch at all.
To display the remote branch on local, use the command git branch -r
Delete a branch
Delete a branch on the local side
- Method 1:
git branch -delete <branch_name>
or
git branch -d <branch_name> - Method 2:
git branch –delete –force <branch_name>
or
git branch -D <branch_name>
Delete a remote branch in the local
git branch –delete –remotes <remote_name> / <branch_name>
or
git branch -d -r <remote_name> / <branch_name>
Attention:
- Method 1 only deletes the local branch when it has been Merge into the current branch and it has been pushed to the remote if it is associated with a remote branch.
- Method 2 will remove all branches even if they do not meet the above conditions.
- Only delete the branch when you are in another branch
Delete a branch on the remote side
git push <remote_name> -delete <branch_name>
Example:
git push origin –delete <branch_name>
Push a branch locally to remote
2 branch of the same name
git push <remote_name> <branch_name>
Eg: push branch master to branch master on remote
git push origin master
2 branch other names
git push <remote_name> <local_branch>: <remote_branch>
Eg: push branch master to remote with the name of develop
git push origin master: develop
Rebase
What is Rebase?
Rebase is a way of integrating changes from one branch to another.
In addition, Rebase works to include commits into 1. (shown below)
Compare Rebase with merge
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | | =============== | ================================= ============================================= | | | F --- G --- H develop | | Initial | / | | | A --- B --- C --- D --- E master | | =============== | ================================= ============================================= | | | Rebase | Merge | | =============== | ================================= ======== | ==================================== | | Branch | develop | master | | current | | | | --------------- | --------------------------------- -------- | ------------------------------------ | | Statement | git rebase master | git merge develop | | | or | | | | git rebase master develop | | | --------------- | --------------------------------- -------- | ------------------------------------ | | | F '--- G' --- H 'develop | F --- G --- H develop | | Results diagram | / | / | | A --- B --- C --- D --- E master | A --- B --- C --- D --- E --- I master | | --------------- | --------------------------------- -------- | ------------------------------------ | | Principle | - Save commits F, G, H (| commits - Create a new commit (I) apply | | | develop taking into account the nearest common father) | all changes of the | | | - Reset develop about commit C | Commit on develop counts to | | | - Apply master commits to | The latest common father. | | | The nearest common father C | Specifically here are all | | | - Create new commits F ', G', H 'by | commit F, G, H | | | change of commits F, G, H | | | --------------- | --------------------------------- -------- | ------------------------------------ | | Features | - Commits of rebase-branch will be | - The commit history has not been changed | | latest commits | | | | - Commit history will be changed | | | | | | | | | | | =============== | ================================= ======== | ==================================== | |
What is git stash
Git stash is used when you want to save changes but not yet committed, often very useful when you want to switch to another branch that is working in the current branch.
Save all the work you are doing, use the command: git stash save or git stash
Some commands with stash
- See stash list : git stash list
- Apply the latest stash and delete that stash: git stash pop
- Apply stash: git stash apply stash @ {<index>}
- View stash content: git stash show stash @ {<index>}
- Delete stash: git stash drop stash @ {<index>}
- Delete all stash: git stash clear
Remove the status of a few commits
Method 1: Use git reset hard
git reset –hard <commit_id>
Or
git reset –hard HEAD ~ <index>
Example:
git reset –hard HEAD ~ 2
git reset –hard 2e07fbe
This command will delete all commits before bringing the branch to the state of the commit with the specified Id.
Method 2: use git revert
git revert <commit_id>
or
git revert HEAD ~ <index>
Example:
revert git HEAD ~ 1
git revert 2e07fbe
This command will create a new commit with the content reversing an old commit.
The result after the new commit is created, the branch will remove the old commit change.
New commits than old commits remain.
Simply put, only the specified commit will be deleted.
Combine several commits into a single commit
To include a commit group into a single commit, use the command:
git rebase –interactive <commit_end>
or
git rebase -i <commit_end>
<commit_end> is the id of the last commit in the group to include.
In the text editor, pick | squash | fixup the commits to merge then save the file and exit.
Differentiating git reset, git reset -hard, git reset -soft
git reset
Statement:
git reset HEAD ~ <index>
or
git reset <commit_id>
Function:
- Move HEAD to the commmit reset position.
- Leave all file changes unchanged to the current location.
- Remove changes from the stage.
git reset -hard
Statement:
git reset –hard HEAD ~ <index>
or
git reset –hard <commit_id>
Function:
- Move HEAD to the commmit reset position.
- Remove all changes of the file after the commit reset time.
git reset –soft
Statement:
git reset –soft HEAD ~ <index>
or
git reset –soft <commit_id>
Function:
- Move HEAD to the commmit reset position.
- Leave all file changes unchanged to the current location.
- Keep the changes on stage
This command only moves HEAD to the commit location. The status of the stage and all file changes will be preserved.
Conclude
Above are some things to keep in mind when using GIT. Hope will help everyone.