Luckylau's Blog

Git常用命令

1.添加.gitignore并生效

vim .gitignore

1
2
3
4
*.project
.settings/
.classpath
.gitignore

git rm -r --cached .

git add .

git commit -m 'update .gitignore'

2.回滚某个版本

git reset --hard commit_id

git push origin -- force

3.Github上fork项目后与原项目保持同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//首先下载自己fork的项目,我叫luckylau
$ git clone https://github.com/Luckylau/PiggyMetrics.git
//查看远端分支
¥cd PiggyMetrics/
$ git remote -v
origin https://github.com/Luckylau/PiggyMetrics.git (fetch)
origin https://github.com/Luckylau/PiggyMetrics.git (push)
//加入新的远端分支 名字随便取,我取作者的名字sgshq
$ git remote add sqshq https://github.com/sqshq/PiggyMetrics.git
//再次查看远端分支
$ git remote -v
origin https://github.com/Luckylau/PiggyMetrics.git (fetch)
origin https://github.com/Luckylau/PiggyMetrics.git (push)
sqshq https://github.com/sqshq/PiggyMetrics.git (fetch)
sqshq https://github.com/sqshq/PiggyMetrics.git (push)
==============================================================
开始保持同步的操作
//把原项目更新的内容fetch到本地
$ git fetch sqshq
From https://github.com/sqshq/PiggyMetrics
* [new branch] master -> sqshq/master
//查看分支
$ git branch -av
* master 914ed47 Merge pull request #17 from ddubson/master
remotes/origin/HEAD -> origin/master
remotes/origin/master 914ed47 Merge pull request #17 from ddubson/master
remotes/sqshq/master 914ed47 Merge pull request #17 from ddubson/master
//合并
$ git checkout master
Already on 'master'
Your branch is up-to-date with 'origin/master'.
$ git merge sqshq/master
Already up-to-date.
//推到github
git push -u origin master
Luckylau wechat
如果对您有价值,看官可以打赏的!