GIT Ignore and removing files from GIT

If you want certain files not to be read by git then you can add those files in the .gitignore file.
This is more useful when your application creates log files within the projects itself, or there is a file which when opened with editor it creates temp file(Example Microsoft Word). You don’t those temp files to be visible when you check the status of your repository. Hence you can add the pattern of those temp files in the .gitignore file.

So the name of the file is .gitignore and you cannot create this file directly in windows, run the below command in command prompt to create the empty .gitignore file in the root directory of your repository

copy NUL .gitignore

Rules for the patterns you can put in the .gitignore file are below
1. Lines starting with # will be considered as comments and ignored
2. Standard global patterns work and it is applied recursively in the complete files in the project.
3. Patterns starting with / Forward slash are avoided recursively
4. Patterns ending with / Forward slash specified a directory
5. You can negate a pattern by starting it with an exclamation point !

Now you can see some of the sample patterns below

# ignore all .tmp files
*.tmp
# but do track lib.a, even though you're ignoring .a files above
!lastLogin.tmp
# only ignore the ABC file in the current directory, not subdir/ABC
/ABC
# ignore all files in any directory named bin
bin/
# ignore bin/comments.txt, but not bin/deploy/comments.txt
bin/*.txt
# ignore all .pdf files in the bin/ directory and any of its subdirectories
bin/**/*.pdf

Now when we create the file accordingly it will not be shown when we run the GIT status command.

Example : In the below example we have created hello.txt and hello.tmp, but since add .tmp file has to be ignore according to the *.tmp added into the .gitignore file, hence when we run git status -s command after creating hello.txt and hello.tmp only hello.txt is displayed and not hello.tmp. Screenshot of the example below.

Removing files from GIT

Removing files from GIT can be done using git rm command. But just git rm command will not remove the files but only remove the files and put it into staging area, you need to commit the files to actually remove.

git rm <file_name>
git commit -m "Comment"

Removing file from GIT but keeping a copy in local

We can remove the file from GIT but keep the file in local i.e make GIT no longer track the file using the below command

git rm --cached "Removing file from GIT but keeping the file in local"
git commit -m "Comment"

Lambda

Benefits of Lambda
1. To enable functional programming in java.
2. Write more readable, maintainable and concisely.
3. Write one or API very easily and effectively.
4. To enable parallel processing.

Explaining Lambda :
Since java is a object oriented language and if we create a function/method in java we need to create a class for it but suppose if the class is has only one method and we don’t want to create a class just to store one method in it then in that case we can use lambda
So what is lambda? Just like we assign a value to a variable like String name = “Java” or int x = 10 , we can assign a function itself to a variable.
Lets us take an example of

Interface obj = public void perform(){
                    System.out.println("Hello World");
              }

So what all things can be removed from the above method perform to make it more concise? Since the function will be available to all the method which has the variable hence it make no sense to add access modifiers lets remove that.

//Removed access specifiers
Interface obj = void perform(){
                    System.out.println("Hello World");
              }

Now in the above code what can be removed to make it more concise? since the name of the function is required to call it but if it assigned to a variable we don’t the name of the function hence remove it and check how it looks

//Removed name of the function
Interface obj = void (){
                    System.out.println("Hello World");
              }

Now when you look at the above code it is half way reached to the lambda expression, now what java 8 compiler says is the block of code which is assigned to temp variable and java 8 compiler can identify the return type of the block of code just by looking at the block of code we dont want explicitly notifying the return type of variable in case of lambda hence we can remove that too, now the function that is assigned to a variable looks something like below

//Removed the return type of the method
Interface obj = (){
                    System.out.println("Hello World");
              }

Good now we have create a block of code which can be assigned to a variable but how can we tell compiler that this is a lambda express? By using the symbol -> in between the bracket of parenthesis and curly braces of code, now after adding that the code looks some thing like this.

//Added the symbol-> to make java compiler understand that it is a lambda exp
//Lambda Expression 1
Interface obj = () -> {
                    System.out.println("Hello World");
              }

Now if your code has only one line then you can also remove the curly braces and the code looks something like this

//Curly braces can be removed if code is of single line
//Lambda Expression 2
Interface obj = () -> System.out.println("Hello World");

