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 |
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 | $ git reset HEAD # reset all added files |
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:sometimes it failed due to late version of project, checkout and pull the latest version.
1
$ ./startindesignenv.sh -c
1
$ git pull # pull the update to local branch
if late-version conflicts occur, reset to last commit version, use1
2$ git rebase -i master
# rebase multi local submits and get sync with new branch on master and local, conflicts may occurgit rebase
to modify commit which is not pushed: link
1 | $ git log # check commit history |
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, usegit 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
- to modify commit message:
1
$ git commit --amend
- to modify changed (already pushed) submit without abandon:
add modified local files, then doFor more info about using amend :1
$ git commit --amend --no-edit
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:
- rebase and list the last N commit:vim will come out and show like:
1
$ git rebase -i HEAD~N
modify the1
2
3
4
5pick 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
...pick
toe
oredit
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
... - 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 | $ git branch <declare a new branch name here> # create |
Switch to certain branch:
1 | $ git checkout <branch name> |
push local commit to remote repository:
1 | # when push current branch to remote |
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 | $ git add . |
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 | MINGW64 ~/Desktop/bdc-test (master) |
1 | MINGW64 ~/Desktop/bdc-test (19q4_cp) $ git pull origin 19q4_cp |
.
save temporarily work:
can skip these steps:
1 | $ git stash # save work with the latest commit title (current modifications) |
.
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 | $ git add . # commit the resolved changes first |