文章目录
Git learning notebook
This is a simple Git learing notebook followed by Git learning, professional manual can be found in Pro Git book.
Thanks to teacher Liao Xuefeng.
Common command
git init //
git config --global user.name "name"
git config --global user.email "name@.com"
git status
git diff
git add file_name
git add . // add all file in folder
git commit -m "message"
git reset --hard HEAD^ // latest version
git log // history
git reset --hard 1094a
git reflog // back to future
Understand Working Directory & Repository
-
git add
: Commit the current modification to Stage -
git commit
: transfer modification from Stage to Repo
two e.g.- modify →
git add
→ modify →git commit
: only commit the first modification to Repo - modify →
git add
→ modify →git add
→git commit
: both modification have been committed to Repo.
- modify →
-
git diff HEAD -- [file_name]
: check the difference between Working Directory & Repository.
Cancel modification
there are 4 situation:
- modified but not to submit to Stage →
git restore file_name
- modified and submitted to Stage (
git add
) →git restore --staged <file>
both operation can rungit status
to get. - submitted to Repo (
git commit
) →git reset --hard HEAD_ID
git push
: remote server have a log
delete file
- can be seen modification
- delete(Y):
git add + git commit
- delete(N):
git restore file_name
- delete(Y):
Branch management
Create and switch to a new branch
- create a new branch ‘dev’ and switch to it :
it also can be realized via :git branch dev // create a new branch named 'dev' git checkout/switch dev // switch to branch 'dev' git branch // observe branch status
git checkout -b dev // create & switch to "dev" git switch -c dev // create & switch to "dev"
Merge and delete branch
-
After master’s work finished, we can merge branch dev into branch master via:
git checkout/switch master git branch git merge dev // Fast forward mode which can't observate branch info git merge --no-ff -m "message" dev
-
To delete a local branch:
git checkout -d dev git branch -d dev // delete branch 'dev' which has **merged**. git branch -D dev // delete branch 'dev' which has **NOT merged**.
-
To delete a remote branch:
git push origin --delete [name]
Solve commit conflict
- if branch and master have conflict modification, we should modify it manually.
Branch management policy
Two mode to merge branch:
-
Fast forward which will discard branch merge info.
git merge dev
-
–no-ff
git merge --no-ff -m "message" dev
-
observe branch merge information
git log --graph --pretty=oneline --abbrev-commit
Bug branch management
Usually, you are working in dev branch, when you realize that other branches have bugs but the current task is not completed, you should save save current work to fix bugs.
-
Save current work:
git stash
-
Switch to bug branch to create temp branch to fix bug:
Do not work in master, especially fixing bugs.git switch master git switch -c bug001 // bug fixed, Note the commit id git switch master git merge --no-ff -m "merge branch bug01" bug01
-
Switch to dev branch to work
git switch dev git stash list // saved uncompleted task
two ways to reload work
-
reload work & delete space
git stash apply stash@{number} // only one task can ignore id git stash drop stash@{number}
-
one-step:
git stash pop
-
-
Copy bug fixed content to current work
git cherry-pick [commit-id]
Cooperative work
-
Remote list:
git remote -v
-
Link to remote repository
$ ssh-keygen -t rsa -C "youremail@example.com"
git remote add origin git@github.com:xxxxx.git
-
Delete link
git remote rm origin
-
Push branch[master, dev, feature] to remote
git push origin [branch_name]
-
Pull Rep
git clone git@github.com:xxxx.git
: only master branch
git switch -c dev origin/dev
: work in dev branch synchronize with Rep -
Note: if remote branch(e.g.:dev) has not a link with local,
git push
&git pull
may be unsuccessful, usegit branch --set-upstream-to <branch-name> origin/<branch-name
.
Tag management
Create tag
git tag
: list all tagsgit tag [tag_id]
:create a tag for latest commitgit tag [tag_id] [commit_id]
: for special commitgit tag -a [tag_id] -m "message" [commit_id]
: add messagegit show [tag_id]
Operate tag
- delete error tag:
git tag -d [tag_id]
- push local tag to remote:
git push origin [tag_id]
- push ALL local tags to remote:
git push origin --tags
- delete remote tag
- delete local tag :
git tag -d [id]
- delete remote:
git push origin :refs/tags/[id]
- check in remote.
- delete local tag :
Q1: After a commit finished, generate a tag and run git push origin [tag_id]
, whether the content that tag represents will be pushed to remote Repository?
A1: YES!
Q2: The content tag managed is independent on branch managed ?
A2: Result show that the answer may be “YES”. So the tag content is convenient for release & management.