Now when suppose you have a lambda expression when returns a value, following the above steps it would look something like this :

Interface obj = (int a, int b) -> return a + b;
//Not valid keyword return since it is a onliner code

But since your code is just one line you can skip the keyword return as well, after removing that the code will look something like this

//Since since line code we can skip keyword return
//Lambda Expression 3
Interface obj = (int a, int b) -> a + b;

Now when you see the example above you can see that in lambda we are implementing one of the function declared in the interface, hence we have the data type of the parameters declared in the interface itself and hence we do not require the type of inputs to be mentioned in the lambda hence we can remove that as well.

//Since input parameters data type is mentioned in function declaration of interface we can remove that from the below lambda expression
//Lambda Expression 4
Interface obj = (a,b) -> a + b;

Now suppose we have only one input argument mentioned in the lambda then we can remove the brackets from the lambda as well

//one input argument example
Interface obj = (a) -> a *2 ;

Then we can remove the brackets from the above example since we can omit it in lambda the expression looks like this

//one input argument example
//Lambda Expression 5
Interface obj = a -> a *2 ;

What java 8? What is the intention of java 8.

Objectives of Java 8

  1. To simplify programming : Concise code
  2. To gain the benefits of functional programming in java (Lamada Expression)
  3. To utilize the advantage of parallel processing in jav(Parallel streams)

Features of Java 8

  1. Lambda Expressions
  2. Functional Interfaces
  3. Default Method in interfaces
  4. Static method inside interfaces
  5. Predicate (Predefined functional interfaces)
  6. Function (Predefined functional interfaces)
  7. Consumer (Predefined functional interfaces)
  8. Method reference and Constructor reference by double colon(::)operator
  9. Stream API
  10. Java 8 Date and Time API( Joda API)

Types of files status in GIT and viewing status of files and difference in them.

Files in a local GIT repository can be in 4 status :
a. Untracked : These files are newly added into the repository.
b. Unmodified : These files exist in repository and local and both have same content.
c. Modified : These files exist in repository and local but some changes are made to the local copy of the file.
d. Staged : These files exist in repository and local but some changes are made to the local copy of the files and the files has been added to the staging area using git add <file_name> command. These file will go with the next commit made to the repository

Status of GIT files can be viewed using below commands

git status
git status -s
#to see the time taken to check status
time git status


So there are different stages files in GIT for example below

Command : git status -s
  1. File f1.txt is modified but not yet staged hence M in the right column with read color
  2. File f2.txt is modified and added to the staging area hence M in the left column with green color
  3. File f3.txt was modified and added to the staging area but after that again there was modification done to f3.txt this time it was not added into staging are. If we commit f3.txt now GIT will only commit that version of file which was added to the staging area, the modified version of f3.txt will not be committed
  4. File f5.txt was a completely new file, it was created and added to the staging are hence A in the left column with GREEN color signifying it a newly added file
  5. File f4.txt displays ?? in the left and the right column which signifies it is an untracked file which is not present in the repository nor added into git staging area.

Now the same set of status can be viewed as a complete version with command git status, below is the screenshot of how it looks.

Command : git status

Configuration files in GIT

GIT configuration can be stored at three locations local, global and system. GIT config tool handles all these configuration by setting and getting its values.


git config

It is tool used by GIT to set and get configuration variables, these configuration controls all aspects of how GIT looks and operates

GIT Local
These configurations are stored in the .git folder in the same location where the repository is created

.git\config                  //on Windows

.git/config                  //on Linux

GIT Global
These configuration resides in the home directory(C:\Users\<UserName>\) i.e %USERPROFILE% and the configuration is stored in the file .gitconfig

%USERPROFILE%\.gitconfig                       //on Windows
or
~/.gitconfig or ~/.config/git/config           //on linux

GIT System
These configuration resides at location C:\ProgramData\Git\config

C:\ProgramData\Git\config        //on Windows
or
/etc/gitconfig                   //on Linux

You can view all your settings and see where they are coming from using the below command

git config --list --show-origin

Precedence of configuration file from lowest of highest
1. System configuration (Lowest priority)
2. Global configuration
3. Local configuration (Highest priority)

If you want to see value of one of the settings then you can run the below command

