This blog is about the workflow of Git version control
You are welcomed to chat about it and if you like this blog, do not forget to give me a like.
Welcome to see my homepage and contact me
: NicholasYe’s Homepage.
Normal workflow
1. Git clone and create a new branch
- use
git clone {repo link}
to download repo into local file, there are two kinds of links:- ssh link(recommended):
git@github.com:NicholasYe/xxxxxxxx.git
- https link:
https://github.com/NicholasYe/xxxxxxxx.git
- ssh link(recommended):
- enter the file, then normally you will find that you are in branch
master
- use
git checkout -b {Branch_Name}
to create a new branch- you can also use
git checkout -b {Branch_Name} origin/{Branch_Name}
to copy others branch
- you can also use
2. Store and push changes in your new branch
- After switching to your new branch, you can write some codes you want
- use
git add .
to temporarily store your changes - use
git commit -m "{Comment}"
to store your changes with some comments - If you are the first time to push:
- use
git push --set-upstream origin {Branch_Name}
to push your local branch into remote branchorigin/{Branch_Name}
- If not, just use
git push
to push your branch into remote branch
- use
- Open
github.com
and you can create your new branch into PR
3. Merge master changes into your branch
- use
git checkout master
to change to your master branch - use
git pull
to update your master branch - use
git checkout {Branch_Name}
to change to your branch - use
git merge master
to merge master branch - use
git add .
andgit commit -m "{Comment}"
andgit push
to push your branch
Useful commands of Git
1. Check your working history
- use
git log
to check your working history - use
git log --oneline
to check your commit’s and comments in one line - use
git log --author={Author_Name}
to check commit with specific author - use
git log -p
to see detail changes of each commit - use
git log --stat
to see summary of each commit
2. Check your local and remote branch
- use
git branch
to check your local branch - use
git branch -r
to check remote branch - use
git branch -a
to check all the branch - use
git branch -D {Branch_Name}
to delete one branch
3. Revert or Reset your commit
Notice: REVERT and RESET is totally different thing, be careful!
revert:
git revert
will only undo only changes associated with a specific commit.
reset:git reset
will undo every changes since a given commit occurred.
- Revert:
- use
git revert {Commit_id}
to revert changes of one commit - use
git revert HEAD~{Number}
to revert to previous {number} commit. (I recommend you to usegit log
before to check where is HEAD now)
- use
- Reset:
- use
git reset HEAD~{Number}
to reset to previous {number} commit. (I recommend you to usegit log
before to check where is HEAD now) - use
git reset --soft {Commit_id}
to reset all to this commit, and all the changes is stored in staged changes, usegit commit -m "{comment}"
- use
git reset --mixed {Commit_id}
to reset all to this commit, and all the changes isn’t stored, usegit add .
andgit commit -m "{comment}"
- use
git reset --hard {Commit_id}
to reset all to this commit. (Be careful when you use this)
- use
4. Cherry-pick forward commit
Notice: With the
cherry-pick
command, Git allows you to integrate selected, individual commits from any branch into your current HEAD branch.
- use
git cherry-pick {Commit_id}
to cherry-pick specific commit in your current HEAD branch. - use
git cherry-pick {Commit_id} --no-commit
to cherry-pick specific commit, then you need to usegit commit -m {Comment}
to commit
Please clearly mark the source of the article in the process of reproducing it! Respect for originality and intellectual property rights, thanks!