Git常用命令笔记

Scenario 1: Committing Files

Create a repository : git init
View git status : git status
Add files to the staging area : git add hello-world.js
Commit the staged file : git commit -m "Changes about hello-world.js"
Add and commit a .gitignore file :
echo '*.tmp' > .gitignore
git add .gitignore
git commit -m "Commit gitignore file"

Scenario 2: Committing Changes

View git status : git status
Compare the changes to a single file : git diff committed.js
Add files to the staging area : git add committed.js
Compare the changes in the staging area : git diff --staged
View the history of the repository and the commit log : git log
Output each commit on a single line : git log --pretty=format:"%h %an %ar - %s"
View the changes made in the commit : git show

Scenario 3: Working Remotely

Add a remote repository : git remote add origin /s/remote-project/1
Push changes to the remote repository : git push origin master
Sync changes from the remote repository : git pull origin master
Find all the commits containing #1234 : git log --grep="#1234"
Download the changes : git fetch
Checkout the branch : git checkout remotes/origin/master

Scenario 4: Undoing Changes

Clear all changes : git checkout .
Remove all the changes from the staging area : git reset .
Remove the changes from both the staging area and working directory : git reset --hard
Revert the changes in the last commit : git revert HEAD
Revert the commits between HEAD and HEAD~2 : git revert HEAD...HEAD~2

Scenario 5: Fixing Merge Conflicts

Merge the changes from origin/master : git merge remotes/origin/master
Select the remote changes : git checkout --theirs staging.txt
Commit the merge :
git add staging.txt
git commit --no-edit
Get the brand-new version : git pull --no-edit origin master
View the past commits : git log --all --decorate --oneline
Rebase current version : git rebase feature/10
Get the current version after rebasing : git pull --rebase

Scenario 6: Experiments Using Branches

Create and checkout a new branch :
git branch new_branch master
git checkout new_branch
List all the branches with their last commit message : git branch -va
Switch to the branch and merge to master :
git checkout master
git merge new_branch
Push the branch to the remote : git push origin new_branch
Delete the branch : git branch -d new_branch

Scenario 7: Finding Bugs

Compare what’s changed between commits : git diff HEAD~2 HEAD
See the overview of the commits : git log --oneline
Output the commit information : git log -p
Output the first two commits information : git log -p -n 2
Output the commits information since 2 weeks ago:git log -p --since="2 weeks ago"
Output the commits which include the word “Initial” in their commit message : git log --grep="Initial"
Enter into the bisect mode : git bisect start
Define your current checkout as bad : git bisect bad
Define when the last known good commit : git bisect good HEAD~5
Check one of those contents : cat list.html
Tag it “good” when there is nothing wrong : git bisect good
Check the next version : cat list.html
Tag it “bad” when there has mistake and we will get the related commit ID: git bisect bad
See who last touched each line : git blame list.html
Provide a range of lines to output : git blame -L 6,8 list.html

Scenario 8: Being Picky With Git

Cherrypick the commit : git cherry-pick new_branch~1
Resolve the conflict :
git status
git diff
git checkout --theirs list2.html
git add list2.html
Continue with the cherry pick (Similar to using merge) : git cherry-pick --continue

Scenario 9: Re-writing History

Enter Interactive Rebase mode : git rebase --interactive --root
See the updated commit message : git log --oneline
Modify the previous 8 commits : git rebase --interactive HEAD~8
See the updated commit message : git log --oneline
Modify the first 2 commits : git rebase --interactive HEAD~2
See the updated commit message : git log --oneline
Modify the first commits : git rebase --interactive HEAD~1
Remove current commit : git reset HEAD^
Separate to two different commits :
git add file3.txt
git commit -m "File 3"
git add file4.txt
git commit -m "File 4"
Rebase and update the repository : git rebase --continue
See the updated commit message : git log --oneline

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值