-
Notifications
You must be signed in to change notification settings - Fork 0
/
github提交操作
125 lines (105 loc) · 5.07 KB
/
github提交操作
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
注意:安装的前提条件是配置好git的相关环境或者安装好git.exe,此处不再重点提及;
上传的步骤:(本文采用git 命令界面进行操作)
( git config --global user.email "[email protected]"
git config --global user.name "Your Name")实现登陆;
上传项目:
1.将要上传的文件拷贝到这个目录下,拷贝完成后,再执行下命令git status,看git是否获取到文件的更改;cd到你的本地项目根目录下,执行git命令:git init;
2.再执行命令:git add --all(git add .),将所有文件都添加到仓库中,如果想添加某一个文件,则将后面的--all换成你要提交的文件名即可;
3.然后还需要将增加的文件commit到仓库里去,执行命令git commmit -m "注释语句";
4.在Github上创建自己的repository;
5.将本地的代码关联到github上:git remote add origin https://github.com/K2Y2K/hello-word.git;
6.上传github之前,要先pull一下,执行如下命令:git pull origin master;
【git pull --rebase origin master//(不加这句可能报错出现错误的主要原因是github中的README.md文件不在本地代码目录中可以通过该命令进行代码合并】
7.上传代码到github远程仓git push -u origin master;
8.之后输入账号和密码,即可上传到github上。
代码修改:
添加目录或文件:
$ git add /XXX/XX.XX
【$ git add .
#将文件添加到暂缓区,每次修改之后都需要将文件放到暂缓区去
# . 代表当前目录下所有已增加或者已修改的文件
# git add /XXX/XXX/XXX.xx 针对某个特定文件
$ git commit -m “xxx log”
#将文件提交到本地的仓库中
】
$ git commit –m “xxx log”
$ git push origin xxxx
【将本地的操作同步到服务器中
#XXX代表需要提交的分支,可用git branch –a查看所有分支】
删除目录或文件:
$ git rm /XXX/XX.XX
$ git commit –m “xxx log”
$ git push origin xxxx
文件 更名:
$ git mv /XXX/file_from.XX /XXX/file_to.XX
$ git commit –m “xxx log”
$ git push origin xxxx
从github上更新代码:
$ git pull
#本地的仓库(.git隐藏文件夹)更新到与远程仓库一样,并同时更新工作目录(alps)
$ git fetch [remote-name]
#本地的仓库(.git隐藏文件夹)更新到与远程仓库一样,但是不会同步到工作目录(alps)
$ git merge origin/xxx
#同步到工作目录(alps)
【
pull与fetch的区别:
git fetch:相当于是从远程获取最新版本到本地,不会自动merge
git pull:相当于是从远程获取最新版本并merge到本地,相当于git fetch 和 git merge
在实际使用中,git fetch更安全一些。因为在merge前,我们可以查看更新情况,然后再决定是否合并
】
从本地仓库恢复代码:
$ git checkout /xxx/xxx
#从本地仓库恢复指定文件或目录至工程中。也可以用git checkout .来更新所有当前目录下的文件
更新单个文件到最新:
$ git fetch
$ git checkout origin/xxx -- path/to/file
#从远程服务器更新到本地仓库,在工作目录下只更新单个file文件到最新
更新单个文件到commit_id:
$ git fetch
$ git checkout commit_id path/to/file
#从远程服务器更新到本地仓库,在工作目录下只更新单个file文件到commit_id
恢复错误提交:
$ git reset –soft ID
#相当于将上几次commit进行回退至暂存区,工作目录不会变动
$ git reset –hard ID
#相当于将上几次commit add(rm mv ..) 以及工作目录的改动都回退
$ git diff --cached
#可以查看当前工作环境所修改的地方(工作环境相对于本地仓库的修改,即通过git add已经将修改添加进“暂存区域”,但并没有运行git commit提交到本地仓库的修改部分)
$ git status
#可以用来查看当前的工程文件状态
git diff 和git status的区别是git diff 会列出文件具体修改的地方,git status是列出文件的修改状态。
查看log:
$ git log
#可以查看简单的提交log
$ git log -u
#查看详细的提交log
$ git log –author=xxxname
#查看某个人的提交log
$ git log \xxx
#查看某个目录或文件相关的修改log
在查看log等git操作后,想要退出git查看界面,先按“Esc”,再按“F2”;
$ git show COMMIT_ID
#可以查看某一次提交
$ git log --stat
查看提交历史,并显示统计信息
查看提交历史(全部):
$ git log
查看提交历史,并显示统计信息:
$ git log --stat
查看提交历史并查看差异:
$ git log -p
查看最近2次提交历史并查看差异:
$ git log -p -2
查看最近2周内提交历史:
$ git log --since=2.weeks
git log --since="2008-09-14"
查看某个时刻以前的提交历史:
$ git log --until="2008-09-14"
查看某个作者的提交历史: $ git log --author="stupid"
其他:
-(n) 仅显示最近的 n 条提交
--since, --after 仅显示指定时间之后的提交。
--until, --before 仅显示指定时间之前的提交。
--author 仅显示指定作者相关的提交。
--committer 仅显示指定提交者相关的提交。
查看某个时刻之后的提交历史: ;