icyrock.com

Home

Git trails

2015-Jun-01 20:42
git

Warning: Make sure you understand the scripts before running them. They delete files. They run destructive git commands. You will likely need to have the same exact folders. If you are in a wrong folder or if you type the commands incorrectly, you can end up losing your data. If unsure, do not run them or run them within a throw-away virtual machine, they are easy to set up. Sample VM setup tutorial.

Some SSCCE examples of using git. All the outputs are produced by using bash -x, so that commands are visible inline.

1. Initialize repository, some basic operations

Code:

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
 
# Set up the author information
export GIT_AUTHOR_NAME=emma@example.org
export GIT_AUTHOR_EMAIL=emma@example.org
 
# Clean the temp folder
cd /mnt/ramdisk
rm -rf /mnt/ramdisk/* /mnt/ramdisk/.git
 
# Init git repository
git init
 
# Add a file and commit
echo "Hello world" > hello-world.txt
git add hello-world.txt
git status -s
git commit -m "Added hello world"
git status -s
git shortlog
 
# Update the file and commit
echo "Hello world!" > hello-world.txt
git status -s
git add hello-world.txt
git diff --staged
git commit -m "Updated hello world"
git status -s
git shortlog

Sample output:

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
+ export GIT_AUTHOR_NAME=emma@example.org
+ GIT_AUTHOR_NAME=emma@example.org
+ export GIT_AUTHOR_EMAIL=emma@example.org
+ GIT_AUTHOR_EMAIL=emma@example.org
+ cd /mnt/ramdisk
+ rm -rf /mnt/ramdisk/hello-world.txt /mnt/ramdisk/.git
+ git init
Initialized empty Git repository in /mnt/ramdisk/.git/
+ echo 'Hello world'
+ git add hello-world.txt
+ git status -s
A  hello-world.txt
+ git commit -m 'Added hello world'
[master (root-commit) f483a76] Added hello world
 Author: emma@example.org <emma@example.org>
 1 file changed, 1 insertion(+)
 create mode 100644 hello-world.txt
+ git status -s
+ git shortlog
emma@example.org (1):
      Added hello world
 
+ echo 'Hello world!'
+ git status -s
 M hello-world.txt
+ git add hello-world.txt
+ git diff --staged
diff --git a/hello-world.txt b/hello-world.txt
index 802992c..cd08755 100644
--- a/hello-world.txt
+++ b/hello-world.txt
@@ -1 +1 @@
-Hello world
+Hello world!
+ git commit -m 'Updated hello world'
[master c67fa20] Updated hello world
 Author: emma@example.org <emma@example.org>
 1 file changed, 1 insertion(+), 1 deletion(-)
+ git status -s
+ git shortlog
emma@example.org (2):
      Added hello world
      Updated hello world

2. Amending a commit

Code:

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
 
# Set up the author information
export GIT_AUTHOR_NAME=emma@example.org
export GIT_AUTHOR_EMAIL=emma@example.org
 
# Clean the temp folder
cd /mnt/ramdisk
rm -rf /mnt/ramdisk/* /mnt/ramdisk/.git
 
# Init git repository
git init
 
# Add a file and commit
echo "Hello world" > hello-world.txt
git add hello-world.txt
git commit -m "Wrong commit message"
git shortlog
 
# Amend the commit message
git commit --amend -m "Correct commit message"
git shortlog
 
# Add another file and amend the commit by adding that file
echo "Yin and yang" > yin-yang.txt
git add yin-yang.txt
git commit --amend -m "Correct commit message and files"
git shortlog

Sample output:

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
+ export GIT_AUTHOR_NAME=emma@example.org
+ GIT_AUTHOR_NAME=emma@example.org
+ export GIT_AUTHOR_EMAIL=emma@example.org
+ GIT_AUTHOR_EMAIL=emma@example.org
+ cd /mnt/ramdisk
+ rm -rf /mnt/ramdisk/hello-world.txt /mnt/ramdisk/.git
+ git init
Initialized empty Git repository in /mnt/ramdisk/.git/
+ echo 'Hello world'
+ git add hello-world.txt
+ git commit -m 'Wrong commit message'
[master (root-commit) c473f07] Wrong commit message
 Author: emma@example.org <emma@example.org>
 1 file changed, 1 insertion(+)
 create mode 100644 hello-world.txt
+ git shortlog
emma@example.org (1):
      Wrong commit message
 
+ git commit --amend -m 'Correct commit message'
[master 92657c3] Correct commit message
 Author: emma@example.org <emma@example.org>
 Date: Wed Apr 1 22:11:10 2015 -0400
 1 file changed, 1 insertion(+)
 create mode 100644 hello-world.txt
+ git shortlog
emma@example.org (1):
      Correct commit message
 
+ echo 'Yin and yang'
+ git add yin-yang.txt
+ git commit --amend -m 'Correct commit message and files'
[master 9f191be] Correct commit message and files
 Author: emma@example.org <emma@example.org>
 Date: Wed Apr 1 22:11:10 2015 -0400
 2 files changed, 2 insertions(+)
 create mode 100644 hello-world.txt
 create mode 100644 yin-yang.txt
+ git shortlog
emma@example.org (1):
      Correct commit message and files

3. Unstaging and reverting files

Code:

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
 
# Set up the author information
export GIT_AUTHOR_NAME=emma@example.org
export GIT_AUTHOR_EMAIL=emma@example.org
 
# Clean the temp folder
cd /mnt/ramdisk
rm -rf /mnt/ramdisk/* /mnt/ramdisk/.git
 
# Init git repository
git init
 
# Add a few files and stage them
echo "Original file1" > file1.txt
echo "Original file2" > file2.txt
echo "Original file3" > file3.txt
git add .
git status -s
 
# Unstage file3.txt
git reset file3.txt
 
# Commit
git commit -m "Committed file1.txt and file2.txt"
git status -s
git shortlog
 
# Modify file2.txt, then revert to the committed state
echo "Modified file2" > file2.txt
git checkout -- file2.txt
cat file2.txt

Sample output:

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
+ export GIT_AUTHOR_NAME=emma@example.org
+ GIT_AUTHOR_NAME=emma@example.org
+ export GIT_AUTHOR_EMAIL=emma@example.org
+ GIT_AUTHOR_EMAIL=emma@example.org
+ cd /mnt/ramdisk
+ rm -rf /mnt/ramdisk/hello-world.txt /mnt/ramdisk/yin-yang.txt /mnt/ramdisk/.git
+ git init
Initialized empty Git repository in /mnt/ramdisk/.git/
+ echo 'Original file1'
+ echo 'Original file2'
+ echo 'Original file3'
+ git add .
+ git status -s
A  file1.txt
A  file2.txt
A  file3.txt
+ git reset file3.txt
+ git commit -m 'Committed file1.txt and file2.txt'
[master (root-commit) 2a07253] Committed file1.txt and file2.txt
 Author: emma@example.org <emma@example.org>
 2 files changed, 2 insertions(+)
 create mode 100644 file1.txt
 create mode 100644 file2.txt
+ git status -s
?? file3.txt
+ git shortlog
emma@example.org (1):
      Committed file1.txt and file2.txt
 
+ echo 'Modified file2'
+ git checkout -- file2.txt
+ cat file2.txt
Original file2

4. Working with remotes

Code:

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
 
# Set up the author information
export GIT_AUTHOR_NAME=emma@example.org
export GIT_AUTHOR_EMAIL=emma@example.org
export GIT_COMMITTER_NAME=emma@example.org
export GIT_COMMITTER_EMAIL=emma@example.org
 
# Clean the temp folder
cd /mnt/ramdisk
rm -rf /mnt/ramdisk/.[^.]* /mnt/ramdisk/*
 
# Init folders
mkdir local
mkdir remote
 
# Init local and remote git repositories
cd local
git init
cd ../remote
 
git init --bare
cd ../local
 
# Setup remote
git remote add rt ../remote
 
# Add a file and commit locally
echo "Original file1" > file1.txt
git add .
git commit -m "Committed file1.txt"
git status -s
 
# Push to remote
git push rt master
git remote show rt
 
# Modify file1.txt, commit, push to remote
echo "Modified file1" > file1.txt
git add .
git commit -m "Modified file1.txt"
git shortlog
 
git push rt master
git remote show rt

Sample output:

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
56
57
58
59
60
61
+ export GIT_AUTHOR_NAME=emma@example.org
+ GIT_AUTHOR_NAME=emma@example.org
+ export GIT_AUTHOR_EMAIL=emma@example.org
+ GIT_AUTHOR_EMAIL=emma@example.org
+ export GIT_COMMITTER_NAME=emma@example.org
+ GIT_COMMITTER_NAME=emma@example.org
+ export GIT_COMMITTER_EMAIL=emma@example.org
+ GIT_COMMITTER_EMAIL=emma@example.org
+ cd /mnt/ramdisk
+ rm -rf '/mnt/ramdisk/.[^.]*' /mnt/ramdisk/local /mnt/ramdisk/remote
+ mkdir local
+ mkdir remote
+ cd local
+ git init
Initialized empty Git repository in /mnt/ramdisk/local/.git/
+ cd ../remote
+ git init --bare
Initialized empty Git repository in /mnt/ramdisk/remote/
+ cd ../local
+ git remote add rt ../remote
+ echo 'Original file1'
+ git add .
+ git commit -m 'Committed file1.txt'
[master (root-commit) 952de11] Committed file1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt
+ git status -s
+ git push rt master
To ../remote
 * [new branch]      master -> master
+ git remote show rt
* remote rt
  Fetch URL: ../remote
  Push  URL: ../remote
  HEAD branch: master
  Remote branch:
    master tracked
  Local ref configured for 'git push':
    master pushes to master (up to date)
+ echo 'Modified file1'
+ git add .
+ git commit -m 'Modified file1.txt'
[master e8c5bb9] Modified file1.txt
 1 file changed, 1 insertion(+), 1 deletion(-)
+ git shortlog
emma@example.org (2):
      Committed file1.txt
      Modified file1.txt
 
+ git push rt master
To ../remote
   952de11..e8c5bb9  master -> master
+ git remote show rt
* remote rt
  Fetch URL: ../remote
  Push  URL: ../remote
  HEAD branch: master
  Remote branch:
    master tracked
  Local ref configured for 'git push':
    master pushes to master (up to date)

5. Server-side hooks

Code:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 
# Set up the author information
export GIT_AUTHOR_NAME=emma@example.org
export GIT_AUTHOR_EMAIL=emma@example.org
export GIT_COMMITTER_NAME=emma@example.org
export GIT_COMMITTER_EMAIL=emma@example.org
 
# Clean the temp folder
cd /mnt/ramdisk
rm -rf /mnt/ramdisk/.[^.]* /mnt/ramdisk/*
 
# Init folders
mkdir local
mkdir remote
mkdir www
 
# Setup remote and local repositories
cd remote
git init --bare
cat > hooks/post-receive <<EOF
#!/bin/sh
GIT_WORK_TREE=../www git checkout -f
echo www is now ready
EOF
chmod +x hooks/post-receive
 
cd ../local
git init
git remote add rt ../remote
 
# Add, commit and push a file
echo "Original file1" > file1.txt
git add .
git commit -m "Committed file1.txt"
git status -s
 
git push rt master
 
cd ../www
ls
cat file1.txt
echo "Outside modification to file1" > file1.txt
echo "Extra file" > file2.txt
 
# Modify file1.txt, commit, push to remote
cd ../local
echo "Modified file1" > file1.txt
git add .
git commit -m "Modified file1.txt"
git shortlog
 
git push rt master
git remote show rt
 
cd ../www
ls
cat file1.txt
 
# Overwrite file2.txt
cd ../local
echo "Original file2.txt" > file2.txt
git add file2.txt
git commit -m "Added file2.txt"
git push rt master
 
cat ../www/file2.txt
 
# Delete file2.txt
git rm file2.txt
git commit -m "Removed file2.txt"
git push rt master
 
ls ../www

Sample output:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
+ export GIT_AUTHOR_NAME=emma@example.org
+ GIT_AUTHOR_NAME=emma@example.org
+ export GIT_AUTHOR_EMAIL=emma@example.org
+ GIT_AUTHOR_EMAIL=emma@example.org
+ export GIT_COMMITTER_NAME=emma@example.org
+ GIT_COMMITTER_NAME=emma@example.org
+ export GIT_COMMITTER_EMAIL=emma@example.org
+ GIT_COMMITTER_EMAIL=emma@example.org
+ cd /mnt/ramdisk
+ rm -rf '/mnt/ramdisk/.[^.]*' /mnt/ramdisk/local /mnt/ramdisk/remote /mnt/ramdisk/www
+ mkdir local
+ mkdir remote
+ mkdir www
+ cd remote
+ git init --bare
Initialized empty Git repository in /mnt/ramdisk/remote/
+ cat
+ chmod +x hooks/post-receive
+ cd ../local
+ git init
Initialized empty Git repository in /mnt/ramdisk/local/.git/
+ git remote add rt ../remote
+ echo 'Original file1'
+ git add .
+ git commit -m 'Committed file1.txt'
[master (root-commit) c031413] Committed file1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt
+ git status -s
+ git push rt master
remote: www is now ready       
To ../remote
 * [new branch]      master -> master
+ cd ../www
+ ls
file1.txt
+ cat file1.txt
Original file1
+ echo 'Outside modification to file1'
+ echo 'Extra file'
+ cd ../local
+ echo 'Modified file1'
+ git add .
+ git commit -m 'Modified file1.txt'
[master ced49af] Modified file1.txt
 1 file changed, 1 insertion(+), 1 deletion(-)
+ git shortlog
emma@example.org (2):
      Committed file1.txt
      Modified file1.txt
 
+ git push rt master
remote: www is now ready       
To ../remote
   c031413..ced49af  master -> master
+ git remote show rt
* remote rt
  Fetch URL: ../remote
  Push  URL: ../remote
  HEAD branch: master
  Remote branch:
    master tracked
  Local ref configured for 'git push':
    master pushes to master (up to date)
+ cd ../www
+ ls
file1.txt
file2.txt
+ cat file1.txt
Modified file1
+ cd ../local
+ echo 'Original file2.txt'
+ git add file2.txt
+ git commit -m 'Added file2.txt'
[master 688c747] Added file2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file2.txt
+ git push rt master
remote: www is now ready       
To ../remote
   ced49af..688c747  master -> master
+ cat ../www/file2.txt
Original file2.txt
+ git rm file2.txt
rm 'file2.txt'
+ git commit -m 'Removed file2.txt'
[master 15ebf09] Removed file2.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 file2.txt
+ git push rt master
remote: www is now ready       
To ../remote
   688c747..15ebf09  master -> master
+ ls ../www
file1.txt