Git 使用教程

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

  1. use git clone {repo link} to download repo into local file, there are two kinds of links:
    1. ssh link(recommended): git@github.com:NicholasYe/xxxxxxxx.git
    2. https link: https://github.com/NicholasYe/xxxxxxxx.git
  2. enter the file, then normally you will find that you are in branch master
  3. use git checkout -b {Branch_Name} to create a new branch
    1. you can also use git checkout -b {Branch_Name} origin/{Branch_Name} to copy others branch

2. Store and push changes in your new branch

  1. After switching to your new branch, you can write some codes you want
  2. use git add . to temporarily store your changes
  3. use git commit -m "{Comment}" to store your changes with some comments
  4. If you are the first time to push:
    1. use git push --set-upstream origin {Branch_Name} to push your local branch into remote branch origin/{Branch_Name}
    2. If not, just use git push to push your branch into remote branch
  5. Open github.com and you can create your new branch into PR

3. Merge master changes into your branch

  1. use git checkout master to change to your master branch
  2. use git pull to update your master branch
  3. use git checkout {Branch_Name} to change to your branch
  4. use git merge master to merge master branch
  5. use git add . and git commit -m "{Comment}" and git push to push your branch

Useful commands of Git

1. Check your working history

  1. use git log to check your working history
  2. use git log --oneline to check your commit’s and comments in one line
  3. use git log --author={Author_Name} to check commit with specific author
  4. use git log -p to see detail changes of each commit
  5. use git log --stat to see summary of each commit

2. Check your local and remote branch

  1. use git branch to check your local branch
  2. use git branch -r to check remote branch
  3. use git branch -a to check all the branch
  4. 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:
    1. use git revert {Commit_id} to revert changes of one commit
    2. use git revert HEAD~{Number} to revert to previous {number} commit. (I recommend you to use git log before to check where is HEAD now)
  • Reset:
    1. use git reset HEAD~{Number} to reset to previous {number} commit. (I recommend you to use git log before to check where is HEAD now)
    2. use git reset --soft {Commit_id} to reset all to this commit, and all the changes is stored in staged changes, use git commit -m "{comment}"
    3. use git reset --mixed {Commit_id} to reset all to this commit, and all the changes isn’t stored, use git add . and git commit -m "{comment}"
    4. use git reset --hard {Commit_id} to reset all to this commit. (Be careful when you use this)

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.

  1. use git cherry-pick {Commit_id} to cherry-pick specific commit in your current HEAD branch.
  2. use git cherry-pick {Commit_id} --no-commit to cherry-pick specific commit, then you need to use git 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!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值