Git的简介与常用指令

简介

git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。

注意gitgithub的区别:

git是一个版本控制工具
github是一个用git做版本控制的项目托管平台。

常用指令

Configure User Information

Configure user information for all local repositories

  • Sets the name you want attached to your commit transactions

    git config --global user.name "[name]"

  • Sets the email you want attached to your commit transactions

    git config --global user.email "[email address]"

  • Enables helpful colorization of command line output

    git config --global color.ui auto

Create repositories

Start a new repository or obtain one from an existing URL

  • Creates a new local repository with the specified name

    git init [project-name]

  • Downloads a project and its entire version history

    git clone [url]

Review Changes

Review edits and craft a commit transaction

  • Lists all new or modified files to be committed

    git status

  • Shows file differences not yet staged

    git diff

  • Snapshots the file in preparation for versioning

    git add [file]

    git add . can snapshot all files for versioning

  • Shows file differences between staging and the last file version

    git diff --staged

  • Unstages the file, but preserve its contents

    git reset [file]

  • Records file snapshots permanently in version history

    git commit -m "[descriptive message]"

Group Changes

Name a series of commits and combine completed efforts

  • Lists all local branches in the current repository

    git branch

  • Creates a new branch

    git branch [branch-name]

  • Switches to the specified branch and updates the working directory

    git checkout [branch-name]

  • Combines the specified branch’s history into the current branch

    git merge [branch]

  • Deletes the specified branch

    git branch -d [branch-name]

Refactor Filenames

Relocate and remove versioned files

  • Deletes the file from the working directory and stages the deletion

  • Removes the file from version control but preserves the file locally

  • Changes the file name and prepares it for commit

Suppress Tracking

Exclude temporary files and paths

  • A text file named .gitignore suppresses accidental versioning of
    files and paths matching the specified patterns

    1
    2
    3
    4
    #This is the sample context in the file .gitignore
    *.log
    build/
    temp-*
  • Lists all ignored files in this project

    git ls-files --other --ignored --exclude-standard

Save Fragments

Shelve and restore incomplete changes

  • Temporarily stores all modified tracked files

    git stash

  • Restores the most recently stashed files

    git stash pop

  • Lists all stashed changesets

    git stash list

  • Discards the most recently stashed changeset

    git stash drop

Review History

Browse and inspect the evolution of project files

  • Lists version history for the current branch

    git log

  • Lists version history for a file, including renames

    git log --follow [file]

  • Shows content differences between two branches

    git diff [first-branch]...[second-branch]

  • Outputs metadata and content changes of the specified commit

    git show [commit]

Redo Commits

Erase mistakes and craft replacement history

  • Undoes all commits after [commit], preserving changes locally

    git reset [commit]

  • Discards all history and changes back to the specified commit

    git reset --hard [commit]

Synchronize Changes

Register a repository bookmark and exchange version history

  • Downloads all history from the repository bookmark

    git fetch [bookmark]

  • Combines bookmark’s branch into current local branch

    git merge [bookmark]/[branch]

  • Uploads all local branch commits to GitHub

    git push [alias] [branch]

  • Downloads bookmark history and incorporates changes

    git pull

Download the Specific File/Directory from Github

1
2
3
4
5
6
7
mkdir project_folder
cd project_folder
git init
git remote add -f origin <url>
git config core.sparsecheckout true
echo “fileORdir_you_want” >> .git/info/sparse-checkout
git pull origin master
梦想还是要有的,万一实现了呢?