【Git】Git初始化一个仓库


简单记录-慕课网 从0开始 独立完成企业级Java电商网站开发

Git初始化一个仓库

git初始化

https://github.com

在这里插入图片描述

在这里插入图片描述

“.gitignore”文件,这个文件的作用就是告诉Git哪些文件不需要添加到版本管理中。

.gitignore

配置

*.class

#package file

*.war
*.ear

#kdiff3 ignore
*.orig

#maven ignore
target/

#eclipse ignore
.settings/
.project
.classpatch

#idea
.idea/
/idea/
*.ipr
*.iml
*.iws

# temp file

*.log
*.cache
*.diff
*.patch
*.tmp

# system ignore
.DS_Store
Thumbs.db



在这里插入图片描述

初始化仓库

通过git init 初始化一个git仓库

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall
$ git init
Initialized empty Git repository in D:/IdeaProjects/imooc/mmall_learning/mmall/.git/


git init该命令将创建一个名为 .git 的子目录,这个子目录含有初始化的 Git 仓库中所有的必须文件,这些文件是 Git 仓库的骨干。git init仅仅是做了一个初始化的操作,项目里的文件还没有被跟踪。

在这里插入图片描述
在这里插入图片描述

检查当前文件状态

可以用 git status 命令查看哪些文件处于什么状态。

通过git status命令发现有几个未被git追踪 的文件

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        pom.xml
        src/

nothing added to commit but untracked files present (use "git add" to track)

未跟踪的文件意味着 Git 在之前的快照(提交)中没有这些文件.

跟踪新文件

使用命令 git add 开始跟踪一个文件。 要跟踪 这里的所有 文件,运行git add.

使用git add.命令将这这几个文件添加到暂存区

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git add .
warning: LF will be replaced by CRLF in src/main/webapp/WEB-INF/web.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/main/webapp/index.jsp.
The file will have its original line endings in your working directory

此时再运行 git status 命令,会看到这几个 文件已被跟踪,并处于暂存状态,发现有几个文件可以被commit。

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitignore
        new file:   pom.xml
        new file:   src/main/webapp/WEB-INF/web.xml
        new file:   src/main/webapp/index.jsp


只要在 Changes to be committed 这行下面的,就说明是已暂存状态。 如果此时提交,那么该文件在运行 git add 时的版本将被留存在历史记录中。

git status 命令的输出十分详细,但其用语有些繁琐。 Git 有一个选项可以帮你缩短状态命令的输出,这样可以以简洁的方式查看更改。 如果你使用 git status -s 命令或 git status --short 命令,将得到一种格式更为紧凑的输出。

提交更新

现在的暂存区域已经准备妥当可以提交了。 在此之前,请一定要确认还有什么修改过的或新建的文件还没有 git add 过,否则提交的时候不会记录这些还没暂存起来的变化。 这些修改过但没有暂存的文件只保留在本地磁盘。 所以,每次准备提交前,先用 git status 看下,所需要的文件是不是都已暂存起来了, 然后再运行提交命令 git commit

$ git commit

这种方式会启动文本编辑器以便输入本次提交的说明。

也可以在 commit 命令后添加 -m 选项,将提交信息与命令放在同一行,如下所示:

$ git commit -m ""

记住:提交时记录的是放在暂存区域的快照。 任何还未暂存文件的仍然保持已修改状态,可以在下次提交时纳入版本管理。 每一次运行提交操作,都是对你项目作一次快照,以后可以回到这个状态,或者进行比较。

跳过使用暂存区域

尽管使用暂存区域的方式可以精心准备要提交的细节,但有时候这么做略显繁琐。 Git 提供了一个跳过使用暂存区域的方式, 只要在提交的时候,给 git commit 加上 -a 选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤:

$ git commit -a -m 'added new benchmarks'

git commit进行第一次commit

git commit -am ‘后面直接加注释’

git commit -am 'first commit init mall_project'
x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git commit -am 'first commit init mall_project'
[master (root-commit) 255b57f] first commit init mall_project
 4 files changed, 117 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 pom.xml
 create mode 100644 src/main/webapp/WEB-INF/web.xml
 create mode 100644 src/main/webapp/index.jsp

在这里插入图片描述

移除文件

要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除(确切地说,是从暂存区域移除),然后提交。 可以用 git rm 命令完成此项工作,并连带从工作目录中删除指定的文件,这样以后就不会出现在未跟踪文件清单中了。

git status命令用于显示工作目录和暂存区的状态。使用此命令能看到那些修改被暂存到了, 哪些没有, 哪些文件没有被Git tracked到。git status不显示已经commit到项目历史中去的信息。看项目历史的信息要使用git log.

添加远程仓库

运行 git remote add 添加一个新的远程 Git 仓库

在这里插入图片描述

git remote add origin SSH的地址

git remote add origin [email protected]:liuawen/mmall_learning.git

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git remote add origin  [email protected]:liuawen/mmall_learning.git

推送到远程仓库

git push [remote-name] [branch-name]。 当你想要将 master 分支推送到 origin 服务器时(再次说明,克隆时通常会自动帮你设置好那两个名字),那么运行这个命令就可以将你所做的备份到服务器:

$ git push origin master

只有当你有所克隆服务器的写入权限,并且之前没有人推送过时,这条命令才能生效。 当你和其他人在同一时间克隆,他们先推送到上游然后你再推送到上游,你的推送就会毫无疑问地被拒绝。 你必须先将他们的工作拉取下来并将其合并进你的工作后才能推送。 阅读 Git 分支 了解如何推送到远程仓库服务器的详细信息。

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
To github.com:liuawen/mmall_learning.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:liuawen/mmall_learning.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

在这里插入图片描述

git pull的作用是从一个仓库或者本地的分支拉取并且整合代码。git pull

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git pull
warning: no common commits
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
From github.com:liuawen/mmall_learning
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master


x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)

git push -u origin master

强制覆盖git push -u -f origin master

$ git push -u -f origin master
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (10/10), 1.54 KiB | 225.00 KiB/s, done.
Total 10 (delta 0), reused 0 (delta 0)
To github.com:liuawen/mmall_learning.git
 + 6e19c70...255b57f master -> master (forced update)
Branch 'master' set up to track remote branch 'master' from 'origin'.




在这里插入图片描述

查看本地分支git branch

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git branch -r
  origin/master


查看远程分支git branch -r

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git branch -r
  origin/master


创建并切换分支

git checkout -b v1.0 origin/master

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (master)
$ git checkout -b v1.0 origin/master
Switched to a new branch 'v1.0'
Branch 'v1.0' set up to track remote branch 'master' from 'origin'.

分支开发 主干发布

git push origin HEAD -u

x1c@DESKTOP-HVI7SH0 MINGW64 /d/IdeaProjects/imooc/mmall_learning/mmall (v1.0)
$ git push origin HEAD -u
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'v1.0' on GitHub by visiting:
remote:      https://github.com/liuawen/mmall_learning/pull/new/v1.0
remote:
To github.com:liuawen/mmall_learning.git
 * [new branch]      HEAD -> v1.0
Branch 'v1.0' set up to track remote branch 'v1.0' from 'origin'.

在这里插入图片描述

在这里插入图片描述

发布了78 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41569732/article/details/104249239