Git
I use a MacBook pro, the following operations are performed under MacOs system. I am using code cloud (Gitee) as a private warehouse hosting.
Git commands for common operations
After installing Git, create a repository, first of all, choose a suitable place, create an empty directory:
$ mkdir StudyGit
$ cd StudyGit
$ pwd
/Users/melendez/StudyGit
pwd command displays the directory address to view the current folder: / Users / melendez / StudyGit
The second step, use the git init
command to turn into this directory can manage Git repository:
$ git init
Initialized empty Git repository in /Users/melendez/StudyGit/.git/
At this repository setup is completed, a .git directory will appear in the current directory, which is used to track the Git version management.
If you do not see .git directory, use the ls -ah
command to view
In StudyGit directory just created, create a test.txt file, there are two methods:
1, directly under StudyGit new directory.
2, in the terminal vi test.txt
command input Hello World
, click esc
after click control +q
, enter the command line wq
to save and exit.
Once you've created a file, you can use the git status
command to view the results:
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
A file into the warehouse, requires two steps:
1, using the git add
command, add files to the repository:
$ git add test.txt
2, use git commit -m xxx
the command, the file will be submitted to the repository:
$ git commit -m add test.txt
[master (root-commit) 646e2a8] add
1 file changed, 2 insertions(+)
create mode 100644 test.txt
Note: The -m
rear input is a description of this submission, you can enter any of the content, of course, the best is meaningful, easy to find from the record change history.
At this point we have submitted a file test.txt, now, we need to change this file, change at the contents:
Hello World
I am Melendez
Now run git status
to see the results:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
git status
Command allows us to always grasp the current state of the repository, the output of the above command tells us, readme.txt
has been modified, but not yet ready to modify submitted.
Although Git tell us at this time readme.txt
has been modified, but if we do not know what changed, you can use the git diff
command to view the content:
$ git diff test.txt
diff --git a/test.txt b/test.txt
index 9801343..db787e2 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
Hello World
+I am Melendez
You know what changed, submit the command again using the above can be.
git log
The furthest from the most recent commit log:
$ git log
commit cbd84e7c1540ff46fb4b6ed6bae861aecaf5e7ea (HEAD -> master)
Author: melendez <[email protected]>
Date: Sat Mar 28 16:02:51 2020 +0800
aaaa
commit 646e2a845d92c0b509eff73c3a054309fe578482
Author: melendez <[email protected]>
Date: Sat Mar 28 15:38:01 2020 +0800
add
If you find it too complicated content can be in git log --pretty=oneline
order:
% git log --pretty=oneline
cbd84e7c1540ff46fb4b6ed6bae861aecaf5e7ea (HEAD -> master) aaaa
646e2a845d92c0b509eff73c3a054309fe578482 add
If you want to return to the previous version, you can enter git reset --head xxxx(版本号)
can.
Cloned from a remote repository project
This semester, the school arranged using code cloud (Gitee) teaching, the following operations are based on Gitee.
First, log Gitee, a new warehouse StudyGit.
Language selection java
, add .gitignore choice Maven
.
Once created, select 克隆/下载
, click the 复制
button, copy Https link.
Open the terminal, enter Clone command:
$ git clone https://gitee.com/me1endez/StudyGit.git
Cloning into 'StudyGit'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
View files in the current directory:
$ ls -ah
. .git .gitignore README.md
.. .gitee README.en.md
Description above appear similar cloning success.
Branch Management
Creating and merging
Create and switch to the feature branch, use the git checkout -b feature
command:
$ git checkout -b feature
Switched to a new branch 'feature'
Once created, use the git branch
command to view the current location of the branch:
$ git branch
* feature
master
Create a test.txt file in the feature branch, and submit to a local warehouse:
$ vi test.txt
$ git add test.txt
$ git commit -m "branch test"
[feature 62bb7d0] branch test
1 file changed, 1 insertion(+)
create mode 100644 test.txt
If you need to switch to another branch, use the git checkout master
command:
$ git checkout master
Switched to branch 'master'
After the switch is complete, use the git branch
command to view the current location of the branch:
$ git branch
feature
* master
In the feature branch, create a test.txt file, submitted to the local repository, use the git merge feature
command will feature branches and merge the master branch:
$ git merge feature
Updating 823660b..62bb7d0
Fast-forward
test.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 test.txt
After the completion of the merger branch, if you want to delete the branch, use the git branch -d feature
command:
$ git branch -d feature
Deleted branch feature (was 62bb7d0).
After delete, view branch
, on the left master
branch:
git branch
* master
Resolve conflicts
Create a branch feature_a
, and README.md
add a file i am melendez
:
$ git checkout -b feature_a
Switched to a new branch 'feature_a'
$ vi README.md
$ git add README.md
$ git commit -m "i am melendez"
[feature_a 3ce1093] i am melendez
1 file changed, 1 insertion(+)
Once submitted, switch to the master
branch, and README.md
add a file I am Melendez
. :
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
$ vi README.md
$ git add README.md
$ git commit -m"I am Melendez"
[master 576310d] I am Melendez
1 file changed, 2 insertions(+), 1 deletion(-)
In this case, feature _a
the branch and master
the branch respectively submitted a new README.md file, it becomes this:
In this case, we will feature_a
branch into master
the branch, there is a conflict may occur:
$ git branch
feature_a
* master
$ git merge feature_a
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
Sure enough clashed, this time using git status
the command file can tell us README.md clashes:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Workaround:
Use cat README.md
the command to view the contents:
cat README.md
Hello World!
<<<<<<< HEAD
I am Melendez
=======
i am melendez
>>>>>>> feature_a
From this, we can see a case of inconsistency led to this conflict, we save changes:
Hello World!
I am Melendez
Resubmit:
$ git add README.md
$ git commit -m"conflict fixed"
[master 1a01533] conflict fixed
At this conflict repairs are completed, we can use git log --graph --pretty=oneline --abbrev-commit
to see the consolidation of branch:
$ git log --graph --pretty=oneline --abbrev-commit
* 1a01533 (HEAD -> master) conflict fixed
|\
| * 3ce1093 (feature_a) i am melendez
* | 576310d I am Melendez
|/
* 6d14291 add Hello World
* 62bb7d0 branch test
* 823660b (origin/master, origin/develop, origin/HEAD) Initial commit