leader

Git Notes

View markdown

Modifying Files

You have a local, working repo and you've modify a file. Now what? It's time to add the change to the repo, commit the change(s) to the repo and push the change to the remote origin/master repo. The following steps do this.

First run git status to see what has changed.


$ git status
On branch master
Your branch is up to date with 'origin/master'.
 Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   lued_root/lued_bindings.lua

The git status command shows that a file has been modified. Note that the comment indicates the changes are not staged for commit and that we should use git add. Let's follow the instructions for a change.


$ git add lued_root/lued_bindings.lua
$ git status
On branch master
Your branch is up to date with 'origin/master'.
 Changes to be committed:
  (use "git reset HEAD ..." to unstage)

	modified:   lued_root/lued_bindings.lua

After running git add the changes are now ready to be committed. Let's run git commit -m to commit the change with a meaningful message.


$ git commit -m "Change alt+B key binding to go to the bottom of file"
[master 71208f2] Change alt+B key binding to go to the bottom of file
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git status
On branch master Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

After running git commit, run git status again. It says that your branch is ahead by 1 commit and that you should run git push. Again, let's following the directions.


$ git push
Username for 'https://github.com': username
Password for 'https://username@github.com': password
Counting objects: 4, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 409 bytes | 409.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/jwrr/lued.git
   0561c5c..71208f2  master -> master

$ git status
On branch master
Your branch is up to date with 'origin/master'.

The changes are now pushed up to the public repo and git status shows that your local repo is up to date.

What's the Diff

Sometimes I don't remember what I changed. git diff will show the difference between the modified file and the repo.

$ git diff efefomatic.php
diff --git a/efefomatic.php b/efefomatic.php
index 461a822..3bad5f0 100644
--- a/efefomatic.php
+++ b/efefomatic.php
-$efef_md[] = array( 'name' => 'mark',   'from' => "/\s==([~]*?)==(?=[\p{P}\s])/s"   , 'to' => ' ');
+$efef_md[] = array( 'name' => 'mark',   'from' => "/\s==([=]*?)==(?=[\p{P}\s])/s"   , 'to' => ' ');
 $efef_md[] = array( 'name' => 'u',      'from' => "/\s___([_]*?)___(?=[\p{P}\s])/s" , 'to' => ' ');

Sometimes I've already done git add, I'm about to write a meaningful commit message, and I run git diff and it doesn't show the dfferences.


$ git add efefomatic.php
$ git diff efefomatic.php

To see the differences between the original repo and the staged files add the --staged qualifier to git diff --staged filename.


$ git diff --staged efefomatic.php
diff --git a/efefomatic.php b/efefomatic.php
index 461a822..3bad5f0 100644
--- a/efefomatic.php
+++ b/efefomatic.php
@@ -108,12 +108,15 @@ $efef_md[] = array( 'name' => 'code',    'from' => "/([\n]+?)/s" , 'to' => '<
-$efef_md[] = array( 'name' => 'mark',   'from' => "/\s==([~]*?)==(?=[\p{P}\s])/s"   , 'to' => ' ');
+$efef_md[] = array( 'name' => 'mark',   'from' => "/\s==([=]*?)==(?=[\p{P}\s])/s"   , 'to' => ' ');

Git Fetch vs Git Pull

The git fetch and git pull commands are often confused, sound similar-ish, and are usually used together in sequence. And to add to the confusion git pull even has options that will run git fetch, although I don't use or recommend these options. I prefer to use each command individually.

Most importantly, the git fetch command is harmless. It can be run at anytime. It will not change, modify, delete or update any of the files in your local repo. I frequently run git fetch just out of habit.

It just fetches useful meta-data from the remote repo, which is usually origin/master in my case.

Here is a simple test case. First run git status and it indicates Your branch is up to date.


$ git status
On branch master
Your branch is up to date with 'origin/master'.

But is it really? You know that commits from another remote repo were pushed to origin. Why doesn't git status indicate your repo is behind? Because your local repo doesn't know it's behind. This is where git fetch comes in.


$ git fetch
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 5 (delta 4), reused 5 (delta 4), pack-reused 0
Unpacking objects: 100% (5/5), done.
From https://github.com/jwrr/lued
   d7d8340..0561c5c  master     -> origin/master

It looks like git fetch did something. Now re-run git status.


$ git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Now git status correctly reports that Your branch is behind by 1 commit. If you want to pull the changes from origin into to your local repo use the git pull command.

In the following example let's pretend we know that the line containing TT was changed and pushed to origin/master. First use grep to show the current value of the line. Then run git pull. It looks like it updated some files, which is promising. Now run grep again and... YES, the TT line was changed to the new value.


$ grep TT lued_root/lued_bindings.lua
alt_TT        = tab_next              hot("TT")

$ git pull
Updating d7d8340..0561c5c
Fast-forward
 README.md                   | 6 +++---
 lued_root/lued_bindings.lua | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

$ grep TT lued_root/lued_bindings.lua
alt_TT        = tab_prev              hot("TT")

Now re-run git status and it shows we're once again current with the remote master/origin repo.


$ git status
On branch master
Your branch is up to date with 'origin/master'.

As shown, git pull changes your working repo, so only use it when you're ready for the changes.

Re-Writing the Past

asdf