Git in Daily Usage

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.

  1. useful commands
  2. real usage

Commands

Commands could be separated to several part, like information check related and real action 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 information
  • git status Check the status of working directory and staging area
  • git log:
    • git log --oneline Show the list of commits in one line format
    • git log -s $string Show commits that make add or remove a certain $string
    • git log --grep $message Show commits with $message in commit message
    • git log --author $username Show commits made by $username
    • git log --all Show not only code change commits, but also branch merge commit
  • git diff Show difference between HEAD and working directory
  • git remote Show all attached remote information
  1. git clean Remove untracked files

We could use configured tool to merge conflicts. You can follow bellow steps.

  1. Configure vimdiff as default tool(only required for the first time):
    1. git config --global merge.tool vimdiff Set vimdiff as mergetool
    2. git config --global mergetool.prompt false Not prompt question
  2. Start mergetool by git mergetool
  3. Use follow keyboard shortcuts/commands to fix conflicts in vimdiff:
    • ] + c(shortcut) Jump to next conflict
    • [ + c(shortcut) Jump to previous conflict
    • diffget LO(command) Use conflict local version as merge version
    • diffget RE(command) Use conflict remote version as merge version
    • diffget 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.

  1. git fetch origin
  2. git checkout --tack origin/$remote_branch_name

Push local new branch when no remote exist

You can make it by following steps.

  1. git checkout -b $branch_name
  2. git push -u origin $branch_name

Delete branch in both local and remote

You can make it by following steps.

  1. git push --delete origin $branch_name
  2. git branch -d $branch_name
  3. 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.

  1. git commit --amend --no-edit or git commit --amend -m "new commit message"
  2. git push -f

History

  • 2022-05-29: create post based on note of processon

  • 2022-08-20: reorganize content and format, publish