> For the complete documentation index, see [llms.txt](https://lifelong-learning.gitbook.io/pythonjourney/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lifelong-learning.gitbook.io/pythonjourney/git-and-github/git-command.md).

# Git command

## Crash Course

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

## Conceptual Model

![](/files/-LU_m2EERHV4yzU_D5PM)

[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)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lifelong-learning.gitbook.io/pythonjourney/git-and-github/git-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
