Getting started with git learning~~~

Create a repository (also known as repository, repository), which can be understood as a repository. All files in this directory can be managed by Git. The modification and deletion of each file can be tracked by Git, so that history can be tracked at any time. , or can be "reverted" at some point in the future.


Creating a repository is very simple. First, choose a suitable location and create an empty directory:

 
 
1.
$ mkdir learngit
$ cd learngit
$ pwd

2.
To initialize a Git repository, use the git init command.

3.
Add files to the Git repository in two steps:

The first step, use the command git add <file>, note that it can be used repeatedly to add multiple files; (add different files multiple times)

The second step, use the command git commit to complete. (commit can submit many files at once)

4.
To keep track of the status of your workspace, use the git status command.

If git status tells you that a file has been modified, use git diff to see the modification.
5. Before the shuttle, use git log to view the commit history to determine which version to roll back to.
  To go back to the future, use git reflog to view the command history to determine which version of the future to go back to.
git reset --hard commit_id

git reset --hard head^ (go back to the previous version)

git reset --hard version number (go back to the future or past)

6. Just the pointer is changing:


7. Repository management


8.  Why Git is better designed than other version control systems, because Git tracks and manages changes, not files .

9.  Scenario 1: When you mess up the contents of a file in the workspace and want to discard the modifications in the workspace (before add) , use the command git checkout -- file(--前后各有一个空格) . git checkout --readme.txt

Scenario 2: When you not only messed up the contents of a file in the workspace, but also added it to the temporary storage area, you want to discard the modification. It is divided into two steps. The first step is to use the command git reset HEAD file, and then you return to the scene 1, and the second step is to press the scene. 1 operation. git reset head readme.txt git checkout -- readme.txt

Scenario 3: When inappropriate changes have been submitted to the repository, if you want to revoke this submission, please refer to the version rollback section, but the premise is that it has not been pushed to the remote repository.

git reset --hard version number

