GIT

常用命令

  • 查看已建立的连接

    git remote -v
    
  • 取消对文件的修改。还原到最近的版本,废弃本地做的修改。

    git checkout -- <file>
    
  • 取消已经暂存的文件。即,撤销先前”git add”的操作

    git reset HEAD <file>...
    
  • 回退到上一个版本,即commit后的撤销操作,保留工作区的改变

    git reset HEAD^ <file>...
    
  • 回退到上一个版本

    git reset --hard HEAD^ <file>...
    

Stashing

当我在一个分支fa中修改了文件,但是还没有完全改好,此时我并不想add/commit,但是这个时候有一个更急迫的事情在另外一个分支fb上需要我去做,我必须要切换分支。

  • 将当前分支文件储藏起来

    git stash
    
  • 恢复刚刚储藏的数据

    git stash apply
    
  • 查看储藏

    git stash List
    
  • 恢复到之前的某一次储藏

    git stash apply stash@{1}
    
  • 恢复储藏并出栈, 将栈顶的储藏移除

    git stash pop
    
  • 删除某一个储藏

    git stash drop stash@{4}
    

分支管理

  • 新建分支

    git branch experimental
    
  • 获取分支列表

    git branch
    
  • 切换分支

    git checkout experimental
    
  • To merge the changes made in experimental into master

    git merge experimental
    
  • 删除分支

    git branch -d experimental
    

Configuring a remote for a fork

  • List the current configured remote repository for your fork.

    $ git remote -v
    > origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
    > origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
    
  • Specify a new remote upstream repository that will be synced with the fork.

    $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
    
  • Verify the new upstream repository you’ve specified for your fork.

    $ git remote -v
    > origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
    > origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
    > upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
    > upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
    

Sync a fork of a repository to keep it up-to-date with the upstream repository.

  • Fetch the branches and their respective commits from the upstream repository. Commits to master will be stored in a local branch, upstream/master

    $ git fetch upstream
    
  • Check out your fork’s local master branch.

    $ git checkout master
    > Switched to branch 'master'
    
  • Merge the changes from upstream/master into your local master branch. This brings your fork’s master branch into sync with the upstream repository, without losing your local changes.

    $ git merge upstream/master