*初始化git仓库,使用git init命令

*添加文件到git仓库分两步:

1、使用git add filename  ;可分多次使用,添加多个文件到暂存区

2、使用git commit -m  “说明”  ;完成提交到分支

*查看工作区状态,使用git status 命令;如果提示有修改可使用git diff filename 查看修改内容

*HEAD指向当前版本,HEAD^表示上一个版本,HEAD^^上上一个版本……HEAD~100指向之前第100个版本。

*回退版本:使用git log查看提交历史;使用git log --pretty=oneline 精简显示

使用git reset --hard commit_id 回退到版本号为commit_id的版本

*回退版本之后如果想再看改回来,可以使用git reflog 查看历史命令,找出想改回的版本号,再使用git reset hard commit_id 返回即可。

*注意:git跟踪并管理的是修改,而不是文件,如果一个文件修改并add之后,再次修改,如果不再次add就提交的话,只会提交第一次的修改。

*撤销修改:

1、如果文件还在工作区,即没有add也没有commit,则使用git checkout -- filename 还原到服务器版即可;

2、如果已经add到暂存区,首先使用git reset HEAD filename从暂存区取回工作区,再按照1进行操作即可;

3、如果已经提交到版本库,则按照版本回退的方式进行修改即可;

4、如果已经push到远程仓库,就麻烦了

*删除使用以下命令:

1、git rm filename 从工作区删除

2、git commit -m ”说明“  更新分支中文件进行删除

将在工作区的文件删除之后,可以使用git checkout -- filename 从分支中取回,但是只能恢复文件到最新版本,最后一次提交之后的修改则不能恢复。

*分支:

1、创建分支

git checkout -b branchname  创建并切换到改分区,相当于一下两个命令:

git branch branchname  创建分支

git checkout branchname  切换到分区

2、查看当前指向的分支:git branch  会列出所有分支,当前指向的分支之前多了个*

3、切换分支就是git checkout branchname

4、合并分支:git merge branchname 合并branchname到当前分支

5、删除分支:git branch -d branchname 删除branchname分支

注意:创建、合并、删除分支都非常快,git鼓励使用分支完成某个任务,合并后删除分支,和直接在master分支上进行工作是一样的效果,但是过程更加安全;  这些之所以快是因为在这些过程中我们只是修改了指向分支的指针,如创建一个branch就是创建了一个指向分支的指针,然后修改HEAD指向该指针;即HEAD指向分支,分支就是提交。

*冲突解决:git无法自动合并分支时,就必须首先解决冲突;解决冲突之后,再提交,即完成了合并

使用git log --graph 可以查看分支合并图。

*保存工作现场  git stash  保存之后就可以进行其他工作  而不影响上次的修改

恢复工作现场:1、git stash apply  恢复时并不删除stash中内容

2、git stash pop    恢复时会删除stash中的内容

*远程库信息产看使用git remote (-v)加上-v显示信息更加详细

*分支推送到远程库:即将所有本地的提交推送到远程库

git push origin(远程库名) master (要推送的分支)

*抓取分支:git pull  ; git clone

*协作模式:

1、使用git push origin branchname 推送自己的修改

2、如果推送失败,因为远程分支比本地更新,先使用git pull 合并

3、如果合并有冲突,解决冲突,在本地提交

4、再推送

注意:如果使用git pull 合并时提示 ”no tracking information“说明本地分支没有和远程分支建立链接关系,使用以下指令建立关系:git branch --set -upstream branch origin/branchname

*在本地创建与远程对应的分支:git branch -b branchname origin/branchname  本地与远程分支的名称最好一致

*创建标签

1、打标签git tag name  默认标签打在最新提交的commit上,如果想打在其他版本上,找到commit_id即可

2、显示标签:git log -pretty=oneline --abbrev -commit

git tag tag_name commit_id

3、查看标签:git tag  显示所有标签

4、查看标签信息:git show tag_name

5、创建带有说明的标签: git tag -a tag_name -m ”信息“;-a表示标签名,-m指定说明文字

*操作标签:git tag -d tag_name 删除标签

推送标签到远程库:git push origin tag_name

一次推送所有标签到远程库:git push origin --tag

永久删除不小心commit的文件

Remove sensitive data

 

Some day you or a collaborator may accidentally commit sensitive data, such as a password or SSH key, into a Git repository. Although you can remove the file from the latest commit with git rm, the file will still exist in the repository's history. Fortunately, there are other tools that can entirely remove unwanted files from a repository's history. This article will explain how to use two of them: git filter-branch and theBFG Repo-Cleaner.

Danger: Once you have pushed a commit to GitHub, you should consider any data it contains to be compromised. If you committed a password, change it! If you committed a key, generate a new one.

This article tells you how to make commits with sensitive data unreachable from any branches or tags in your GitHub repository. However, it's important to note that those commits may still be accessible in any clones or forks of your repository, directly via their SHA-1 hashes in cached views on GitHub, and through any pull requests that reference them. You can't do anything about existing clones or forks of your repository, but you can permanently remove all of your repository's cached views and pull requests on GitHub by contacting GitHub support.

Purging a file from your repository's history

Using filter-branch