10. 

    vim test.txt (exit mode: i (for insert), esc is ready to exit. ZZ, save and exit;: q! (exit without saving)

     git add test.txt    git commit -m"add a new file"

    To delete the file from the repository: git rm test.txt, git commit -m "delete the test.txt"

command git rmis used to delete a file. If a file has already been committed to the repository, then you never have to worry about deleting it by mistake, but be careful, you can only restore the file to the latest version, you will lose your changes since the last commit .

11. Remote warehouse:

To associate a remote repository, use the command git remote add origin git@server-name:path/repo-name.git; (git remote add origin ....)

After the association, use the command git push -u origin master to push all the contents of the master branch for the first time; (git push -u origin master)

Thereafter, after each local commit, whenever necessary, you can use the command git push origin master to push the latest changes; (git push origin master)
One of the biggest advantages of a distributed version system is that working locally does not need to consider the existence of remote libraries, that is, it can work normally with or without networking, and SVN refuses to work without networking! When there is a network, the synchronization is completed by pushing the local commit again, which is really convenient!


12. Clone the remote repository:

To clone a repository, you must first know the address of the repository and then use the git clonecommand to clone.

Git supports a variety of protocols including https, but is the fastest via sshthe native protocols supportedgit

13. branch management;

What is the use of branches in practice? Suppose you are going to develop a new feature, but it takes two weeks to complete. You wrote 50% of the code in the first week. If you submit it immediately, the incomplete code base will cause others to be unable to work because the code has not been written yet. If you wait for the code to be fully written and then submit it again, there is a huge risk of losing your daily progress.

Now that you have branches, you don't have to be afraid. You created a branch that belongs to you, others can't see it, and you continue to work normally on the original branch, while you work on your own branch, submit it if you want to, and then merge it at one time after the development is completed. On the original branch, in this way, it is both safe and does not affect the work of others.

14. Create the merge branch:

Git encourages heavy use of branches:

Check out the branch:git branch(git branch)

Create a branch:git branch <name>(git checkout -b dev)

Switch branches:git checkout <name>

Create + switch branches:git checkout -b <name>

Merge a branch into the current branch:git merge <name>(git merge dev)

Delete branch:git branch -d <name>(git branch -d dev)


15. To resolve merge conflicts:

When Git can't automatically merge branches, it has to resolve conflicts first. After the conflict is resolved, commit again, and the merge is complete.

Use the git log --graphcommand to see the branch merge diagram.


16. Merge branches without using fast forword:


17. Bug branch management:

When fixing a bug, we can fix it by creating a new bug branch, then merging, and finally deleting; git checkout -b dev; git merge dev;

git checkout master;git branch -d dev

When the work at hand is not completed, visit the work site first git stash, then fix the bug, and after the fix, go git stash popback to the work site

git stash ; git stash list; git stash pop;git stash list.

18. feature分支:

开发一个新feature,最好新建一个分支;git checkout -b dev; git add test.txt;git commit -m"add a test.txt";git checkout master;git merge dev之前

如果要丢弃一个没有被合并过的分支,可以通过git branch -D <name>强行删除。git branch -D dev(强行删除)


19. 多人协作:

多人协作的工作模式通常是这样:

  1. 首先,可以试图用git push origin branch-name推送自己的修改;git push origin dev

  2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;(如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name)git branch --set-upstream dev origin/dev;git pull

  3. 如果合并有冲突,则解决冲突,并在本地提交;

  4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!git push origin dev

这就是多人协作的工作模式,一旦熟悉了,就非常简单。

总结:

  • 查看远程库信息,使用git remote -v

  • 本地新建的分支如果不推送到远程,对其他人就是不可见的;

  • 从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;

  • 在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name本地和远程分支的名称最好一致;

  • 建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name

  • 从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。

20. 创建标签
  • 命令git tag <name>用于新建一个标签,默认为HEAD,也可以指定一个commit id;git tag v1.0 fedsf1

  • git tag -a <tagname> -m "blablabla..."可以指定标签信息;git tag -a v0.9 -m "add a new tag"

  • git tag -s <tagname> -m "blablabla..."可以用PGP签名标签;

  • 命令git tag可以查看所有标签。


21. 操作标签

  • 命令git push origin <tagname>可以推送一个本地标签;git push origin v1.0

  • 命令git push origin --tags可以推送全部未推送过的本地标签;git push origin --tags

  • 命令git tag -d <tagname>可以删除一个本地标签; git tag -d v1.0

  • 命令git push origin :refs/tags/<tagname>可以删除一个远程标签git push origin :refs/tags/v1.0

22. 使用github

  • 在GitHub上,可以任意Fork开源仓库;fork之后使用:git clone [email protected]:linhj-james/learngit-1.git

    [email protected]:linhj-james/learngit-1.git
    [email protected]:linhj-james/learngit-1.git
  • 自己拥有Fork后的仓库的读写权限; pull request

  • 可以推送pull request给官方仓库来贡献代码。

23. 最后,供上版本管理常用命令:


git clone [email protected]:linhj-james/learngit.git(你的远程地址)

git init (初始化一个新建的仓库)


git status (所在文件夹的文件状态)

git diff(显示两次文件的修改的不同之处)

git add (+filename)

git commit -m"add some chages"


git log(显示记录)


git branch (查看分支)

git checkout -b dev(新建并切换到另一个分支dev分支去)

git branch master (切换到master分支去)

git branch -d dev(删除dev分支)、

git tag v1.0(贴上v1.0的标签)



git remote -v(查看远程里连接的状态)

(git remote add origin (远程仓库地址))

git remote show <remote>(显示remote的信息)

git pull <remote><branch>(看上面)

git fetch <remote>

git push <remote><branch>(git push origin maste)



git merge <branch>(dev)

git rm <resolved files>


git reset --hard head^

自己总结一下常用 的操作:

一。 . 上传本地文件到github

步骤:

1. 先各自在远程仓库和本地仓库建一个文件夹(仓库)

2. git init(初始化仓库)

3. git add (+文件名)

4. git commit -m"add a new file"

5.git remote -v(查看远程仓库的连接情况,把无关的git remote rm origin(移出掉))

6. git remote add origin (+远程仓库地址)(建立远程仓库)

7. git pull --rebase origin master

git pull = git fetch + git merge

git pull --rebase = git fetch + git rebase(创建一个新的虚拟提交R)(关于git merge和git rebase 的区别可以参考:https://www.cnblogs.com/kevingrace/p/5896706.html)


8. git push -u origin master(有时候会出错,如果排除7的问题可以使用:git push -f origin master(利用本地分支去覆盖远程仓库))


二。 克隆:

git clone (+远程仓库地址)


https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013744142037508cf42e51debf49668810645e02887691000(转载博文)



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325668875&siteId=291194637