Git

139 阅读3分钟

git

  1. 打开命令行工具,切换到需要上传的工程目录下。
  2. 初始化当前目录作为Git仓库。 git init
  3. 添加目录下的文件到本地仓库。 git add . 添加当前目录下的所有文件 如果需要unstage某个文件,使用git reset HEAD <file>命令
  4. 提交staged的文件 git commit -m "initial commit"
  5. 从GitHub上拷贝之前新建的仓库地址。
  6. 使用命令行添加远程仓库的地址。 git remote add origin url
  7. git remote -v 查看远程仓库地址
  8. Push本地仓库到远程GitHub。 git push -u origin master
  9. 如果提示冲突可以强制push:git push -u origin master -f
  • 从已有的分支创建新的分支(如从master分支),创建一个test分支
    git branch test 创建新分支
    git checkout test 切换到新分支
    上面命令等用于git checkout -b test\

  • 创建完可以查看一下,分支已经切换到test
    git branch *表示在当前分支\

  • 提交该分支到远程仓库
    git push origin test\

  • 从远程获取test
    git pull origin test\

  • 设置git push,pull默认的提交获取分支,这样就很方便的使用git push 提交信息或git pull获取信息
    git branch --set-upstream-to=origin/test
    取消对master的跟踪
    git branch --unset-upstream master\

  • 随便修改一下工程文件的内容,然后git commit ,git push,之后就可以直接提交到远程的test分支中,而不会是master,若想设置回默认提交获取master,切换master后,重复操作上一步即可\

在分支test上开发完成后,需要合并到主干master

  • 从当前分支切换到主干master上
    git checkout master

  • 合并某个分支到主干master
    git merge test

  • 上传代码
    git push origin
    报出如下:
    fatal: The current branch master has no upstream branch.
    To push the current branch and set the remote as upstream, use
    git push --set-upstream origin master
    参考:blog.csdn.net/benben_... 解决

  • 再次上传
    git push --set-upstream origin master
    报错如下:
    ! [rejected] master -> master (non-fast-forward)
    error: failed to push some refs to 'gitee.com/tahara/blue…'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    出现这个问题是因为gitee中的一些文件不在本地代码目录中,可以通过如下命令进行代码合并

    git pull --rebase origin master\

  • 合并后删除本地分支信息
    git branch -d test
    若报出如下错误:
    error: The branch 'test' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D test'.
    使用git branch -D test删除\

  • 删除远程分支
    git push origin --delete test\

遇到的问题
1)master合并分支时提示“Already up-to-date”,但当前分支和 master 分支代码不同步。\

解决方法:
git checkout master
git reset --hard dev\

git push --force origin master\

注意:在不考虑配置文件等测试、生产不同的文件,可以从dev推到master,但是个人分支推送dev分支千万不要用,不然会把其他人的冲掉

参考文章:git创建分支提交远程分支,将分支branch合并到主干master - SegmentFault 思否