git 常用命令

本贴最后更新于 2655 天前,其中的信息可能已经时移俗易
title: git
date: 2016-11-09 15:13:48
tags: [工具使用,git]
categories: 工具使用
keywords: 工具使用,git
description: 记录git的常用命令。

git push origin master 报错

fatal: could not read Username for 'https://github.com': No such file or directo
原因使用 https 方式的时候 在 git remote add origin 的 https url 里面没有用户名和密码
修改为如下:
git remote add origin https://{username}:{password}@github.com/{username}/project.git
Issue #176 · kemayo/sublime-text-git
或者直接修改 .git/config 隐藏文件 为 git remote add origin https://{username}:{password}@github.com/{username}/project.git 格式

显示当前分支

git branch

显示远程信息

git remote -v

强制回退到 master 分支

取回远程主机某个分支的更新,再与本地的指定分支合并
git pull < 远程主机名 > < 远程分支名 >:< 本地分支名 >
git pull origin master:master -f

显示 git 状态

git status

git branch * master

将 master 分支合并到当前分支

git merge master

将远程 master 的修改合并到本地 mbl 分支。

git pull origin master:master -f
git add .
git commit -m "test"
git pull origin master:master -f
git branch * master
git checkout mbl
git pull
git merge master
git push

取消 commit 和 pull

git reset --hard <commit_id>
git push origin HEAD --force

git 更换仓库

  1. 从原地址克隆一份裸版本库,比如原本托管于 GitHub
    git clone --bare
  2. 然后到新的 Git 服务器上创建一个新项目,比如 GitCafe。
  3. 以镜像推送的方式上传代码到 GitCafe 服务器上。
cd project.git
git push --mirror git@gitcafe.com/username/newproject.git
  1. 先查看 remote 的名字
git branch -r
  1. 假设你的 remote 是 origin,用 git remote set_url 更换地址
git remote set-url origin remote_git_address

提交代码

git commit -am "提交说明"

追踪远端更新

  • 为了将远程的 Alluxio 源码改动更新到你本地的副本中,你需要创建一个指向远程 Alluxio 源代码库的源。在刚拷贝的副本的目录下,运行:
git remote add upstream https://github.com/Alluxio/alluxio.git
  • 显示所有分之,并比较本地和远端分之版本
git branch -av
  • 把远端的分支抓取过来
git fetch
  • 切换到分支
git checkout --track remotes/upstream/branch-1.4
  • revert file,such as pom.xml
git checkout -- pom.xml
git push origin branch-1.4
  • merge master branch
git checkout master
git merge upstream/master
git branch -av
git status 
git push
  • create a sub branch of an assigned branch
#切换到分支
git checkout branch1

#基于该分支创建子分支
git checkout -b branch2_based_on_b1 branch1 

  • 为推送当前分支并建立与远程上游的跟踪
git push --set-upstream origin mbl-baseon-JDAlluxio-1.3.0

# checkout时和提交时都用已有的换行符,不做替换
$ git config --global core.autocrlf false
# checkout时不转换,提交的时候自动转换为LF
$ git config --global core.autocrlf input
  • Git

    Git 是 Linux Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

    205 引用 • 357 回帖 • 1 关注
  • scm
    1 引用 • 2 回帖

相关帖子

2 回帖

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • 382309806

    最近在用 git 好贴 顶一个 :+1|type_1_2:

  • 不错,但我最常用的就是 pull、push、commit、status。其他都是 GitHub Desktop 上面操作。。。