Category: Uncategorized

Home / Category: Uncategorized

git – starter project

October 24, 2019 | Uncategorized | No Comments

Git can be confusing to use. I tried contributing to open source projects on github but got hung up several times.

The trick is to get comfortable with the basics using throwaway data. Stick to your local machine and a tiny file. Once you master that, do the same but now include a remote repository like GitHub. Doing it this way gives the freedom to quickly repeat commands over several fast iterations. It lets you get comfortable using the (frankly ridiculous) number of features and workflows without the fear of accidentally breaking data. In the process of repeating these little commands, you master them so you’ll have them when you need them later, whether it’s a bit personal project or when you collaborate with others on a project.

Try this project to get started with git:

Challenge 1: Local Repository with Feature Addition

  • Create a local git repository (a.k.a. “repo”)
  • Add a text file.
  • Add the new file to the repo.
  • Commit the addition to the repo.
  • Add a new feature branch to the repo.
  • Add the new feature by changing the text file.
  • Stage the updated file.
  • Compare the staged file to the original.
  • Commit the changes to the feature branch.
  • Merge the feature branch into the master branch.
  • Commit the changes.
  • View commit history.
  • View all branches.
  • Cleanup merged branches.
  • Review commit history with changes.

Once you’ve done this, try doing it several times with variations on the theme: more branches, bigger changes, etc. Afterward, you’ll have in your toolbox the git commands git: init, add, stage, commit, status, branch (add and delete) and diff.

The first time I completed this task took about 45 minutes. After running through it five or six times, it was down to 3 minutes. Here’s what my session looked like:

aaron@OICREMOTE75:~$ mkdir GitProject
aaron@OICREMOTE75:~$ cd GitProject
aaron@OICREMOTE75:~/GitProject$ git init
Initialized empty Git repository in /home/aaron/GitProject/.git/
aaron@OICREMOTE75:~/GitProject$ touch sayhi.py
aaron@OICREMOTE75:~/GitProject$ cat > sayhi.py
print("Hi from the original master branch.")
^Z
[1]+  Stopped                 cat > sayhi.py
aaron@OICREMOTE75:~/GitProject$ python3 sayhi.py
Hi from the original master branch.
aaron@OICREMOTE75:~/GitProject$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        sayhi.py

nothing added to commit but untracked files present (use "git add" to track)
aaron@OICREMOTE75:~/GitProject$ git add sayhi.py
aaron@OICREMOTE75:~/GitProject$ git status
On branch master

No commits yet

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

        new file:   sayhi.py

aaron@OICREMOTE75:~/GitProject$ git commit -m "initial commit"
[master (root-commit) 1ce81bd] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 sayhi.py
aaron@OICREMOTE75:~/GitProject$ git status
On branch master
nothing to commit, working tree clean
aaron@OICREMOTE75:~/GitProject$ git branch ChangeMessage
aaron@OICREMOTE75:~/GitProject$ git checkout ChangeMessage
Switched to branch 'ChangeMessage'
aaron@OICREMOTE75:~/GitProject$ cat > sayhi.py
print("Hello from the ChangeMessage branch.")
^Z
[2]+  Stopped                 cat > sayhi.py
aaron@OICREMOTE75:~/GitProject$ git status
On branch ChangeMessage
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   sayhi.py

no changes added to commit (use "git add" and/or "git commit -a")
aaron@OICREMOTE75:~/GitProject$ git stage sayhi.py
aaron@OICREMOTE75:~/GitProject$ git status
On branch ChangeMessage
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   sayhi.py

aaron@OICREMOTE75:~/GitProject$ git diff HEAD
diff --git a/sayhi.py b/sayhi.py
index acedb8a..87f65ec 100644
--- a/sayhi.py
+++ b/sayhi.py
@@ -1 +1 @@
-print("Hi from the original master branch.")
+print("Hello from the ChangeMessage branch.")
aaron@OICREMOTE75:~/GitProject$ git commit -m "updated message"
[ChangeMessage 3914c00] updated message
 1 file changed, 1 insertion(+), 1 deletion(-)
aaron@OICREMOTE75:~/GitProject$ git checkout master
Switched to branch 'master'
aaron@OICREMOTE75:~/GitProject$ python3 sayhi.py
Hi from the original master branch.
aaron@OICREMOTE75:~/GitProject$ git merge ChangeMessage
Updating 1ce81bd..3914c00
Fast-forward
 sayhi.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
aaron@OICREMOTE75:~/GitProject$ python3 sayhi.py
Hello from the ChangeMessage branch.
aaron@OICREMOTE75:~/GitProject$ git branch -a
  ChangeMessage
* master
aaron@OICREMOTE75:~/GitProject$ git branch -d ChangeMessage
Deleted branch ChangeMessage (was 3914c00).
aaron@OICREMOTE75:~/GitProject$ git branch -a
* master
aaron@OICREMOTE75:~/GitProject$ git log -p
commit 3914c00d23f19472c7d9e9d1b637d4c9a98504d1 (HEAD -> master)
Author: AaronVBrown <worldofaaron@hotmail.com>
Date:   Thu Oct 24 14:12:00 2019 -0700

    updated message

diff --git a/sayhi.py b/sayhi.py
index acedb8a..87f65ec 100644
--- a/sayhi.py
+++ b/sayhi.py
@@ -1 +1 @@
-print("Hi from the original master branch.")
+print("Hello from the ChangeMessage branch.")

commit 1ce81bd3300a5a3bbf3b1a4c53e8eecb7c7c2db5
Author: AaronVBrown <worldofaaron@hotmail.com>
Date:   Thu Oct 24 14:09:17 2019 -0700

    initial commit

diff --git a/sayhi.py b/sayhi.py
new file mode 100644
index 0000000..acedb8a
--- /dev/null
+++ b/sayhi.py
@@ -0,0 +1 @@
+print("Hi from the original master branch.")
aaron@OICREMOTE75:~/GitProject$           

No more waiting for the perfect blog post to initiate the site! I’m diving right in with semi-random thoughts on programming as I encounter them. Maybe I’ll be the only reader. That will work! There’s at least a lesson or two every day to write down, so this is where I’ll do that.