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 --versionCheck installed git version informationgit statusCheck the status of working directory and staging areagit log:git log --onelineShow the list of commits in one line formatgit log -s $stringShow commits that make add or remove a certain $stringgit log --grep $messageShow commits with $message in commit messagegit log --author $usernameShow commits made by $usernamegit log --allShow not only code change commits, but also branch merge commit
git diffShow difference between HEAD and working directorygit remoteShow all attached remote information
Action Related
git cleanRemove 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 vimdiffSet vimdiff as mergetoolgit config --global mergetool.prompt falseNot 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 origingit 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_namegit push -u origin $branch_name
Delete branch in both local and remote
You can make it by following steps.
git push --delete origin $branch_namegit branch -d $branch_namegit 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-editorgit 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