Git Series (Git/Github)

Git

concept

distributive version controller

Example of version controlling in developing cooperation:

image-20250714142050243

working mechanism

image-20250714162939404

Workspace is not IDE, but the index of the code(On disk) .

git add just add the code temporarily into temporary storages.

After git commit, the history version is created in the local lib, which cannot be deleted.(Unless deleting the whole local lib)

remote code host center (remote lib) on Internet: github

on LAN: GitLab

common command

  • set user signature:
1
2
git config --global user.name [name]
git config --global user.email [email@example.com]

The signature is an ID justification. The email you set should be your github account.

  • Initialize local lib
1
git init

Suppose the project is in the folder git-demo, then under this folder, start the git bash, enter the command above, then git get authorization of the folder git-demo, and initialized the local lib.

  • check status
1
git status

example log:

1
2
3
On branch master //on which branch
no commits yet //blank git lib
nothing to commit

add a new file hello.txt under the project and input “hello world.”

1
2
3
4
5
6
7
8
9
10
11
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
//hello.txt is only in the workspace, but not added in the cache, so it's not been tracked by git

nothing added to commit but untracked files present (use "git add" to track)

  • add files to cache
1
git add [filename]

after git add hello.txt, the status:

1
2
3
4
5
6
7
8
On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt

  • commit the file to the local lib
1
git commit -m "log message" [filename]

Example: after git commit -m "my first commit" hello.txt, the log:

1
2
3
4
5
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) 64c69a6] hello git commit
1 file changed, 1 insertion(+)
create mode 100644 hello.txt

the status:

1
2
On branch master
nothing to commit, working tree clean
  • check log
1
2
git reflog
64c69a6 (HEAD -> master) HEAD@{0}: commit (initial): hello git commit
1
2
3
4
5
6
7
git log
commit 64c69a6abe6429b73df0648c46c809104003c0d1 (HEAD -> master)
Author: Miss.m <example@gmail.com>
Date: Mon Jul 14 17:19:23 2025 +0800

hello git commit

Version iteration stimulation Example

For example, now change the content in hello.txt to “hello world again.” to create a new version:

1
2
3
4
5
6
7
8
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: hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

after git add:

1
2
3
4
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt

git commit:

1
2
3
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master 522457b] second commit
1 file changed, 1 insertion(+), 1 deletion(-)

now check the version:

1
2
522457b (HEAD -> master) HEAD@{0}: commit: second commit
64c69a6 HEAD@{1}: commit (initial): hello git commit

There are 2 versions in the local lib and the pointer points to the second committed version currently.

  • Version shuttle
1
git reset --hard [version id]

for example, shuttle to ver1 of hello.txt

1
2
3
4
5
6
7
8
9
10
git reset --hard 64c69a6
HEAD is now at 64c69a6 hello git commit

git reflog
64c69a6 (HEAD -> master) HEAD@{0}: reset: moving to 64c69a6
522457b HEAD@{1}: commit: second commit
64c69a6 (HEAD -> master) HEAD@{2}: commit (initial): hello git commit

cat hello.txt
hello world. #hello.txt ver1

image-20250714185201838

branch

In the version control process, when advancing multiple tasks at the same time, you can create an independent branch for each task. Using branches means that developers separate their tasks from the main line of development, and when developing their own branches, it will not affect the main line.

explanation:

image-20250714191718196

Programmer A want to add function A for the project, so it forks a branch Task A from the ver1.0 Master branch. After development, he unions Task A to the main branch, and update it to ver1.1. Then the Infra found some bugs in ver1.1, so he forks a branch Hot fix from the ver1.1 Master branch, fixing the bugs, and update the Master to ver1.2.

branch operations

  • check branches:

    1
    git branch -v
  • add branch:

    1
    git branch [branchname]
  • branch shuttle:

    1
    git checkout [branchname]

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$ cat hello.txt
hello world.

$ git branch -v
* master 64c69a6 hello git commit

$ git branch hot-fix

$ git branch -v
hot-fix 64c69a6 hello git commit
* master 64c69a6 hello git commit

$ git checkout hot-fix
Switched to branch 'hot-fix'

$ git branch -v
* hot-fix 64c69a6 hello git commit
master 64c69a6 hello git commit

$ echo "Hello fixed" > hello.txt

$ git status
On branch hot-fix
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: hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it

