git使用记录

最近在写一些代码的时候,发现在公司和住的地方,同步很不方便,老是用U盘很麻烦,于是就想使用下git,于是开始看看git怎么用,遇到的一些问题,记录下
1.安装git,百度一下就可以了
2.配置下用户信息

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

3.初始化仓库,在某个目录下,初始化之后就可以用git管理目录下的文件了

$ git init
Initialized empty Git repository in f:/GitTest/.git/

4.新加文件并提交到版本库

$ echo "hahahah" > test.txt

$ git add test.txt

$ git commit -a -m "1"

5.删除文件

$ git rm test.txt
rm 'test.txt'

$ git commit -a -m "2 rm"
[master 47e19d1] 2 rm
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

6.远程服务器,丢弃本地所有修改,完全和远程一样(本地和远程的不一样导致的错误,例如下面,这个本地的版本库我在添加远程之前,是有做过一些操作的,然后把远程pull到本地的时候,就报错了,如果本地的东西可以放弃掉不要的话,可以使用下面的策略)

$ git remote add origin git@github.com:scchary/Test1.git

$ git pull origin
warning: no common commits
remote: Counting objects: 81, done.
remote: Compressing objects: 100% (44/44), done.
remote: Total 81 (delta 7), reused 79 (delta 5), pack-reused 0
Unpacking objects: 100% (81/81), done.
From github.com:scchary/Test1
 * [new branch]      master     -> origin/master
You asked to pull from the remote 'origin', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

$ git fetch origin

$ git reset --hard origin/master

7.本地版本回溯之后再push到远程的时候报错,

$ echo "test" >> t3.txt

$ git add t3.txt

$ git commit -a -m "test add file"
[master 4428651] test add file
warning: LF will be replaced by CRLF in t3.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 t3.txt

$ git push origin master   #新创建了一个文件,并提交push到远程,这是远程就是在新的版本库了
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:scchary/Test1.git
   b3677f6..4428651  master -> master

$ git reflog
4428651 HEAD@{0}: commit: test add file
b3677f6 HEAD@{1}: commit: test
2620363 HEAD@{2}: reset: moving to origin/master
47e19d1 HEAD@{3}: commit: 2 rm
821154b HEAD@{4}: commit (initial): 1

$ git reset --hard b3677f6 #版本回溯
HEAD is now at b3677f6 test

$ ls
t1.txt  t2.txt  test.txt  testA.txt  #添加的t3文件确实不见了


$ git push origin  master   #push到远程的时候报错,因为现在本地比远程的版本旧,所以要先pull
To git@github.com:scchary/Test1.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:scchary/Test1.git'
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.

#如果是想把远程上面的也回溯到现在这个版本,可以直接用下面的命令
$ git push -f origin master
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:scchary/Test1.git
 + 4428651...b3677f6 master -> master (forced update)

8.忽略掉已经添加版本库的文件,git可以在.gitignore文件里面设置忽略的文件,比如

$ cat .gitignore
test.txt

上面就把test.txt文件夹忽略掉了,之后这个文件的更改git是不会管的,但是这个如果在设置.gitignore之前,test.txt就已经加入到了git,那么这个设置是无效的,可以利用这个命令来设置,

jin@JIN /f/GitTest (master)
$ git rm --cached test.txt

标签: none

添加新评论