Git 初步

『刚刚下载好软件,配置』

1. 三种配置文件,等级不同,由大到小

2. identity设置

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
用 $ git config --list 来查看

默认的编辑器
$ git config --global core.editor emacs

『开始』

1. initialize a repository

$ cd /home/user/my_project
$ git init
$ git add *.c (git add -A)
$ git commit -m 'initial project version'

2. gitignore文件

$ git status



正则表达

3. unstage a file

$ git reset <file>
$ git reset (unstage all the files)

4. 放弃working file的变化

$ git checkout . 放弃所有变化
$ git checkout <file> 放弃某个文件的变化

注意和下面的对比

5. 恢复到某个commit

$ git reflog
$ git checkout <hash>
进入detached branch,做出一点修改后,如果想要保留这个变化,可以新建一个branch,变化后的文件会commit到新的branch上面
$ git checkout <new_branch_name>

6. 查看历史修改

$ git log
$ git log --stat (详细)
$ git log -p (所有,相当于diff <comit n> <commit n-1>)
$ git log -p -n (只查看最近n次的)

7. 分支branch

$ git branch <xxx> 建立分支
$ git branch 查看所有分支
$ git checkout <xxxbranch> 切换到某个分支
$ git branch -m <old_branch_name> <new_branch_name> 分支重命名

8. 对比 git diff

$ git difftool 默认比较working file和staged file
$ git difftool <hash1> <hash2>

『处理bad commitment』

1. 不要working directory的变化

$ git checkout <file>

2.写错commit message

$ git commit --amend -m "xxxxx"

3. 补充last commitment

$ git add <file>
$ git commit --amend 然后会冒出一个inactive的文档

4. commit错branch 【这个还有一些问题】

$ git checkout <源branch>
$ git cherrypick <hash>
$ git checkout master
三种reset

  • changes丢到了staged
    $ git reset --soft <hash of initial commit>
  • changes丢到了working directory
    $ git reset <hash of initial commit>
  • changes丢掉了
    $ git reset --hard <hash of initial commit>

5. 清除所有的untracked 文件

$ git clean -df
d表示directory,f表示file
解压缩文件放错地方时,用这个清除比较方便

6. 删错的恢复,见上文的5

7. 得到某个commit的逆patch 【没试过】

当发布了某个版本,但是觉得不ok,通过发布补丁的方式倒回去
$ git revert <hash>

7. 比较某两个commit 【没试过】

$ git diff <hash1> <hash2>

『反复更改——stash』

git stash save "message" 这时候会恢复到上一个commitment,所以需要apply stash@{0}
git stash list
git stash apply stash@{n} 应用某个stash
git stash drop stash@{n} 删除某个stash
git stash pop (apply the first stash and drop it)
git stash clear 删除全部stash

github_with _local_repo

  • git push <URL.git> <branch>
  • git remote add <alias_name> <URL.git> 从而下一次push的时候不用输入一大串URL
  • git push <alias_name> <branch>

github_build_from_scratch

不需要新建文件夹
git clone <URL.git>

  • git remote -v 【截图】
  • git push origin <master或其他branch>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容