Abstract
Git is commonly used in daily work. In this post, some frequently used commands and few common cases are listed.
Content
The main content would be organized as two parts.
- useful commands
- real usage
Commands
Commands could be separated to several part, like information check related and real action related.
Information Related
Those commands would provide you many information. Don’t worry! they wouldn’t change or hurt your files/contents.
git --version
Check installed git version informationgit status
Check the status of working directory and staging areagit log
:git log --oneline
Show the list of commits in one line formatgit log -s $string
Show commits that make add or remove a certain $stringgit log --grep $message
Show commits with $message in commit messagegit log --author $username
Show commits made by $usernamegit log --all
Show not only code change commits, but also branch merge commit
git diff
Show difference between HEAD and working directorygit remote
Show all attached remote information
Action Related
git clean
Remove untracked files
Conflict Related
We could use configured tool to merge conflicts. You can follow bellow steps.
- Configure vimdiff as default tool(only required for the first time):
git config --global merge.tool vimdiff
Set vimdiff as mergetoolgit config --global mergetool.prompt false
Not prompt question
- Start mergetool by
git mergetool
- Use follow keyboard shortcuts/commands to fix conflicts in vimdiff:
] + c
(shortcut) Jump to next conflict[ + c
(shortcut) Jump to previous conflictdiffget LO
(command) Use conflict local version as merge versiondiffget RE
(command) Use conflict remote version as merge versiondiffget BA
(command) Use conflict base version as merge version
Use cases
Pull remote branch when no local exist
You can make it by following steps.
git fetch origin
git checkout --tack origin/$remote_branch_name
Push local new branch when no remote exist
You can make it by following steps.
git checkout -b $branch_name
git push -u origin $branch_name
Delete branch in both local and remote
You can make it by following steps.
git push --delete origin $branch_name
git branch -d $branch_name
git fetch -p
Amend few change to last commit which already pushed
This case commonly happened when you have Pull Request, and reviewer want you to change a little bit. You can make it by following steps.
git commit --amend --no-edit
orgit commit --amend -m "new commit message"
git push -f
History
2022-05-29: create post based on note of processon
2022-08-20: reorganize content and format, publish