Git cmd for daily use

Codes commit procedure for daily work:

1
$ git status          # check current modifications

see detailed changes from current code:

1
$ git diff

Add work:

1
$ git add .           # do not miss "."

or

1
$ git add -u

or

1
$ git add -A

add -u 和 add . 的区别

Check status and see that modifications displayed in green.

1
$ git commit -m "[Task/Bug] Jira-id : Jira Task content."

if there’s any change not need is added, withdraw the addition with:

1
2
$ git reset HEAD              # reset all added files
$ git reset HEAD <file-name> # reset certain file

Notice:

  • better to build locally first and see if it will get failed. (Close local server before building, no need to close db server on docker)
  • how to build:
    1
    $ ./startindesignenv.sh -c
    sometimes it failed due to late version of project, checkout and pull the latest version.
    1
    $ git pull          # pull the update to local branch
    1
    2
    $ git rebase -i master  
    # rebase multi local submits and get sync with new branch on master and local, conflicts may occur
    if late-version conflicts occur, reset to last commit version, use git rebase to modify commit which is not pushed: link
1
2
3
4
5
6
$ git log                   # check commit history
$ git log <file-path> # check commit history for specific directory or file
$ git log --stat # list detailed changes for every commit
# for detailed search by commit message:
$ git log --all --grep="<msg-to-search>"
$ git reset <commit id> # reset to certain commit version

If build failed, you should redo “add” and “commit” step after resetting.

Tip: git log only show commit history of default remote branch (usually master). If remote has multiple branches in work, use git log --all to check full historical submits.

Final Step:

1
$ git push origin HEAD:refs/for/master

Remember check Gerrit and add reviewers when work pushed. After the work is successfully built on cloud and get merged, add resolved mark and commit on JIRA task system.


.

amend & reset:

if something wrong with codes was found in code review and have to add modifications, you should try to use amend to change the Last Commit

  1. to modify commit message:
    1
    $ git commit --amend
  2. to modify changed (already pushed) submit without abandon:
    add modified local files, then do
    1
    $ git commit --amend --no-edit
    For more info about using amend :
    Rewriting history
    git commit –amend用法(摘抄)

If unfortunately failed to amend the commit, go back to last committed submit:

1
$ git reset --hard <commit-id>

remember to pull down the latest version in this step, then resubmit the new changes.

If we want to amend the merged commit:

  1. rebase and list the last N commit:
    1
    $ git rebase -i HEAD~N
    vim will come out and show like:
    1
    2
    3
    4
    5
    pick da7ba9213 Update .. test
    pick 3ed3643f3 Automatically updating VERSION_PREFIX to 1.3888.0
    pick 6a440cb82 Add ... test
    pick ffdb5f6b2 Automatically updating VERSION_PREFIX to 1.3889.0
    ...
    modify the pick to e or edit before target commit and save:
    1
    2
    3
    4
    5
    ...
    pick 12a0e887f Automatically updating VERSION_PREFIX to 1.3897.0
    e 4e60477ed Adjust .. tools
    pick 38290e07d Automatically updating VERSION_PREFIX to 1.3898.0
    ...
  2. make the changes
    after changes satisfied, amend and finish the rebase:
    1
    2
    3
    $ git commit --amend
    # chenges done
    $ git rebase --continue

.

branch & merge:

Create a personal branch (excluded from master) locally:

1
2
3
$ git branch <declare a new branch name here>   # create
# e.g. git branch test-branch
$ git branch # view all existing branches

Switch to certain branch:

1
$ git checkout <branch name>

push local commit to remote repository:

1
2
3
4
# when push current branch to remote
$ git push origin test-branch
# when push another branch to remote repository
$ git push origin <local_branch_name>:<remote_branch_name>

pull master branch to current branch:

1
$ git merge master

.

git add not work

Today I encountered with a problem that git add . did not work.
The reason might be that someone else is pushing his work to main repository throght branch master while I was also trying to add my work on same branch then.
The solution is simple, use:

1
$ git add -A  # equal to --all

Cuz the work I was working on is changed to untracked status for forking reason.


.

git crashed

起因:deploy自己的react练手项目到Heroku上时,因为react-script 3.3.0在webpack上不靠谱的脚本遇到websocket错误,在本地更改node_module里的脚本完,手贱把gitignore里的/node_module去掉后git add -A试图上传github, 放弃并cancel以后再打开git bash,把react-script downgrade到3.2.0再次提交时发现:

1
2
3
4
5
6
7
8
$ git add .
fatal: Unable to create '.../.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

Solution:
Find the .../.git/index.lock file and delete it. Then reopen git bash.


.

re-login after changing password

1
$  git config --global credential.helper wincred  # for Windows, different from mac OS

Then just use git cmd like git pull, credential confirm dialog will popup, enter the updated password.


.

check remote branches:

1
$ git branch -r

Push to remote branch which is not master

1
$ git push origin HEAD:refs/for/<branch-name>

.

when there exist different branches, after download codes, first switch to target branch then pull, like:

1
2
3
4
MINGW64 ~/Desktop/bdc-test (master)
$ git checkout 19q4_cp
Switched to a new branch '19q4_cp'
Branch '19q4_cp' set up to track remote branch '19q4_cp' from 'origin
1
2
3
4
MINGW64 ~/Desktop/bdc-test (19q4_cp) $ git pull origin 19q4_cp
From https://gerrit.ericsson.se/a/EMBMS/bdc-test
* branch 19q4_cp -> FETCH_HEAD
Already up to date.

.

save temporarily work:
can skip these steps:

1
2
3
4
$ git stash           # save work with the latest commit title (current modifications)
$ git stash save "note" # save work with title "note"
$ git stash list # check to see saved work list
$ git stash apply 0 # apply work-id to restore the work to tmp branch

git stash 用法总结和注意点


.

Git search with format:

1
$ git log --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' --date=short

with defined author, add --author="auth_name"
for given tag, add --tags="given_tag"


.

When rebase is interrupted by accident, the console branch will be like MINGW64 ~/Desktop/api (main|REBASE 2/1)
This means you are stuck with the rebase procedure.

1
2
3
$ git add .                 # commit the resolved changes first
$ git rebase --continue # continue the unfinished rebase
$ git rebase --abort # exit the rebase procedure