简单的记录一下git常用命令

Git 是一个开源的版本控制系统,用于记录代码的历史版本和变更。它可以方便地管理代码,并与团队成员协作,以便随时跟进变更并避免出现代码冲突。

以下是一些常用的 Git 命令:

1. git init:初始化一个新的 Git 仓库
2. git status:查看当前工作区、暂存区和版本库中的文件状态
3. git add:将工作区中的文件添加到暂存区
4. git commit:将暂存区中的文件提交到版本库
5. git push:将本地版本库的提交推送到远程仓库
6. git pull:将远程仓库的提交拉取到本地版本库
7. git clone:从远程仓库获取一个 Git 仓库的副本

1. `git init`

执行该命令后,Git 会在当前目录中初始化一个新的 Git 仓库。执行结果如下:

Initialized empty Git repository in /path/to/your/folder/.git/


 

2. 创建一个新文件 hello.txt,并进行编辑

$ touch hello.txt
$ echo "Hello, World!" > hello.txt


 

3. `git add hello.txt`

执行该命令后,Git 会将 hello.txt 文件添加到暂存区(Index)。执行结果如下:

$ git add hello.txt
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   hello.txt


现在 hello.txt 文件已经准备好被提交到版本库了。

4. `git commit -m "Add hello.txt"`

执行该命令后,Git 会将暂存区中的 hello.txt 文件提交到版本库,并为该次提交添加一个提交说明。执行结果如下:

$ git commit -m "Add hello.txt"
[master (root-commit) 03cc4c6] Add hello.txt
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt


 

现在版本库中有了一个新的提交,提交说明为“Add hello.txt”。

5. `git push origin master`

执行该命令后,Git 会将本地版本库的提交推送到远程仓库。执行结果如下:

$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 222 bytes | 111.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/username/repo.git
 * [new branch]      master -> master


 

以上命令将本地的 master 分支推送到名为 origin 的 GitHub 仓库上。

6. `git pull origin master`

执行该命令后,Git 会从远程仓库拉取一份最新的代码。执行结果如下:

$ git pull origin master
From https://github.com/username/repo.git
 * branch            master     -> FETCH_HEAD
Already up to date.


 

如果没有合并冲突,则可以获得最新的代码。

你可能感兴趣的:(git,github)