Git基本命令

掌握使用一个分散式版本控制软件

Git流程图

1.初始化仓库

$git init

包含.git文件的文件夹是工作区(Workspace),也就是工作目录。

2.本地仓库添加文件

如果在工作区添加了一个“hello.txt”的文件,我们通过”git add “hello.txt””命令添加到暂存区中。此时还需要commit命令将修改添加到本地仓库中。

$git add "hello.txt"
$ git commit -m "第一次添加了hello.txtx的文件"

其中引号中的内容为日志信息

3.本地仓库删除文件

另在仓库中新建haowan.txt文件,并按照上述方法添加到本地仓库中。下面进行删除操作。

$ git rm haowan.txt
rm 'haowan.txt'

这里是类似Linux系统的删除命令,仅仅是在工作区删除了haowan.txt文件。若要从本地仓库删除文件,还要commit命令。

$ git commit -m "删除了hello.txt文件"
[master 729fd2b] 删除了hello.txt文件
 1 file changed, 1 deletion(-)
 delete mode 100644 hello.txt

4.将Python项目添加到本地版本库中

image-20200123204234619

如将这个项目添加到本地仓库,同理先add到暂存区

$ git add Python_GUI
$ git commit -a -m "添加了Python项目"

-a表示将所有的修改的文件一同commit到本地仓库中。

[master c1e8c12] 添加了Python项目
 21 files changed, 1141 insertions(+)
 create mode 100644 Python_GUI/.idea/encodings.xml
 create mode 100644 Python_GUI/.idea/misc.xml
 create mode 100644 Python_GUI/.idea/modules.xml
 create mode 100644 Python_GUI/.idea/other.xml
 create mode 100644 Python_GUI/.idea/workspace.xml
 create mode 100644 Python_GUI/Python_GUI.iml
 create mode 100644 Python_GUI/window/Window1.py
 create mode 100644 Python_GUI/window/Window2.py
 create mode 100644 Python_GUI/window/__init__.py
 create mode 100644 Python_GUI/window/__pycache__/Window1.cpython-37.pyc
 create mode 100644 Python_GUI/window/__pycache__/Window2.cpython-37.pyc
 create mode 100644 Python_GUI/window/__pycache__/__init__.cpython-37.pyc
 create mode 100644 Python_GUI/window/chestimouse.ui
 create mode 100644 Python_GUI/window/img/background.png
 create mode 100644 Python_GUI/window/img/logo-mini.svg
 create mode 100644 Python_GUI/window/img/logo.svg
 create mode 100644 Python_GUI/window/loginwindow.py
 create mode 100644 Python_GUI/window/main.py
 create mode 100644 Python_GUI/window/pic.qrc
 create mode 100644 Python_GUI/window/quit.png
 create mode 100644 Python_GUI/window/test.py

image-20200123205024038

5.使用.gitignore忽略本地文件

gitignore:在Git工作区的根目录下创建一个特殊的.gitignore文件,可以把要忽略的文件名填进去,Git就会自动忽略这些文件。

.gitignore文件:

#My configurations:
node_modules
*.log
*.jpg
!icon.jpg

语法

注释
*.log *为通配符,所有.log都会被忽略
!icon.jpg !为该文件不被忽略 取反
node_modules文件夹整个都被忽略

测试

$ cd Python_GUI
Administrator@PC20190401w MINGW64 /d/360MoveData/Users/Administrator/Desktop/gittt/Python_GUI (master)
$ touch .gitignore

image-20200123210105778

在文件中添加以下文字

image-20200123210307399

之后add,commit到本地仓库中

远程仓库

1.添加远程库

与远程仓库建立https的链接

$ git remote add origin https://github.com/LehiChiang/NeteaseMusicDownloader.git
$ git push -u origin master

与远程仓库建立SSH的链接

创建公钥

$ ssh-keygen -t rsa

image-20200123232754463

然后在id_rsa.pub文件,复制其中的密钥,在用户中心中,找到设置选项,找到”SSH and GPG keys”,点击“New SSH key”按钮,添加密钥。然后标题随便写一个,在下面粘贴上刚刚复制的密钥,然后保存。截图如下:

image-20200123233446717

最后就可以使用以下命令建立ssh链接,将代码push到远程仓库中了。

$ git remote add origin git@github.com:LehiChiang/NeteaseMusicDownloader.git
$ git push -u origin master

2.推送修改的文件及冲突解决

在本地仓库修改的文件,推送到远程,在git GUI中进行,选择菜单栏中的”remote”中的”push”按钮,push到远程后成功的截图如下:

image-20200124000001891

3.从远程仓库拉取修改

$ git pull -v --progress "origin"

或者

$ git pull origin master

4.克隆远程仓库到本地

$ git clone https://github.com/LehiChiang/NeteaseMusicDownloader.git

分支管理

使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作。

image-20200124003839861

image-20200124003944853

image-20200124004311357

1.在GUI中创建分支

image-20200124004738468

image-20200124004909629

2.分支的合并与删除

image-20200124005815294

image-20200124005835231


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!