Git基本使用

3289 2019-04-22 Git

Git Bash 是 Git for Windows 中的一个命令行工具,它提供了类似 Unix/Linux 的命令行环境,用于执行 Git 命令和其他 Shell 命令。在 Windows 系统上,Git Bash 是与 Git 配合使用的常见工具,允许你执行 Git 操作以及 Unix 风格的命令。

配置

# 项目配置
    git config user.name 'your_name'
    git config user.email 'your_name@xx.com'

# 全局配置
    git config --global user.name 'your_name'
    git config --global user.email 'your_name@xx.com'

# 系统配置(注意:需要有root权限)
    git config --system user.name 'your_name'
    git config --system user.email 'your_name@xx.com'
            

使用

# 克隆远程仓库到本地。
    git clone http://example.com/repo.git
# 关联远程仓库
    git remote add origin https://git.example.com/your-repo.git
# 拉取最新代码
    git pull                            # 根据当前分支的默认远程分支进行拉取。
    git pull origin master              # 拉取master分支的代码。
# 初始化, 让git帮助我们管理当前文件夹
    git init
# 检测当前目录下文件状态
    git status
# 将文件添加到暂存区,准备提交。
    git add readme.txt                  # 添加指定文件
    git add .                           # 将所有修改过的文件添加到暂存区
#提交暂存区的更改到本地仓库。
    git commit -m '描述信息'
# 向远程推送代码
    git push                            # 推送到远程仓库的同名分支
    git push origin master              # 推送到远程 origin 仓库的 master 分支
# 查看提交历史记录。
git log
            

在你刚加入新公司并得到一个仓库地址后,假设你需要将本地新建的项目上传到远程仓库(即使用 Git),你可以按照以下步骤操作:

# 1.配置用户信息
    git config user.name "your_name"
# 2.初始化本地仓库
    git init
# 3.关联远程仓库
    git remote add origin https://git.example.com/your-repo.git
# 4.继续开发

# 5.提交代码
    git add .
    git commit -m '描述信息'
    git push origin dev
            

如果公司给了你一个仓库地址和用户名密码,你可以按照以下步骤来使用 Git 来访问和操作代码仓库:

# 1.配置 Git 用户信息
    git config user.name "your_name"
    git config user.email "your_email@example.com"

# 2.克隆仓库
    git clone http://example.com/repo.git
    # 由于公司给了你用户名和密码,你在克隆仓库时,Git 会提示你输入用户名和密码。

# 3.查看状态
    git status

# 4.拉取最新代码
    git pull

# 5.提交代码
    git add .                           # 将所有修改过的文件添加到暂存区
    git commit -m "Commit message"      # 提交暂存区的更改到本地仓库。
    git push origin master              # 推送代码

# 如果你每次推送或拉取时都需要输入用户名和密码,可以使用以下方法来避免频繁输入密码:
    git config credential.helper cache