Notes on Git Page - How to


using linux    Home


Chapter 1

3 places git stores info
Command To configure
To configure user and email
git config --global user.name "Abe Lincoln"
git config --global user.email "AbeL@1865.com"
git config --global core.editor "vim"
git config --global color.ui true
git config --list



Chapter 2

configure auto completion
script on github: https://github.com/git/git
Go to folder called contrib
Then to 'completion'
Get the one for bash shell (at top are instructions)
(select "RAW" and copy and paste to .git-completion.bash)
To .bashrc file, add:
if [ -f ~/.git-completion.bash ]
then
soure ~/.git-completion.bash
fi
git help (for git help :)


Chapter 3

Initialize a Project:
create a directory, i.e. git_proj_cpu_chk
cd to that directory
lnx# git init
(there will now be a .git directory/file, and it is that dir where git is tracking)
Add a file
use:
lnx# git add .
that will add all changes in current dir
Then need to commit them:
lnx# git commit -m "msg noting first commit of chk date py script"
Make change
Add changes
commit changes
Read commit log:
lnx# git log
or
lnx# git log -n 5
lnx# git log --since=2019-05-01
lnx# git log --until=2019-08-01
lnx# git log --author="Car"
lnx# git log --grep="Init"
Will list commit ID, author, date, message


Chapter 4

Three trees

Repository Staging Working
lnx# git add myFile.txt (will place in staging)
lnx# git commit puts in repository

git generates a checksum for each change
a checksum algorithm converts data into a number
The same data will always have same checksum
data integrity is important
(changing data will change the checksum)
git uses SHA-1 hash algorithm to create checksums. It creates a 40 char hex string


HEAD Pointer - points to top of current branch in respository, the place to record next. It is the last state of repository (what was last checked out). And it points to parent of next commit where writing commits takes place


Chapter 5

To get status of git: lnx# git status


Chapter 6

push a new project/repository on linux to githug:
On github, add the new repository
github will display commands that you can copy and paste onto linux command line:
git remote add origin git@github.com:<username>/git_myProjII.git
git push -u origin master
(note that you will need to have proper keys added to github)


On linux, create a directory to make git repository:
lnx# mkdir testRepo lnx# cd testRepo lnx# git init

point to github:
git remote add origin https://github.com/username/testRepo.git

Fork another's repository:
navigate to desired repository on github, and click "fork"

Now you have a copy of desired respository on your github account
Make a local copy of the repo on your computer
This is called "cloning" and can be done using the following:
lnx$ git clone https://github.com/username/repoNameHere.git
(this will clone the repository into your current directory)

Chapter 7

EDIT FILE
If make change to file in repository, and check 'git status' It will show modified but not committed
Git recognize it is tracked, but knows version in working tree is not same as in staging or repository.
Process of adding changes to file is same as when add new file
pi@raspberrypi:~/git_chksbc $ git status
On branch 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:   chksbc.py

no changes added to commit (use "git add" and/or "git commit -a")
Or 1 file staged (but not committed) and 1 not staged.
pi@raspberrypi:~/git_chksbc $ git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

	modified:   chksbc.py

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   chk_sip_grey_list.py

So, type, 'git add chksbc.py'
(staging and working now agree, but not yet committed to repository)

Chapter 8

Using diff - shows what has been added, removed, or changed
File 'a' is the one in repository, and File 'b' is the one in the working directory
The '+' will tell you lines added (colored green)

pi@raspberrypi:~/git_chksbc $ git diff
diff --git a/chk_sip_cpu.py b/chk_sip_cpu.py
index 4ebd5b4..ffb268f 100755
--- a/chk_sip_cpu.py
+++ b/chk_sip_cpu.py
@@ -11,7 +11,7 @@ import matplotlib.pyplot as plt
 
 ##################################################################
 # check_sipd_logs
-#  go thru each sipd log in cur dir and search for line with cpu
+#  go thru each sipd log in cur dir and search for relevant line with cpu
 #  add cpu and date list and then graph
 ###################################################################
 def check_sipd_logs_cpu(currDir,writeDir,currYear):
@@ -77,5 +77,6 @@ def check_sipd_logs_cpu(currDir,writeDir,currYear):
 
 def main():
     print("module only")
+
 if __name__=='__main__':
     main()



If want to diff on files in staging:
git diff --staged
Note that 'staged' is alias for 'cached':
git diff --cached (does same thing)


Chapter 9

Using git to track files when deleted

pi@raspberrypi:~/git_chksbc $ git status
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)

	newFile.py

nothing added to commit but untracked files present (use "git add" to track)

(Not in repository or staging - only in working directory)
You can simply delete a file, and git status will be:
pi@raspberrypi:~/git_chksbc $ git status
On branch master
Changes not staged for commit:
  (use "git add/rm ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	deleted:    newFile.py

no changes added to commit (use "git add" and/or "git commit -a")

Then remove with git:
git rm newFile.py
pi@raspberrypi:~/git_chksbc $ git rm newFile.py 
rm 'newFile.py'
pi@raspberrypi:~/git_chksbc $ git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

	deleted:    newFile.py

OR YOU CAN JUST HAVE GIT REMOVE IT - more convenient:
git rm fileToDelete.txt
uses linux rm. Note that it will already be added to staging directory


Chapter 10

Move and rename files (really the same thing)
if rename, git show as a delete and an add
You then do a git add tstFile2.txt and a git rm tstFileTwo.txt (as in example below), and it will show 'renamed' :
(if more than, like, 50% similar, git recognize is same file)
Example:
pi@raspberrypi:~/git_chksbc $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add/rm ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	deleted:    tstFileTwo.txt

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

	tstFile2.txt

no changes added to commit (use "git add" and/or "git commit -a")
pi@raspberrypi:~/git_chksbc $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add/rm ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	deleted:    tstFileTwo.txt

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

	tstFile2.txt

no changes added to commit (use "git add" and/or "git commit -a")
pi@raspberrypi:~/git_chksbc $ git add tstFile2.txt
pi@raspberrypi:~/git_chksbc $ git rm tstFileTwo.txt
rm 'tstFileTwo.txt'
pi@raspberrypi:~/git_chksbc $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

	renamed:    tstFileTwo.txt -> tstFile2.txt

OR just use git mv:
git mv tstFileOne.txt tstFile1.txt
pi@raspberrypi:~/git_chksbc $ git mv tstFileOne.txt tstFile1.txt
pi@raspberrypi:~/git_chksbc $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

	renamed:    tstFileOne.txt -> tstFile1.txt
	renamed:    tstFileTwo.txt -> tstFile2.txt




OLD NOTES
add
git add . (adds all new files)
git add -u (updates tracking for files that changed names or wer deleted)
git add -A (does both)

git commit -m "message" (updates your local repo)
git push (push to remote dir on github)

Branches - sometimes you are working on a project with a version being used by many people
you many not want to edit that version
So you can create a branch with the command
git checkout -b branchname
To see what branch you are on type:
git branch
To switch back to the master branch:
git checkout master


fork a repo
Git Basics


Markdown Syntax
md file
## This is a secondary heading
### This is a tertiary heading

* first item in list
* second item in list
* third item in list