git - Every Day Use Command

Subscribe Send me a message home page tags


Table of Contents

Related Readings

Alias

alias gitdiffcommit='git --no-pager diff HEAD'
alias gitdiffprod='git --no-pager diff origin/master'
alias gitdiffstage='git --no-pager diff'
alias gitfilediffcommit='git diff --name-only HEAD'
alias gitfilediffprod='git diff --name-only origin/master'
alias gitfilediffstage='git diff --name-only'
alias gitglog='git log --graph --decorate --all'
alias gitlistallbranch='git branch -a'
alias gitlistbranch='git branch -vv'
alias gs='git status'

Common Tasks

Rebase

The objective is to transform the commit tree from left to right.

objective.png

1. checkout testing branch

We first need to checkout the testing branch using command git checkout testing.

2. Rebase testing branch on top of mainline branch

We could achieve this using command git rebase mainline testing. This gives us the following structure:

rebase_mainline_testing.png

Note: The instruction provided by git rebase commands is useful but confusing. In case of conflict, we need to:

  1. Resove the conflict.
  2. Use git add to mark the conflicts as resolved.
  3. Continue the rebase using git rebase --continue. Now, there are two possibilities:
    1. We can move to next step. (Git will ask us to resolve new conficts. It's possible that we need to resolve different conflicts for the same files.)
    2. Git complains and asks us to check if we forget to execute git add. Now we need to
      1. Execute git status and see if there are unresolved conflicts. If we do have unresolved conflicts, go back to step 1.
      2. (Optional) Execute git rebase --continue again. (We will probably get the same complain.)
      3. Skip the current round using git rebase --skip

Repeat the process until all conflicts are resolved.

3. Rebase mainline branch on top of testing branch

This can be done by executing git rebase testing mainline command.

The objective of this step is to move the pointer of mainline branch to the last commit of the testing branch. After this step, we have

all points to the last commit in the local environment.

rebase_testing_mainline.png

4. Squash commits if needed

Note that we should be in the mainline branch now. With our example, if we execute git status at this point, git should report we are currently 3 commits ahead of the origin/mainline.

If we want to squash commits in the testing branch, we could use command git rebase -i HEAD~3, which produces the following result:

result.png

5. Check the local commit history before push

Perform sanity checks using git log --graph --decorate --all!

----- END -----

Welcome to join reddit self-learning community.
Send me a message Subscribe to blog updates

Want some fun stuff?

/static/shopping_demo.png