git config user.name

Creating a repository and committing files.

Repository can be created in two ways :
1. Take a local directory which is not under version control and convert it into GIT directory which is under version control
2. Take a online repository which already has files in it and clone it to your system.

  1. Making you own new repository (command : git init) and adding files to it (command : git add <fileName>)
git init
git add file1
git commit -m "Fist Commit"

2. Cloning files from an online repository

git clone https://github.com/libgit2/libgit2

We recommend you to use -o attribute with the git clone command so that we can rename the default short name that is orgin with some other value so that we can identify it more clearly.

git clone <url> -o <new short name>

Committing files directly in GIT (Skipping the staging area)

As we saw in the above example we can commit files into git using command git -m “Commit”, but only if the files are added to the staging area. If you want to directly commit a file without adding it into the staging area you can also do that but this type of commit commits all the files that are modified directly, it is a bit risky to add all the files hence you need to be sure before doing such type of commits.

git commit -a -m "Committing files directly into GIT, skipping the staging area"

Download and setting up git profile

  1. Download Git from following website (https://git-scm.com/)
  2. Install the GIt software with default Settings except, select “Use Git from Git Bash only”
  3. Then install GIT all by default options.

Setting up GIT
GIT requires setting up profile name and email
Open up GIT bash and fire below commands to setup git

git config --global user.name "Tyson Gill"
git config --global user.email "gill.tyson332@gmail.com"
git config --global core.editor nano
Note : Instead of user name "Tyson Gill" enter you own name and instead of email id enter your own email id.

To increase the performance of your GIT bash you can execute below commands

git config --global core.preloadindex true
git config --global core.fscache true
git config --global gc.auto 256
set GIT_TRACE=true
#below command should be handled with caution
export PS1='$'

To verify the all the sets run the below command

git config --list

If you need any help with any git command you can fireup the “help” attribute of any command in git using -h or –help attribute, sample below

$ git add -h
usage: git add [<options>] [--] <pathspec>...

    -n, --dry-run         dry run
    -v, --verbose         be verbose

    -i, --interactive     interactive picking
    -p, --patch           select hunks interactively
    -e, --edit            edit current diff and apply
    -f, --force           allow adding otherwise ignored files
    -u, --update          update tracked files
    --renormalize         renormalize EOL of tracked files (implies -u)
    -N, --intent-to-add   record only the fact that the path will be added later
    -A, --all             add changes from all tracked and untracked files
    --ignore-removal      ignore paths removed in the working tree (same as --no-all)
    --refresh             don't add, only refresh the index
    --ignore-errors       just skip files which cannot be added because of errors
    --ignore-missing      check if - even missing - files are ignored in dry run
    --chmod (+|-)x        override the executable bit of the listed files

What is GIT?

GIT is a version control tool just like CVS, Preforce etc but it works totally in a different way, if you need to undestand GIT you have to understand it like a totally new concept, trying to map concept of this tool with any another similar tool will not help you any faster.

GIT stores the changes like a stream of snapshots. Note these snap shots will only contain the changes that you have made with the previous version and not the complete file it self. If there is no change then it does not create a file or store the snapshot of it, GIT just links the file to it previous version.

Advantages of GIT :
1. All the GIT operation is possible in local : In traditional version control you have to compare the file history then you have to hit the repository and compare it but in case of GIT it all happens locally, this prevents the GIT from network dependency latency and give it a overhead of fast comparison.
2. GIT has integrity : Everything in GIT is stored in form of checksum, so you cannot escape from GIT after making any changes, even after you pull the data from repository GIT does compare the checksum(SHA-1 algorithm) to see if there were no damage done while transferring the file over the internet.
3. GIT Generally only Adds data : All the changes you make in GIT are store in GIT in the form of additional data, and you can go back to any point of time you committed the data since GIT maintains all of it, and hence you can code without the fear if loosing the previous code.

Three stages of GIT :
1. Committed : The file changes are successfully committed to the repository.
2. Modified : The file has been changes but not committed to the repository.
3. Staged : The file has been changes and it is marked as stages i.e the changes will go in the next commit made to the repository

Note : Staging is a new middle layer where you mark the files that there are the files that has been modified and need to committed.