Git使用基础

一、如何使用命令将本地项目上传到git

  1. 进入项目文件夹,通过 git init 命令把这个项目变成git可以管理的仓库
  2. 把文件点加到版本库中,使用 git add .添加到暂存区去
  3. 用命令 git commit -m '说明' 把文件提交到仓库
  4. 关联到远程库
    git remote add origin 你的远程库的地址
    
  5. 获取远程库与本地同步(如果远程库不为空必须做这一步)
    git pull --rebase origin master
    
  6. 把本地库的内容推送到远程,实际上是把当前分支master推送到远程。执行次命令后会要求输入用户名和密码。
    git push -u origin master
    

      

二、将服务器上的仓库克隆都本地,进行修改然后上传

  1. 通过clone命令将远程仓库克隆下来 git clone username@ip:/path/yourcode ,然后输入对应username用户的密码即可克隆远程仓库,进行代码修改。
  2. 修改完代码,便可提交:
    git add 'filename'
    git commit -m '修改说明'
    git push

    如果你没有配置免密,push后需要输入对应用户名的密码。

  3. 在服务器端可以通过  git log 查看提交记录。
  4. git reset --hard 1c38838ad39b396e271cb10e2146a1d673b4a2b9 撤回制定版本号(不包括该提交)之前的所有提交。省略版本号则会使得最近提交的版本生效。
  5. 有时候服务器可能会禁止push操作,显示错误为:
    remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
    remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into

    这是由于git默认拒绝了push操作,需要进行设置,修改.git/config添加如下代码:

    [receive]
        denyCurrentBranch = ignore

三、git本地创建.gitignore不起作用

.gitignore只能忽略那些原来没有被 track 的文件,如果某些文件已经被纳入了版本管理中,则修改 .gitignore 是无效的。 解决方法是先把本地缓存删除,然后再提交

git rm -r --cached .
git add .
git commit -m 'We really don't want Git to track this anymore!'

如果缓存中有重要的数据更改,那么你需要单独删除不需要的缓存:git rm --cached logs/xx.log

原文地址:https://www.cnblogs.com/jxc321/p/7337373.html