Git - Working Locally

From Training Material
Jump to navigation Jump to search

Git Locally

  • Git as an extension to the file system
  • No network required
  • Repository deleted if .git folder is deleted

Initializing a repository for existing files

git init
git add .
git commit

Setting Up Username

git config --global "Bernard"
git config --global

Browsing Repository

git show rev:file 
git show rev:directory  
git show rev

Basic Commands

git add file   
git rm file 
git mv file

Moving Files

git mv afile.txt movedfile.txt

# equivalent to

mv afile.txt movedfile.txt
git rm afile.txt
git add movedfile.txt

File Status


Committing new files and changes

# Commiting new files
git add newfile.txt
git commit

# Changes
echo new line >> afile.txt
git add afile.txt
git commit
echo new line >> afile.txt
git commit -a 

Git-commit-a1.png Git-commit-a2.png

Committing Folders

mkdir afolder
touch afolder/file1.txt
touch afolder/file2.txt
git add afolder
git commit

Empty Folders

mkdir emptyfolder
git add emptyfolder
git commit 
# Nothing to commit
touch emptyfolder/.gitignore
git add emptyfolder/.gitignore
git commit


1. Create a file structure

sudo mkdir /rcs
sudo chmod 777 /rcs 
mkdir /rcs/repo
cd /rcs/repo
echo first line > afile.txt

2. Initialize repository

3. Stage the file

4. Check whether the file has been staged

5. Commit the changes

6. Check whether the files have been committed

7. What is the SHA1 of the commit?

Tracking Changes

git status
git diff
git diff rev path
git show



Diff Examples

echo Commited Changes >> afile.txt 
git commit -a -m "asdf"

echo Staged Changes >> afile.txt 
git add afile.txt

echo Not-tracked Changes >> afile.txt 

git diff
[master c81907a] Commited changes
 1 file changed, 1 insertion(+)
diff --git a/afile.txt b/afile.txt
index fc321d0..978da6e 100644
--- a/afile.txt
+++ b/afile.txt
@@ -1,3 +1,4 @@
 first line
 Commited Changes
 Staged Changes
+Not-tracked Changes
git diff --cached
diff --git a/afile.txt b/afile.txt
index 37a8835..fc321d0 100644
--- a/afile.txt
+++ b/afile.txt
@@ -1,2 +1,3 @@
 first line
 Commited Changes
+Staged Changes
git diff HEAD
diff --git a/afile.txt b/afile.txt
index 37a8835..978da6e 100644
--- a/afile.txt
+++ b/afile.txt
@@ -1,2 +1,4 @@
 first line
 Commited Changes
+Staged Changes
+Not-tracked Changes

git diff HEAD^
diff --git a/afile.txt b/afile.txt
index 08fe272..978da6e 100644
--- a/afile.txt
+++ b/afile.txt
@@ -1 +1,4 @@
 first line
+Commited Changes
+Staged Changes
+Not-tracked Changes

Who fixed (spoiled) it? - git annotate

git annotate afile.txt
03be4dec        (   Bernard     2012-09-02 14:53:27 +0200       1)first line
c81907a9        (   Bernard     2012-09-02 16:50:24 +0200       2)Commited Changes
00000000        (Not Committed Yet      2012-09-02 17:08:44 +0200       3)Staged Changes
00000000        (Not Committed Yet      2012-09-02 17:08:44 +0200       4)Not-tracked Changes

Undoing Changes

git checkout .         # Remove not staged changes in local copy
git reset HEAD         # Remove changes from cache but leaves local copy intact
git restore <path>     # Same as checkout, EXPERIMENTAL command (atm 11.2023)

Amending latest commit

You can amend your latest commit (re-edit the metadata as well as update the tree)

git commit --amend

To toss your latest commit away completely

Exercise (Index)

  1. Add the new line to afile.txt
  2. Check the difference between index and the file
  3. Add the file
  4. Check the difference between repository and the staging
  5. Revert all not committed changes to the file (the file should be the same as in the repo)
  6. Add new line to a file
  7. Commit the changes

Reverting with New Commit

Revert the most recent commit:

git revert HEAD

This will create a new commit which undoes the change in HEAD.

Other example:

git revert HEAD^

Exercise (git revert)

  1. Add the line “bad changes” to afile.txt
  2. Commit your changes
  3. Use git revert to remove

Cleaning Untracked Files

touch untrackedfile.txt
git status
echo untrackedfile.txt > .gitignore
git status
git clean -n
git clean -f

.gitignore syntax

Lines starting with '#' are considered comments.

# Ignore any file named foo.txt.
# Ignore (generated) html files,
# except foo.html which is maintained by hand.
# Ignore objects and archives.

gitignore vs excludes


  • version-controlled
  • distributed to other repositories via clone (i.e., files that all developers will want to ignore)


  • specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow)

~/.gitconfig (core.excludesfile)

  • ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice)


  • echo something > Thumb.db
  • Add Thumb.db file to .gitignore
  • Add .gitignore to the repository and commit
  • Check whether Thumb.db is ignored in afolder
  • Clone the repo to /rcs/repoclone
  • Check whether the Thumb.db is ignored there

Browsing Repo

git gui
git log
git show 
git show HEAD:afolder
git show HEAD:afile
git show commit (can be tag or branch)
 git show HEAD^
 git show master
 git show c01a0d43


Press h to view more options!