Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git实用命令 #8

Open
hawx1993 opened this issue Mar 26, 2017 · 0 comments
Open

git实用命令 #8

hawx1993 opened this issue Mar 26, 2017 · 0 comments
Labels

Comments

@hawx1993
Copy link
Owner

hawx1993 commented Mar 26, 2017

删除未被track过的文件

$ git clean -f;// 删除当前目录下所有没有track过的文件. 他不会删除.gitignore文件里面指定的文件夹和文件, 不管这些文件有没有被track过
$ git clean -f <path> ;//删除指定路径下的没有被track过的文件
$ git clean -df;//删除当前目录下没有被track过的文件和文件夹
$ git clean -xf;//删除当前目录下所有没有track过的文件. 不管他是否是.gitignore文件里面指定的文件夹和文件

your local changes would be overwritten by checkout

git stash save your-file-name
git checkout yourBranch

git删除添加远程源

添加前请先删除

git remote remove origin
git remote add origin xxx

gitlab和github协同

ssh-keygen -t rsa -C "example@your-email.com" -b 4096
生成ssh_key,然后粘贴pub文件到gitlab:https://gitlab.com/profile/keys 或者github

config file 配置:

# GitLab.com server
  Host gitlab.com
  IdentityFile ~/.ssh/id_gitlab_rsa

# github
Host github
  HostName github.com
  IdentityFile ~/.ssh/id_github_rsa

git查看当前用户名和邮箱

$ git config user.name

$ git config user.email

Please move or remove them before you can merge

在使用git pull时,经常会遇到报错:

error: The following untracked working tree files would be overwritten by merge:

Please move or remove them before you can merge

▲ 问题分析 :

这是因为本地有修改,与云端别人提交的修改冲突,又没有merge.

▲ 解决方案 :

如果确定使用云端的代码,最方便的解决方法是删除本地修改,可以使用以下命令:

$ git clean  -d  -fx ""

d -----删除未被添加到git的路径中的文件
f -----强制运行
x -----删除忽略文件已经对git来说不识别的文件

cherry-pick

git cherry-pick <commit id>
# 在master分支摘取test分支的commit文件
➜ git cherry-pick c5589363ae5027
[master 1c563c4] test
 Date: Thu May 24 22:11:15 2018 +0800
 1 file changed, 1 insertion(+)

reset和revert

# 下面这两条命令让 hotfix 分支向后回退了两个提交。
git checkout hotfix
git reset HEAD~2
#舍弃没有提交的改动,文件恢复到上一次commit状态
git reset --hard HEAD

#git revert是用一次新的commit来回滚之前的commit
git checkout hotfix
git revert HEAD~2

git revert撤销某次操作,此次操作之前的commit都会被保留。
git reset 是撤销某次提交,但是此次之后的修改都会被退回到暂存区。

git rebase合并多次commit

将多次commit合并,只保留一次提交历史:

# 4次提交的commit倒序排列
git rebase -i HEAD~4
# 取消commit
git rebase --abort 

删除远程分支

git push -d origin branchName

在本地创建一个和远程分支一样的分支

 git checkout -t origin/feature

默认会在本地建立一个和远程分支名字一样的分支,并自动拉取远程分支代码

查看git个人代码量

git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

统计每个人的增删行数

git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

提交次数统计

git log --oneline | wc -l 

git修改远程分支

//删除远程分支
$ git remote rm origin
//添加新的远程分支
$ git remote add origin url

查看远程分支

$ git branch -r

查看远程log信息

$ git log --oneline

打标 tags

我们在执行 git push 的时候,tag是不会上传到服务器的,为了共享这些tag,你必须这样:

git push origin --tags
  • 带有信息的tag
git tag -a v1.0.0 -m 'first version'

-m 后面带的就是注释信息,一般写当前的版本作用

  • 删除远程tag
git push origin :refs/tags/v0.1.2

HEAD

cat .git/HEAD
ref: refs/heads/publish/0.0.4

将add和commit合为一步

git commit -am 'msg'

适用于非第一次commit

显示提交历史对应的文件修改

git whatchanged

恢复某个已修改的文件(撤销未提交的修改):

$ git checkout file-name;//git ct ./src/pages/index/index.js

撤销所有更改

//撤销所有修改
$ git checkout . ;//恢复到上一次commit状态

git拉取远程分支并创建本地分支

git checkout -b local-branchname origin/remote_branchname 

暂存当前修改,将所有置为HEAD状态

$ git stash

会分别对暂存区和工作区的状态进行保存
git stash pop: 应用最新的储藏
git stash save "message..." 暂存时候添加信息
git stash list查看暂存列表
git stash apply stash@{2} 应用指定的储藏

git clone下来指定的单一分支

git clone -b <branch-name> --single-branch https://github.com/user/repo.git

删除.idea文件或某一指定文件

error: The following untracked working tree files would be overwritten by checkout:
.idea/workspace.xml

执行:

$ git rm -f --cached .idea/workspace.xml
$ git rm -rf file-name;//git递归删除指定文件

然后把.idea/workspace.xml添加到gitignore文件

git短命令注册

$ git config --global alias.st status

git pull与本地文件冲突:

$ git stash:暂存修改 ;//备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
$ git pull;// 当前分支自动与唯一一个追踪分支进行合并

// 其他相关命令
git stash pop:  ;// git stash会从最近一次commit提交中读取内容,并暂存当前未commit的内容; pop会从最近的一个stash中读取内容并恢复。

// 如果提示需要merge才能执行stash,可以使用

$ git reset --hard

撤销commit

git revert HEAD -m 'add' ;//撤销前一次 commit
git revert HEAD^ ;//撤销前前一次 commit
git reset --hard HEAD~3:将最近3次的提交回滚

版本回退/撤销merge 只需要添加commit版本号的前几位

git reset--hard 3628164

reset是指向原地或者向前移动指针,git revert是创建一个commit来覆盖当前的commit,指针向后移动

git revert 是撤销某次操作,此次操作之前的commit都会被保留,而git reset 是撤销某次提交,但是此次之后的修改都会被退回到暂存区中。

git显示commit的文件的修改

git show 356f6def9d3fb7f3b9032ff5aa4b9110d4cca87e;

显示如下:


diff --git a/config/index.js b/config/index.js
index c2ebdbc4..b6369449 100755
--- a/config/index.js
+++ b/config/index.js
@@ -155,8 +155,8 @@ function getServerName() {
 
 function getHost() {
   return {
-    development: '127.0.0.1',
-    // development: '192.168.101.223',
+    // development: '127.0.0.1',
+    development: '192.168.101.223',
     // development: '192.168.103.66',
     // development: '192.168.100.99',
     // development: '192.168.101.223',
diff --git a/src/common/pages/recruit/style.scss b/src/common/pages/recruit/style.scss
index b45007a1..b2a5a7ce 100755
--- a/src/common/pages/recruit/style.scss
+++ b/src/common/pages/recruit/style.scss
@@ -64,8 +64,7 @@ body>span>a{

@hawx1993 hawx1993 added the git label Nov 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant