UP | HOME

Git使用

Table of Contents

Git安装

安装

GitHub官方帮助文档(包含Windows,OS X,Linux)

TortoiseGit

类似TortoiseSvn的客户端,图形化界面操作。

Windows下配置TortoiseGit与msysGit环境

配置

免密码执行git命令

  • git库在github上

    上传公钥到github后,可以在本地~/.ssh/config配置文件中写入如下配置:

    Host github.com
      User git
      Hostname github.com
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/private_key
    

    然后执行:

    ssh -T git@github.com
    

    会返回一条认证成功信息。

  • git库在只有IP的服务器上

    如果git库部署在只有IP地址的服务器上,如何实现免密码执行git命令?方法如下。

    首先在服务器上的~/.ssh目录下部署你的公钥文件。

    然后在本地,添加如下内容到 ~/.ssh/config 文件中:

    1: Host            gitserv
    2:     Hostname        199.199.199.199
    3:     IdentityFile    ~/.ssh/private_key
    4:     IdentitiesOnly yes
    

    在git库工作目录下添加remote库:

    git remote add origin git@gitserv:myrepo.git
    

    然后执行git命令就不用再使用密码:

    1: git push orgin master
    

    参考:http://stackoverflow.com/a/11251797/1036812

关键概念

与SVN不同,Git代码提交(commit)时,只是提交到了本地仓库,并没有提交到远程代码库中。
将暂存区文件提交到远程代码库:

git remote add origin git@github.com:username/Hello-World.git
git push -u origin master

操作

基本操作

  • 检查当前文件状态
git status
  • 跟踪新文件
git add FILE_NAME
  • 暂存(stage)已修改文件
git add FILE_NAME
  • 提交更新
git commit

或者

git commit -m "input comment here"
  • 移除文件
git rm FILE_NAME

从跟踪清单中删除:

git rm --cached FILE_NAME
  • 重命名文件
git mv file_from file_to
  • 更新文件
git fetch origin
  • 更新后合并文件
git merge origin/master
  • 批量删除

已经批量删除(rm)的文件,批量提交:

git add -A

它会把我们未通过 git rm 删除的文件全部stage。

撤销提交

撤销操作-单个文件

参考:Git 基础 - 撤消操作 - Progit

  • 取消对文件的修改

    如果觉得刚才对 benchmarks.rb 的修改完全没有必要,该如何取消修改,回到之前的状态(也就是修改之前的版本)呢?

git checkout -- benchmarks.rb

撤销一次 commit

$ git commit -m "Something terribly misguided"              (1)
$ git reset --soft HEAD~                                    (2)
<< edit files as necessary >>                               (3)
$ git add ...                                               (4)
$ git commit -c ORIG_HEAD                                   (5)
  1. This is what you want to undo
  2. This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message1, or both. Leaves working tree as it was before git commit.(使用 soft 参数将保留 commit 工作区的修改).
  3. Make corrections to working tree files.
  4. git add whatever changes you want to include in your new commit.
  5. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option instead.

参考:http://stackoverflow.com/questions/927358/how-do-you-undo-the-last-commit

更新已经fork的仓库

  1. Open Terminal (for Mac and Linux users) or the command line (for Windows users).
  2. Change the current working directory to your local project.
  3. 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
    # remote: Counting objects: 75, done.
    # remote: Compressing objects: 100% (53/53), done.
    # remote: Total 62 (delta 27), reused 44 (delta 9)
    # Unpacking objects: 100% (62/62), done.
    # From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
    #  * [new branch]      master     -> upstream/master
    
  4. Check out your fork's local master branch.
    git checkout master
    # Switched to branch 'master'
    
  5. 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
    # Updating a422352..5fdff0f
    # Fast-forward
    #  README                    |    9 -------
    #  README.md                 |    7 ++++++
    #  2 files changed, 7 insertions(+), 9 deletions(-)
    #  delete mode 100644 README
    #  create mode 100644 README.md
    

If your local branch didn't have any unique commits, Git will instead perform a "fast-forward":

git merge upstream/master
# Updating 34e91da..16c56ad
# Fast-forward
#  README.md                 |    5 +++--
#  1 file changed, 3 insertions(+), 2 deletions(-)

参考:

  1. https://help.github.com/articles/syncing-a-fork/
  2. http://stackoverflow.com/questions/7244321/how-to-update-github-forked-repository

更新时发生冲突

更新了本地代码但没有commit,此时执行pull时会发生错误:

error: Your local changes to the following files would be overwritten by merge:
    xxx/xxx/xxx.txt
Please, commit your changes or stash them before you can merge.
Aborting

有两种解决办法:

  1. 放弃本地更改,用远程仓库内容覆盖本地:
    git reset --hard
    git pull
    
  2. 用stash保存本地更改,然后更新

    执行:

    git stash
    git pull
    git stash pop
    

    接下来diff一下此文件看看自动合并的情况,并作出相应修改。

    解释:

    • git stash: 备份当前的工作区的内容,将当前的工作区内容保存到Git栈中。然后从最近的一次提交中读取相关内容,让工作区和上次提交的内容一致;
    • git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复;

    相关操作:

    • git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复;
    • git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。
    • git stash show -p stash@{0}:显示各个 stash 内容

参考:http://blog.csdn.net/lincyang/article/details/21519333

链接

Date: 2014-10-25

Author: 马楠

Created: 2015-12-24 Thu 23:20

Emacs 24.3.1 (Org mode 8.2.10)

Validate