git操作的命令和技巧

git的一些基本操作,很多时候自己遇到一些比较棘手的状况也会到处搜索,权当是为自己记录一下,以方便日后查阅。

基本操作

git clone 

git pull

git fetch

git status

git log

查看遠程URL

git remote -v

修改遠程URL

Change your remote's URL from SSH to HTTPS with the git remote set-url command.

$ git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

git branch -a  branchName

以当前分支创建新分支

git checkout -b branchName

以远程分支创建新分支

git checkout -b  branch   <remote branch>

git push --set-upstream origin branch

 

覆盖本地

用remote版本覆盖本地

git reset --hard origin/master

or

git reset --hard commitId

git checkout comitId  filename/path

 

git tag的相关操作,创建,删除,推送

git tag -l

git tag -d name

git tag -a name -m ""

push本地tag到远程

git push origin --tags

 

删除本地缓存,重新提交

对于已经提交过git的项目,当我们创建了.gitignore文件后,发现它并不能自动按照我们的规则排出,这个时候就需要先删除本地缓存,再重新提交。

这个命令也可以用在其他想要重新提交的时候。

git rm -r --cached .

git add .

git commit -m 'update .gitignore'

 

stash

https://www.cnblogs.com/tocy/p/git-stash-reference.html

隐藏更改:git stash message

恢复更改:git stash pop

删除:git stash drop {version}

git stash list

查看diff的差异:git stash show

 

set global config

往往应为开发在内网环境下做的,当此时需要访问外部网络就只能通过代理

set proxy:

git config --global http.proxy proxy-sen.test.com:10080
git config --global https.proxy  proxy-sen.test.com:10080
set user:
git config --global user.email xxx@xxx.com
git config --global user.username rand

合并提交记录
git rebase -i HEAD~4

pick

suqure

git push -u origin feature-1

拉取远程服务器的分支与本地做合并

合并自己的分支

git pull --rebase

合并其他远程分支 

git pull --rebase origin master

Rebase到第一个提交的版本

The easy way, with a recent-enough git (this has been out for a long time now so you should have this):

git rebase -i --root

初始化本地既存项目到git远程仓库

1.在远程仓库创建一个git项目

2.cd 到本地项目目录初始化,git init

3.添加本地项目文件到git,git add   .

4.提交本地修改,git commit -m "commit message" 

5.添加远程仓库地址,git remote add origin  https://xxx.git

6.向远程仓库推送,

git push -u origin feature-1
git push -f origin feature-1

 

修改已提交的内容,提交履历

git commit --amend

  如果我们不小心提交了一版我们不满意的代码,并且给它推送到服务器了,在代码没被merge之前我们希望再修改一版满意的,而如果我们不想在服务器上abondon,那么我们怎么做呢?

         git commit --amend //也叫追加提交,它可以在不增加一个新的commit-id的情况下将新修改的代码追加到前一次的commit-id中,

What is the difference between author and committer in Git?

https://stackoverflow.com/questions/6755824/what-is-the-difference-between-author-and-committer-in-git

有时会发现git的Author和Committer不是同一个人,一般原因是提交Commit时的用户和gitconfig指定的用户不一致

可以通过设置项目单独的用户使提交者和创建者相同

git multiple user names for the different projects within the same system [duplicate]

https://stackoverflow.com/questions/9063176/git-multiple-user-names-for-the-different-projects-within-the-same-system

git可以设置全局的用户,邮箱也可以为各一个项目设置当都的用户,邮箱

git config --global user.email personal@example.org
git config --global user.name "whatf hobbyist"
git config user.email personal@example.org
git config user.name "whatf hobbyist"

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值