To illustrate how git filter-branch works, we'll show you how to remove the Rakefile from the history of the GitHub gem repository (and add it to .gitignore to ensure that it is not accidentally re-committed).

  1. Clone the GitHub gem repository.

    git clone https://github.com/defunkt/github-gem.git
    # Initialized empty Git repository in /Users/tekkub/tmp/github-gem/.git/
    # remote: Counting objects: 1301, done.
    # remote: Compressing objects: 100% (769/769), done.
    # remote: Total 1301 (delta 724), reused 910 (delta 522)
    # Receiving objects: 100% (1301/1301), 164.39 KiB, done.
    # Resolving deltas: 100% (724/724), done.
    
  2. Navigate to the repository's working directory.

    cd github-gem
    
  3. Run git filter-branch, forcing (--force) Git to process—but not check out (--index-filter)—the entire history of every branch and tag (--tag-name-filter cat -- --all), removing the specified file ('git rm --cached --ignore-unmatch Rakefile') and any empty commits generated as a result (--prune-empty). Note that you need to specify the pathto the file you want to remove, not just its filename.

    Be careful! This will overwrite your existing tags.

    git filter-branch --force --index-filter \
    'git rm --cached --ignore-unmatch Rakefile' \
    --prune-empty --tag-name-filter cat -- --all
    # Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266)
    # Ref 'refs/heads/master' was rewritten
    

    If the file used to exist at any other paths (because it was moved or renamed), you must run this command on those paths, as well.

  4. Add the Rakefile to .gitignore to ensure that you don't accidentally commit it again.

    echo "Rakefile" >> .gitignore
    git add .gitignore
    git commit -m "Add Rakefile to .gitignore"
    # [master 051452f] Add Rakefile to .gitignore
    #  1 files changed, 1 insertions(+), 0 deletions(-)
    
  5. Double-check that you've removed everything you wanted to from your repository's history, and that all of your branches are checked out.

  6. Once you're happy with the state of your repository, force-push your local changes to overwrite your GitHub repository, as well as all the branches you've pushed up:

    git push origin --force --all
    # Counting objects: 1074, done.
    # Delta compression using 2 threads.
    # Compressing objects: 100% (677/677), done.
    # Writing objects: 100% (1058/1058), 148.85 KiB, done.
    # Total 1058 (delta 590), reused 602 (delta 378)
    # To https://github.com/defunkt/github-gem.git
    #  + 48dc599...051452f master -> master (forced update)
    
  7. In order to remove the sensitive file from your tagged releases, you'll also need to force-push against your Git tags:

    git push origin --force --tags
    # Counting objects: 321, done.
    # Delta compression using up to 8 threads.
    # Compressing objects: 100% (166/166), done.
    # Writing objects: 100% (321/321), 331.74 KiB | 0 bytes/s, done.
    # Total 321 (delta 124), reused 269 (delta 108)
    # To https://github.com/defunkt/github-gem.git
    #  + 48dc599...051452f master -> master (forced update)
    
  8. Tell your collaborators to rebasenot merge, any branches they created off of your old (tainted) repository history. One merge commit could reintroduce some or all of the tainted history that you just went to the trouble of purging.

  9. After some time has passed and you're confident that git filter-branch had no unintended side effects, you can force all objects in your local repository to be dereferenced and garbage collected with the following commands (using Git 1.8.5 or newer):

    git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
    git reflog expire --expire=now --all
    git gc --prune=now
    # Counting objects: 2437, done.
    # Delta compression using up to 4 threads.
    # Compressing objects: 100% (1378/1378), done.
    # Writing objects: 100% (2437/2437), done.
    # Total 2437 (delta 1461), reused 1802 (delta 1048)
    

    Note that you can also achieve this by pushing your filtered history to a new or empty repository and then making a fresh clone from GitHub.

Using the BFG

The BFG Repo-Cleaner is a faster, simpler alternative to git filter-branch for removing unwanted data. For example, to remove any file named 'Rakefile' (and leave your latest commit untouched), run:

bfg --delete-files Rakefile

To replace all text listed in passwords.txt wherever it can be found in your repository's history, run:

bfg --replace-text passwords.txt

See the BFG Repo-Cleaner's documentation for full usage and download instructions.

Avoiding accidental commits in the future

There are a few simple tricks to avoid committing things you don't want committed:

  • Use a visual program like GitHub for Mac or gitk to commit changes. Visual programs generally make it easier to see exactly which files will be added, deleted, and modified with each commit.
  • Avoid the catch-all commands git add . and git commit -a on the command line—use git add filename and git rm filename to individually stage files, instead.
  • Use git add --interactive to individually review and stage changes within each file.
  • Use git diff --cached to review the changes that you have staged for commit. This is the exact diff that git commit will produce as long as you don't use the -a flag.

最新文章

  1. OpenSSL 使用拾遗(一)---- 生成 pkcs12 文件
  2. TCP数据包的封包和拆包
  3. onhashchange事件,只需要修改hash值即可响应onhashchange事件中的函数(适用于上一题下一题和跳转页面等功能)
  4. android 圆角边框及图片
  5. 冲突--ScrollView嵌套ListView只显示一行
  6. Android笔记——JDK实现WebService服务
  7. cocos2d-x 读取 json 文件并用 jsoncpp 做解析
  8. java.io与网络通信
  9. Android技术分享-文字转语音并朗读
  10. dubbo服务的发布和调用
  11. linux下shell中执行命令的顺序问题
  12. SpringCloud系列——Eureka 服务注册与发现
  13. c#一步一步实现ORM(二)
  14. 基于服务器AAA实验
  15. Django搭建博客记(一)
  16. windows 内建环境变量
  17. linux 服务器安装 nginx
  18. Linux下调整ext3分区大小【转】
  19. python 获取命令行参数
  20. mysql_事务

热门文章

  1. 斯坦福 IOS讲义 课件总结 二
  2. HDU 2393 Higher Math
  3. BZOJ 2016: [Usaco2010]Chocolate Eating
  4. hdoj 1166 敌兵布阵(树状数组)
  5. 全栈JavaScript之路(七)学习 Comment 类型节点.
  6. uva311 - Packets(贪心)
  7. MVC表单提交加JS验证
  8. C - N皇后问题(搜索)
  9. jQuery+Ajax+Jsp做二级级联
  10. 【转】从底层了解ASP.NET体系结构