$ git commit -m "hot-fix first commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[hot-fix 1a25fb0] hot-fix first commit
1 file changed, 1 insertion(+), 1 deletion(-)

$ cat hello.txt
Hello fixed

$ git checkout master
Switched to branch 'master'

$ cat hello.txt
hello world.

  • branch merge

    1
    git merge [branchname]

    can merge specific branch to the current one.

    example1(without confliction):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ git merge hot-fix
    Updating 64c69a6..1a25fb0
    Fast-forward
    hello.txt | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)

    $ cat hello.txt
    Hello fixed

    conflict

    Two different branches fixed the same position of the same file differently. In that case, Git cannot determine which one to deserve, then it causes conflict.

    example2(confliction):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$ vim hello.txt

$ cat hello.txt
Hello fixed 111

$ git add hello.txt

$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt


$ git commit -m "master commit 111" hello.txt
[master bd2481a] master commit 111
1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout hot-fix
Switched to branch 'hot-fix'

$ cat hello.txt
Hello fixed

$ vim hello.txt

$ cat hello.txt
Hello fixed 222

$ git add hello.txt

$ git commit -m "hot-fix commit 222" hello.txt
[hot-fix 185866c] hot-fix commit 222
1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout master
Switched to branch 'master'

$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

$ git status
On branch master
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: hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

The log Automatic merge failed; fix conflicts and then commit the result. shows that the merge was failed, because master and hot-fix fixed the same file hello.txt .

As a result, we need to merge the code manually.

now check the file:

1
2
3
4
5
6
cat hello.txt
<<<<<<< HEAD
Hello fixed 111
=======
Hello fixed 222
>>>>>>> hot-fix

The code between <<<<<<< HEAD and ======= is what the current branch has in the file, while the code between ======= and >>>>>>> hot-fix is that in the branch hot fix.

Now suppose that we’d like to keep the hot-fix version, then delete the special lines which are combined of > = <, and keep Hello fixed 222, then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ cat hello.txt
<<<<<<< HEAD
Hello fixed 111
=======
Hello fixed 222
>>>>>>> hot-fix


$ vim hello.txt

$ cat hello.txt
Hello fixed 222

$ git add hello.txt

-$ git commit -m "Merge test"
[master c1996a0] Merge test

-$ cat hello.txt
Hello fixed 222

-$ git status
On branch master
nothing to commit, working tree clean

git cooperation mode

  • collaboration within a team:

    image-20250714201103007

  • Cross-team collaboration:

    image-20250714213327829

Github

Create a new repository

image-20250714214627445

image-20250714215206613

Creating an Alias for the remote lib:

1
git remote add [alias] [remotelib]

Check the alias:

1
git remote -v

example:

1
2
3
4
5
6
7
8
$ git remote -v

$ git remote add git-test https://github.com/murasame-mio-misaki/git-test.git

$ git remote -v
git-test https://github.com/murasame-mio-misaki/git-test.git (fetch)
git-test https://github.com/murasame-mio-misaki/git-test.git (push)

Push

1
git push [alias] [branch]

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ cat hello.c
#include<stdio.h>

int main()
{
printf("Hello Github!");
return 0;
}


$ git reflog
fb7bd98 (HEAD -> master) HEAD@{0}: commit (initial): murasame master first commit

$ git push git-test master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 32 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 295 bytes | 295.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/murasame-mio-misaki/git-test.git
* [new branch] master -> master

image-20250714220815017

Pull

1
git pull [alias] [branch]

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ git pull git-test master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 979 bytes | 97.00 KiB/s, done.
From https://github.com/murasame-mio-misaki/git-test
* branch master -> FETCH_HEAD
fb7bd98..a0352da master -> git-test/master
Updating fb7bd98..a0352da
Fast-forward
hello.c | 1 +
1 file changed, 1 insertion(+)

$ cat hello.c
#include<stdio.h>

int main()
{
printf("Hello Github!");
printf("Hello midori!");
return 0;
}

Clone

1
git clone [remotelib]

Add collaborator

image-20250714222556042

image-20250714222707340

image-20250714222818997

Commit and push automatically in Vscode/IntelliJ/…

image-20250805114427768


Git Series (Git/Github)
https://murasame-mio-misaki.github.io/2025/07/15/Git/
Author
Miss.M
Posted on
July 15, 2025
Licensed under