Skip to content

Git#

Git is a tool allows people to track any tiny footprint.


Commend Git Command#

Configuration#

Set name#

git config user.name "your name"

Set email#

git config user.email "your email"

Start#

init#

Create a new local repository in current directory

git init [project name]

clone#

Clone from exist remote repository

git clone [url]

Working#

status#

Show modified files

git status

log#

Show commit history

git log

add#

Add a file to staging area

git add [file] git add . # add all files

restore#

Restore the code

gir restore [file]

rm#

Let code move to untracked

git rm -cached [file]

diff#

Show changes between working directory and staging area

git diff [file]

commit#

Create a new commit for changed files in the staging area

git commit -m "your commit"

reset#

Remove file from the staging area

git reset [HEAD^]

reset the last commit ^ means go back one stage, if there is ^^, it means go back two stage if there is too many times, you can write it as commit~3 or commit^3

Different

    only HEAD will move
    files in both working directory and staging area will be removed
    - default
    - files in staging area will be removed, but files in working directory will not
mixed soft hard
working directory keep keep remove
staging area remove keep remove

Caution

使用 --hard 的時候請注意!!


Storing#

stash#

Put current changes in your working directory into stash for later use

git stash

Different

    list stash
    remove stashed state form the stash list
    remove one stash from the list
    remove all stash

Branching#

branch#

List all local branches in repository

git branch

Different

    list branches
    delete the branch

checkout#

Switch working directory to the other branch

git checkout [-b]

-b: create the specified branch if it does not exist

merge#

Join specified branch into current branch

git merge [branch]

rebase#

Reapply commits on top of another base tip

git rebase [branch]

git rebase

git rebase --continue
git rebase --skip
git rebase --abort


Synchronizing#

fetch#

Fetch changes form the remote

git fetch [remote]

pull#

Fetch changes from the remote and merge current branch

git pull [remote]
  • git pull = git fetch + git merge

push#

Push local changes to the remote

git push

remote#

> Display the remote repository
git remote
git remote -v # show the URL used by both fetch and push
git remote add [name] [url] # add a new repository

Diagram#

sequenceDiagram
    participant w as Workspace
    participant sa as Staging Area
    participant d as Branch develop
    participant m as Local Repo
    participant r as Remote Repo
    participant c as Colleagues

    rect
    m ->> w: git init
    r ->> w: git clone
    end

    w ->> sa: git add
    sa ->> m: git commit
    m ->> d: git checkout
    d ->> m: git merge
    Note over d, m: Merge request
    m ->> r: git push
    c ->> r: git push

    rect 
    r ->> w: git pull
    r ->> m: git fetch
    m ->> w: git merge
    end

    m -) w: git reset --hard
    m -) sa: git reset --soft
    Note over w, sa: git reset --mixed

.gitignore#

  • ignore
  • .gitignore