This tutorial walks through basic GIT usage in the local directory.
The tutorial covers:
.
- How to intiailise a directory to make it a git repository.
- How to add and commit any new files or changes to files, to the change history.
- How to view the status of the repository and look at its history in various ways.
- How to make a branch in the master branch to allow new versions to be developed.
- How to merge any development versions back into the master version.
- Understanding of how conflicts are created and how to solve them.
- How to revert to an earlier version of a project and resolve any conflicts.
Contents
Instructions are in blue,
commands to be typed are in a bordered white box
and a 'Return' should be made after each instruction.
The terminal output is in a bordered yellow box
and webpage edits are in a bordered blue box
Creating the repository
To create a git repository the directory in which you want to track changes.
The first step is to initialise the directory
and save any files present using the git commit method.
Begin by creating a new directory an changing into it.
mkdir git-demo
cd git-demo
[no output]
Create a README.txt as recommended by GIT
echo This is a demonstration of basic git operations > README.txt
[no output]
Check file is there
ls -al
drwxrwxr-x 2 paul paul 4096 Jan 11 21:09 .
drwxrwxr-x 7 paul paul 4096 Jan 11 21:08 ..
-rw-rw-r-- 1 paul paul 42 Jan 11 21:09 README.txt
Initialise this directory as a git directory.
git init
Initialised empty Git repository in .../git-demo/.git/
See the status of the new directory
git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.txt
nothing added to commit but untracked files present (use "git add" to track)
To 'Stage' the file ready for 'Saving' to the repository we use 'git add', as suggested:
git add README.txt
[no output]
...and to 'save' the 'staged' file(s) into repository
git commit -m 'first commit'
master (root-commit) db2a2d9] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.txt
Note: The commit numbers will be different on your machine.
See the status of the repository
git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
Copy and paste the following into a new text file and save as webpage.html
in the git-demo directory created earlier.
This is version 1 on
master
See the status of the repository
git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
webpage.html
nothing added to commit but untracked files present (use "git add" to track)
'Stage' the file ready for 'Saving' to the repository using 'git add', as suggested:
git add webpage.html
[no output]
As before commit the changes using 'git commit'
git commit -m 'Version 1 of webpage'
on 1 of webpage
[master 97fbe63] Version 1 of webpage
1 file changed, 27 insertions(+)
create mode 100644 webpage.html
Edit webpage.html. Change 'version 1' to read as 'version 2'and save.
version 2 on
See the status of the repository
git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: webpage.html
no changes added to commit (use "git add" and/or "git commit -a")
'Stage' the file ready for 'Saving' to the repository using 'git add', as suggested:
git add webpage.html
[no output]
As before commit the changes using 'git commit'
git commit -m 'Version 2 of webpage'
[master 939e01c] Version 2 of webpage
1 file changed, 1 insertion(+), 1 deletion(-)
See the status of the repository
git status
On branch master
nothing to commit, working tree clean
Edit webpage.html. Change 'version 2' to read as 'version 3' and save.
version 3 on
See the status of the repository
git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: webpage.html
no changes added to commit (use "git add" and/or "git commit -a")
'Stage' the file ready for 'Saving' to the repository using 'git add', as suggested:
git add webpage.html
[no output]
As before commit the changes using 'git commit'
git commit -m 'Version 3 of webpage'
[master e731947] Version 3 of webpage
1 file changed, 1 insertion(+), 1 deletion(-)
Exploring the history Git Log
Enter git log
git log
commit fe3dd1bce5f2af23b1206a82d94fb2c645d44e67
(HEAD ->
master)
Author: sxs98pmh
Date: Thu Jan 12 16:13:00 2023 +0000
Version 3 of webpage
commit 46359e4a64666aa0008c4b66f32cf121c3884ef3
Author: sxs98pmh
Date: Thu Jan 12 15:58:23 2023 +0000
Version 2 of webpage
commit e742d19c47949ed71348d4088af77d6e595ae850
Author: sxs98pmh
Date: Thu Jan 12 14:47:16 2023 +0000
Version 1 of webpage
.
.
.
Press SHIFT and Q to quit the readout or RETURN to move down the list
The output can be modified with git log options: enter
git log --oneline
e731947 (HEAD ->
master)
Version 3 of webpage
939e01c Version 2 of webpage
97fbe63 Version 1 of webpage
28cf8f7 first commit.
.
.
Press SHIFT & Q to quit the readout or RETURN to move down the list
And the output can be in the form of a primitive graph: enter
git log --graph
* commit e731947247a712a973d2742af12601b4c4a4c0a3
(HEAD ->
master).
| Author: sxs98pmh <sxs98pmh@reading.ac.uk>.
| Date: Thu Jan 12 16:13:00 2023 +0000.
| .
| Version 3 of webpage.
| .
* commit 939e01c56930de4af8a2e40d9367b82158ad7205.
| Author: sxs98pmh <sxs98pmh@reading.ac.uk>.
| Date: Thu Jan 12 15:58:23 2023 +0000.
| .
| Version 2 of webpage.
| .
* commit 97fbe6330cd5a2e106a55b674a64dca336773200.
| Author: sxs98pmh <sxs98pmh@reading.ac.uk>.
| Date: Thu Jan 12 14:47:16 2023 +0000.
| .
| Version 1 of webpage.
.
.
.
And mixed together: enter
git log --oneline --graph --decorate --all
* e731947 (HEAD ->
master) Version 3 of webpage
* 939e01c Version 2 of webpage
* 97fbe63 Version 1 of webpage
* db2a2d9 first commit
It is useful to have the last command an alias to save the typing: enter
Linux: alias gitgraph='git log --oneline --graph --decorate --all'
Windows: Function gitgraph { git log --oneline --graph --decorate --all}
[no output]
Now typing gitgraph will display as before: enter
gitgraph
* e731947 (HEAD ->
master) Version 3 of webpage
* 939e01c Version 2 of webpage
* 97fbe63 Version 1 of webpage
* db2a2d9 first commit
Exploring the differences: Git Diff
Enter git diff with the Version 2 commit id , then the Version3 commit id.
git diff 939e01c e731947
diff --git a/webpage.html b/webpage.html
index 8db630d..031c5c9 100644
--- a/webpage.html
+++ b/webpage.html
@@ -16,7 +16,7 @@
<body>
<p>Hello world</p>
<p>
- <span class="mydivstyle">This is <b>version 2</b> on
+ <span class="mydivstyle">This is <b>version 3</b> on
<b>
master
</b>
.
.
.
Branching
Safe modifications to version edits
Enter git branch new_version
git branch new_version
[no output]
Now look at all the branches: enter
git branch
* master
new_version
To use the branch we must switch to it:
git checkout new_version
Switched to branch 'new_version'
Reopen the file webpage.html and change as follows and save
To save the changes on the new branch, add and commit the changes
git add webpage.html
git commit -m 'version 4 on branch new_version'
[new_version 48f4c66] new version on branch
1 file changed, 31 insertions(+), 35 deletions(-)
rewrite webpage.html (79%)
Now use gitgraph to see the changes
gitgraph
* 48f4c66 (HEAD ->
new_version)version 4 on branch new_version
* e731947 (master
)Version 3 of webpage
* 939e01c Version 2 of webpage
* 97fbe63 Version 1 of webpage
* db2a2d9 first commit
The HEAD is now set to new_version.To see the effect refresh webpage.html in the browser
To see Version 3 again we need to switch back to master:
git checkout master
Switched to branch 'master'
The HEAD is now set to master. Refresh webpage.html in the browser
Switch back to new_version:
git checkout new_version
Switched to branch 'new_version'
Now make one more version of webpage.html
Change and save the version line to read as follows:
<span class="mydivstyle">This is <b>version 5</b> on
After saving webpage.html, add and commit changes:
git add webpage.html
git commit -m 'version 5 on branch new_version'
[new_version 0d3877b] version 5 on new_version
1 file changed, 1 insertion(+), 1 deletion(-)
Now use gitgraph to see the changes
gitgraph
| * 5a9a298 (
HEAD ->
version 5 on branch new_version
) version 5 on branch new_version
| * 598bfdf version 4 on branch new_version
* 5647c4b (master)
Version 3 of webpage
* 939e01c Version 2 of webpage
* 97fbe63 Version 1 of webpage
* db2a2d9 first commit
Merging branches
Change back to master branch
git checkout master
Switched to branch 'master'
Should you decide that you like the changes made to new_version
and want to make them part of the master branch.
The format of the merge command is git merge <source branch> <target branch>
Source and Target are optional - if you only have one branch you do not need to specify
Perform git merges from the master branch.
git merge new_version
Updating 95fe460..7c46681
Fast-forward
webpage.html
| 27 +++++++++++
----------------
1 file changed, 11 insertions(+), 16 deletions(-)
Now use gitgraph to see the changes
gitgraph
* 1d4e74a (HEAD ->
master,
new_version)
version 5 on branch new_version
* 9c0ca81 version 4 on branch new_version
* 5647c4b Version 3 of webpage
* b90367a Version 2 of webpage
* bb6c4d5 Version 1 of webpage
* 28cf8f7 first commit
Refresh webpage.html and you will see that the colour has changed to pink and the version number has changed to 5.
and the branch has changed the 'new_version'.
Conflicts
Sometimes you will get conflicts: this happens when a branch version has had the same line(s) altered as on the master branch.
When GIT tries to merge it does not know which change should take priority.
Change webpage.html to lines to read as "version 6 and "new_version" to "master", as before in the text editor.
After saving webpage.html, add and commit changes:
git add webpage.html
git commit -m 'version 6 on branch master'
on 6 on branch master'
[master e2af762] version 6 on branch master
1 file changed, 3 insertions(+), 3 deletions(-)
Change back to new_version branch. Change the version number to 7 for webpage,html, as before in the text editor.
git checkout new_version
Switched to branch 'new_version'
After saving webpage.html, add and commit changes:
git add webpage.html
git commit -m 'version 7 on branch new_version'
on 7 on branch new_version'
[new_version 4a8f145] version 7 on branch new_version
1 file changed, 1 insertion(+), 1 deletion(-)
Change back to the master. Now try a git merge:
git checkout master
git merge new_version
Auto-merging webpage.html
CONFLICT (content): Merge conflict in webpage.html
Automatic merge failed; fix conflicts and then commit the result.
Now look at the file in the text editor. You will see that git has highlighted the problem line(s)
<<<<<<<<<<<< HEAD
This is version 6 on
===========
This is version 7 on
>>>>>>>>>>>> new_version
Git is asking you to resolve the conflict in the file in the text editor.
We decide to keep 'version 7' from the 'new_version' branch.
Erase the <<<<<< ====== and >>>>>>, and the line "This is version 6 on ",
so edit the file so it reads like this:
This is version 7 on
After saving webpage.html, add and commit changes:
git add webpage.html
git commit -m 'version 7 on branch master'
[master 3fc5156] version 7 on branch master
Now use gitgraph to see the changes
gitgraph
* 3fc5156 (HEAD ->
master) version 7 on branch master
|\
* 4a8f145 (new_version
) version 7 on branch new_version
* | e2af762 version 6 on branch master
|/
* 48f4c66 (HEAD ->
new_version) new version on branch
* e731947 Version 3 of webpage
* 939e01c Version 2 of webpage
* 97fbe63 Version 1 of webpage
* db2a2d9 first commit
Refresh webpage.html and you will see that the colour has changed to magenta and the version number has changed to 7.
Reverting changes
What if you merge your version to a 'live' version in 'master', but then realise the update is causing an error.
You may want to put the original back and fix the problem and recommit.
In this case you would want to 'revert' the changes.
Say you made a mistake and no longer wanted version 7 from new_version?
In this case you can revert to the previous Version 6 state by issuing a a git revert command.
git revert [version 6 commit id]
hint: Fix them up in the work tree, and then use 'git add/rm '
hint: as appropriate to mark resolution and make a commit.
fatal: revert failed
Because the two versions have the same line altered, Git is asking you to confirm, by asking you to
resolve the conflict in the file in the text editor.
error: Reverting is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: revert failed
git status
On branch master
You are currently reverting commit e2af762.
(fix conflicts and run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: webpage.html
We revert to 'version 6' from the 'master' branch, so edit the file so it reads like this:
<span class="mydivstyle">This is <b>version 6 </b> on
<b>
master
</b>
</span>
Git will now ask you to re-commit to the reverted version and mark it in the history
After saving webpage.html, add and commit changes:
git add webpage.html
git commit -m 'version 6 on branch master'
[master e72ab60] version 6 on branch master
1 file changed, 2 insertions(+), 2 deletions(-)
Now use gitgraph to see the changes
gitgraph
* e72ab60 (HEAD ->
master) version 6 on branch master
* 3fc5156 version 7 on branch master
|\
|
* 4a8f145 (new_version
) version 7 on branch new_version
* | e2af762 version 6 on branch master
|/
* 7c46681 version 5 on branch new_version
* e5b86d1 version 4 on branch new_version
* 95fe460 Version 3 of webpage
.
.
.
That completes the basics of GIT usage locally.
You should now understand:
- How to intiailise a directory to make it a git repository.
- How to add and commit any new files or changes to files, to the change history.
- How to view the status of the repository and look at its history in various ways..
- How to make a branch in the master branch to allow new versions to be developed
- How to merge any development versions back into the master version.
- Understand how conflicts are created and how to solve them.
- How to revert to an earlier version of a project and resolve any conflicts.
Contents