GIT Command (To Be Continued)

来源:互联网 发布:win7碎片整理软件 编辑:程序博客网 时间:2024/05/22 12:53

帮助命令

git help command // eg: git commit help

windows 打开默认的浏览器显示帮助内容, mac直接显示

配置

git config --global setting value示例:git config --global user.name "Your Name"示例:git config --global user.email "you@someplace.com"git config --global --list // 列出全局配置项

配置内容保存在当前用户目录下的.gitconfig文件中

本地命令

初始化

方式一:

cd projects/git init git-demo  // projects下面创建文件夹 git-demo, 并初始化; 初始化其实就是在文件夹下面创建了相关内容存放在.git 隐藏文件夹下面

方式二:

cd projects/mkdir websitecd website/git init // 初始化

方式三:
大多数的方式,我们从clone一个git 库开始的。

git clone 'url'

查看状态 -> 添加新文件 -> 提交修改

git status // Shows which files have been modified in the working directory vs Git's staging area.git add file-name  // Adds the new or newly modified file-name to Git's staging area (index).
git commit -m "A really good commit message" // Commits all files currently in Git's staging area.

上面的命令只有所有的文件都在staging area在有效。 git commit -am "A really good commit message" 可以省掉git add这步,不过新文件必须先add下。

git add . // Add all new and newly modified files.git reset HEAD file-name // Unstage the specified file from stage area. 修改的内容还在git checkout -- file-name // 回滚本次修改

检查修改内容

git diff // 查看unstage状态下的文件的修改内容,staged的无法查看

合并到上次提交

git commit --amend // 将当前的staged的修改合并到上次commit,并打开编辑器修改commitgit commit --amend -m "New commit message" // 将当前的staged的修改合并到上次commit,并实用新的Message

使用Interactive Rebasing/squash也可以达到合并的效果,区别就是一个是事先(commit 前)就合并,一个是事后(commit 后)合并。

切换分支

切换分支前必须保证工作空间是干净的。(没有未提交的修改和新增)

git checkout branchename // 切换到branchname分支

日志

git loggit log --oneline --graph --decorate --color

移除文件

方式一:完全通过git命令

git rm debug.log  // remove and stage the changegit commit -m 'remove file debug.log'

方式二:
非git 命令删除文件后,运行下面的命令

git add -u // git 2.0 以前的版本  stage删除的changegit add file-name // git 2.0 后也可以通过这个命令达到上面的命令的效果git commit -m 'commit message'

移动文件

git mv index.html web/  // 移动index.html 到web文件夹内。 命令完成后直接进入staging 状态git commit -m 'move index.html into web folder'

ignore 文件

编辑 .gitignore 文件

SSH 命令

windows cmd并没有自带ssh命令,我们可以通过git bash命令窗来运行这些命令。
假定在当前用户的目录下:

cd .sshssh-keygen -t rsa -C "your email" // 生成SSH Key, 将id_rsa.pub公钥配置到github/bitbucket 等服务器上ssh -T git@github.com // 验证SSH 配置成功

Git Remote 相关命令

关联一个远程的Repo。 (针对前两种初始化方式,一般情况用不上)

git remote add remote-name remote-repository-location // 示例: git remote add origin git@github.com:choelea/keycloak-demo.gitgit push -u remote-name branch-name // 示例: git push -u origin master;  The -u parameter is needed the first time you push a branch to the remote.git remote -v // list the names of all the remote repositories

关联远程repo之前需要先在git服务器上创建对应的repo,如果采用的是github,在创建repository后,会有如下的提示:
git reposority tips

git pull origin master // 下载当前分支远程修改;每次push前都应该先pull

Git Rebase

关于rebase和merge的区别,建议参考:Merging vs Rebasing

一定要看看 ‘The Golden Rule of Rebasing’这部分.
rebase 和 merge都是应该发生在分支之间的事情,当然在同一个分支上有时也需要。(可能不是最佳实践,git的开发流程一般建议创建单独的feature分支来完成不同的story,避免出现多人在同一个分支上直接commit)

Squash

通过Interactive Rebasing来完成当前分支的commits的squash

git rebase -i // 列出所有未push的commit,注意是倒序

根据提示编辑来达到squash的作用。
这里写图片描述
将第二个commit(435d22b)修改为:pick 435d22b ... 即将这个commit压缩至上面的commit,并放弃当前的commit message。

有些公司会很强调squash。 git估计本地多次提交防止丢失,所以git的commit有可能会很多;而svn的commit就意味着修改可以被其他用户拉取到, 所以svn的每一次commit都要保证系统可以运行,svn的commit会偏少。svn的代码更新时间取决于文件多少和大小;git的代码拉取时间取决于commit的多少。所以。。。是每次提交尽量合理依然很重要,squash/Ineractive Rebasing 很实用。

原创粉丝点击