# Git command

## Crash Course

[Most simple and useful](http://www.bootcss.com/p/git-guide/)

## Conceptual Model

![](https://2389839300-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCZTjhTe719eHovxKB0%2F-LU_m1CVRmg2Z3QlC1ts%2F-LU_m2EERHV4yzU_D5PM%2F2018-12-25-21-37-29.png?generation=1545748364134535\&alt=media)

[More detail here](https://git-scm.com/book/en/v2/Getting-Started-Git-Basics)

## Removing Files

### Option1-Manually

If you modify file, and commit it. Now you want to delete the file on Git and working directory, use`git rm <file>` to remove file from working directory and stage, then `git commit`.The removing action would be recorded as a new commit.

```
git rm <file>
git commit -m "<comment>"
git push origin master
```

### Option 2

If you commit new files, and want to remove them all, just to find the commit that doesn't contain these files. Use the following code:

```
git reset --hard <commit hash>
git push --force origin master
```

The first line of code `get reset...`repoint HEAD to the commit mentioned, copy file to stage, and refresh working directory according to it. The action would cause "your branch behind origin/ master", so you can't just `git push` to remote repository, must use `git push --force`.

[More detail here](https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified)

## Unmodifying a Modified File

You could use the code to cancel the uncommitted modification: `git checkout -- <file>`. It copy the file on stage to cover working directory. `git checkout -- .` With a dot, it cancel all the uncommitted modification.

## Unstaging a Staged File

If you add file to stage mistakenly, use code `git reset HEAD <file>` to unstage. It would copy the file on commit and thus cover the file on stage.

## Command of refreshing local gitbook command to remote

```
#first refresh remote change to local 
git pull
#add local change to remote
git add *
git commit -m "code info"
git push origin master
```

## Usual commands

* Creating repo:

  ```
    git init
  ```
* Clone repo:
  * local:

    ```
      git clone /path/to/repository
    ```
  * remote:

    ```
      git clone username@host: /path/to/repository
    ```
* Working Procedure of Local repo: working directory-`add`-index(stage)-`commit`-HEAD
* Add file:

  ```
    git add <filename>/*
  ```
* Commit:

  ```
    git commit -m "describ info"
  ```
* Push to remote:

  ```
      git push origin master
  ```
* 若还没有克隆现有仓库，并欲将你的仓库连接到某个远程服务器：

  ```
      git remote add origin <server>
  ```
* Creat "feature\_x" branch, and switch to it:

  ```
    git checkout -b feature_x
  ```
* Switch back to another branch(e.g. master):

  ```
      git checkout master
  ```
* Delete feature\_x branch:

  ```
    git branch -d feature_x
  ```
* You need to push the branch to remote, unless it's unseen:

  ```
      git push origin <branch>
  ```
* Refresh local repo to the newest :

  ```
      git pull
  ```
* Fetch and **merge** remote modification:

  ```
    git merge <branch>
  ```
* Sometime, the merge could come cross conflicts, then you need to modify file manually, when you complete, add the file:

  ```
      git add <filename>
  ```
* To find the difference between different version:

  ```
      git diff <source_branch> <target_branch>
  ```
* Tag: `git tag 1.0.0 1b2e1d63ff` ( 1b2e1d63ff is the ID of file which you can get through `git log`)
* Cancel local modification to the file in Head:

  ```
    git checkout --<filename>
  ```
* If you want to discard all your local modification and commits, you can get the version in remote repo, and aim your local brach to it:

  ```
    git fetch origin 
    git reset --hard origin/master
  ```
* GUI:

  ```
    gitk
  ```
* Colorful GUI: git config color.ui true

  > **More info:**

  [Git Guide](http://www.bootcss.com/p/git-guide/)

  [Git Cheat Sheat](https://github.com/flyhigher139/Git-Cheat-Sheet)
