@@ -53,7 +53,7 @@ You should have `git` installed and configured from the setup instructions.
5353
5454In this section, we are going to edit files in the Python package that we created earlier, and use ` git ` to track those changes.
5555
56- First, use a terminal to ` cd ` into the top directory of the local repository.
56+ First, use a terminal to ` cd ` into the top directory of the local repository (the outer molecool directory) .
5757
5858In order for ` git ` to keep track of your project, or any changes in your project,
5959you must first tell it that you want it to do this.
@@ -329,7 +329,7 @@ We are asking git to show us the difference between the current files and the se
329329Lines that have been added are indicated in green with a plus sign next to them (`+`),
330330while lines that have been deleted are indicated in red with a minus sign next to them (`-`)
331331
332- ## Viewing our previous versions
332+ ## Viewing previous versions
333333
334334If you need to check out a previous version,
335335
@@ -390,6 +390,90 @@ $ git commit -m "Initial commit after CMS Cookiecutter creation, version 1.0"
390390> {: .solution}
391391{: .challenge}
392392
393+ ## Exploring git history
394+
395+ When working on a project, it is easy to forget exactly what changes we have made to a file.
396+ To check this, do
397+
398+ ~~~
399+ $ git diff HEAD README.md
400+ ~~~
401+ {: .language-bash}
402+
403+ We should get a blank result.
404+ "HEAD" is referencing the most recent commit.
405+ Since we committed our changes `to README.md`, there is no difference to show.
406+
407+ Open your `README.md` and add the following line to the end of it.
408+
409+ ~~~
410+ This line doesn't add any value.
411+ ~~~
412+ Save that file and run the same command.
413+ ~~~
414+ $ git diff HEAD README.md
415+ ~~~
416+ {: .language-bash}
417+
418+ ~~~
419+ diff --git a/README.md b/README.md
420+ index 94e0b50..a68f349 100644
421+ --- a/README.md
422+ +++ b/README.md
423+ @@ -17,6 +17,8 @@ This package requires the following:
424+ - numpy
425+ - matplotlib
426+
427+ +This line doesn't add any value.
428+ +
429+ ### Copyright
430+ ~~~
431+ {: .output}
432+
433+ To compare against the commit just before the most recent commit, add "~1" to end of "HEAD":
434+
435+ ~~~
436+ $ git diff HEAD~ 1 README.md
437+ ~~~
438+ {: .language-bash}
439+
440+ ~~~
441+ diff --git a/README.md b/README.md
442+ index e778cd4..94e0b50 100644
443+ --- a/README.md
444+ +++ b/README.md
445+ @@ -13,6 +13,10 @@ This repository is currently under development. To do a development install, dow
446+
447+ in the repository directory.
448+
449+ +This package requires the following:
450+ + - numpy
451+ + - matplotlib
452+ +
453+ ### Copyright
454+ ~~~
455+ {: .output}
456+
457+
458+ If we want to compare against a specific commit, we can first do `git log` to find the commit's ID, and then do:
459+
460+ ~~~
461+ $ git diff * commit_id* README.md
462+ ~~~
463+ {: .language-bash}
464+
465+ Another problem that we sometimes encounter is wanting to undo all of our changes to a particular file.
466+ This can be done with
467+
468+ ~~~
469+ $ git checkout HEAD README.md
470+ ~~~
471+ {: .language-bash}
472+
473+ If you open `README.md` you will see that it has reverted to the content from the most recent commit.
474+
475+ Of course, you could also replace `HEAD` here with `HEAD~1` or a specific commit ID.
476+
393477
394478## Creating new features - using branches
395479
@@ -420,7 +504,25 @@ $ git checkout -b zen
420504~~~
421505{: .language-bash}
422506
423- Next, add a new function to your `functions.py` module.
507+ We can visualize what branching looks like with some simple figures.
508+ Before branching, imagine a git commit history that looks like this.
509+ In the diagram below, each circle represents a git commit.
510+ There have been two commits, and the HEAD is currently after commit 2.
511+
512+ <center><img src='../fig/github_workflows/git_history_0.svg'></center>
513+
514+ After we have created a new branch and checked it out, we can imagine our git history looking like this.
515+ The `zen` branch 'branches' or starts from the point where we used the git branch command.
516+
517+ <center><img src = '../fig/github_workflows/git_branch.svg'></center>
518+
519+ Now, when we make a commit on the `zen` branch, our changes will continue from this point, leaving the main branch unchanged.
520+ Note that we have not yet made a commit, but this diagram is for illustrative purposes.
521+
522+ <center><img src = "../fig/github_workflows/branch_development.svg"></center>
523+
524+ Now that we have a better understanding of what branching looks like, lets make some changes to the `zen` branch.
525+ Add a new function to your `functions.py` module.
424526We are going to add the ability to print "The Zen of Python". You can get the Zen of Python by typing
425527
426528~~~
@@ -821,6 +923,153 @@ $ git branch -d zen
821923> {: .solution}
822924 {: .challenge}
823925
926+ ## Ignoring files - .gitignore
927+
928+ Sometimes while you work on a project, you may end up creating some temporary files.
929+ For example, if your text editor is Emacs, you may end up with lots of files called `<filename>~`.
930+ By default, Git tracks all files, including these.
931+ This tends to be annoying, since it means that any time you do `git status`,
932+ all of these unimportant files show up.
933+
934+ We are now going to find out how to tell Git to ignore these files,
935+ so that it doesn't keep telling us about them ever time we do `git status`.
936+ Even if you aren't working with Emacs, someone else working on your project might,
937+ so let's do the courtesy of telling Git not to track these temporary files.
938+ First, lets ensure that we have a few dummy files.
939+ Make empty files called `testing.txt~` and `README.md~` in your repository using your text editor of choice.
940+
941+
942+ While we're at it, also make some other files that aren't important to the project.
943+ Make a file called `calculation.out` in `molecool/data` using your text editor of choice.
944+
945+ Now check what Git says about these files:
946+
947+ ~~~
948+ $ git status
949+ ~~~
950+ {: .language-bash}
951+
952+ ~~~
953+ On branch main
954+ Your branch is up to date with 'origin/main'.
955+
956+ Untracked files:
957+ (use "git add <file >..." to include in what will be committed)
958+
959+ README.md~
960+ molecool/data/calculation.in
961+ molecool/data/calculation.out
962+ testing.txt~
963+
964+ nothing added to commit but untracked files present (use "git add" to track)
965+ ~~~
966+ {: .output}
967+
968+ Now we will make Git stop telling us about these files.
969+
970+ Earlier, when we looked at the hidden files, you may have noticed a file called `.gitignore`.
971+ Cookiecutter created this for us, however, GitHub also has built in `.gitignore` files you can add when creating an empty repository.
972+
973+ This file is to tell `git` which types of files we would like to ignore (thus the name `.gitignore`)
974+
975+ Look at the contents of `.gitignore`
976+
977+ ~~~
978+ # Byte-compiled / optimized / DLL files
979+ __ pycache__ /
980+ * .py[ cod]
981+ * $py.class
982+
983+ # C extensions
984+ * .so
985+
986+ # Distribution / packaging
987+ .Python
988+ env/
989+ build/
990+ develop-eggs/
991+ dist/
992+ downloads/
993+ eggs/
994+ .eggs/
995+ lib/
996+ lib64/
997+ parts/
998+ sdist/
999+ var/
1000+ wheels/
1001+ * .egg-info/
1002+ .installed.cfg
1003+ * .egg
1004+
1005+ # PyInstaller
1006+ # Usually these files are written by a python script from a template
1007+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
1008+ * .manifest
1009+ * .spec
1010+
1011+ ...
1012+ ~~~
1013+
1014+ Git looks at `.gitignore` and ignores any files or directories that match one of the lines.
1015+ Add the following to the end of `.gitignore`:
1016+
1017+ ~~~
1018+ # emacs
1019+ * ~
1020+
1021+ # temporary data files
1022+ * .in
1023+ * .out
1024+
1025+ ~~~
1026+
1027+ Now do "git status" again. Notice that the files we added are no longer recognized by git.
1028+
1029+ ~~~
1030+ $ git status
1031+ ~~~
1032+ {: .language-bash}
1033+
1034+ ~~~
1035+ On branch main
1036+ Your branch is up to date with 'origin/main'.
1037+
1038+ Changes not staged for commit:
1039+ (use "git add <file >..." to update what will be committed)
1040+ (use "git checkout -- <file >..." to discard changes in working directory)
1041+
1042+ modified: .gitignore
1043+
1044+ no changes added to commit (use "git add" and/or "git commit -a")
1045+ ~~~
1046+ {: .output}
1047+
1048+ We want these additions to `.gitignore` to become a permanent part of the repository:
1049+
1050+ ~~~
1051+ $ git add .gitignore
1052+ $ git commit -m "Ignores Emacs temporary files and data directory"
1053+ ~~~
1054+ {: .language-bash}
1055+
1056+ One nice feature of `.gitignore` is that prevents us from accidentally adding
1057+ a file that shouldn't be part of the repository.
1058+ For example:
1059+
1060+ ~~~
1061+ $ git add data/calculation.in
1062+ ~~~
1063+ {: .language-bash}
1064+
1065+ ~~~
1066+ The following paths are ignored by one of your .gitignore files:
1067+ data/calculation.in
1068+ Use -f if you really want to add them.
1069+ ~~~
1070+ {: .output}
1071+
1072+ It is possible to override this with the `-f` option for `git add`.
8241073
8251074## More Tutorials
8261075If you want more `git`, see the following tutorials.
0 commit comments