git小白的打怪升级之程

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 addgit commit : both modification have been committed to Repo.
  • git diff HEAD -- [file_name]: check the difference between Working Directory & Repository.

Cancel modification

there are 4 situation:

  • modified but not to submit to Stagegit restore file_name
  • modified and submitted to Stage (git add) → git restore --staged <file>
    both operation can run git 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

Branch management

Create and switch to a new branch

  • create a new branch ‘dev’ and switch to it :
    git branch dev                              // create a new branch named 'dev'
    git checkout/switch dev                     // switch to branch 'dev'
    git branch                                  // observe branch status
    
    it also can be realized via :
    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, use git branch --set-upstream-to <branch-name> origin/<branch-name.


Tag management

Create tag

  • git tag: list all tags
  • git tag [tag_id]:create a tag for latest commit
  • git tag [tag_id] [commit_id] : for special commit
  • git tag -a [tag_id] -m "message" [commit_id]: add message
  • git 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
    1. delete local tag :git tag -d [id]
    2. delete remote: git push origin :refs/tags/[id]
    3. check in remote.

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.


The part to learn

.gitignore

Server config

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值