Create local repository and push it to GitHub

First of all, you will need to create local repository.

Requirement: you should have git already installed. You can find more info here: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Go to the folder where your project is, then in terminal if you use Linux (0r Git Bash if you use Windows)

then type:

See if you have .gitignore file. If you have it then add folders which you want to exclude from appearing in your repository. If you don’t have it, then create it and add folders to ignore.

When you enter above command you might see message this message *** Please tell me who you are.

That means git doesn’t know particulars of the committer.

Then run this commands:

After that try committing again:

Now that git knows who we are, head to GitHub and create new repository(also skip adding README file, it’s too early too do so)

Then:

When you do this, you might face this error: hint: Updates were rejected because the remote contains work that you do
hint: not have locally.

That means remote repository has files that you don’t have locally. For example LICENSE file.

Then type:

This will allow you to get the remote files

Now:

Type your username and password

Now you if check your GitHub repository you can see that your files are there.

That’s it.

So whenever you do changes locally and want to commit them, just run these commands:

You might notice that every time you push your files, you need to enter your username and credentials. You can use below command to store your username and password. However, it will store your password somewhere on computer in plain text 🙂

then you will be asked to enter your username and password. After completing this you won’t be asked to do it again.

 

Sources:

http://kbroman.org/github_tutorial/pages/init.html

https://stackoverflow.com/questions/11403407/git-asks-for-username-every-time-i-push

 

 

How to enable minify on your local maven build

Sometimes you want to minify your js code on your local.

Normally, minify will be enabled on production, but not on local environment. So if you want to update or add new js library, you can test the minify on your local.

Go to pom.xml(the main one, the one in the project folder)

navigate to local profile(the name might be different)  <environment>local</environment>

then change this line <skipMinify>true</skipMinify>

to <skipMinify>false</skipMinify>

JS: IIFE(Immediately-invoked function expression ) and safe code

Sometimes we need to declare the function and call it immediately. Below code does that, it can also accept the parameter:

So we will see “hello John”. What happens here is that we wrapped the anonymous function inside the (); brackets. This allows to declare the anonymous function. Because JS compiler treats everything inside () as declaration. Then we add function and call it

second () actually invokes the function. The rest is easy we just pass the variable that we need. You can see the first example.

My understanding is that this approach is used in many frameworks.  It also possible to put the execute brackets () outside the function wrapper. Both are ok:

The beauty of the above code is that it’s safe. By that I mean that this code will run inside it’s own execution context and not global one.

So, variable “greetings” will not clash with variables with same name, but from other places in code.

If, however, we want to overwrite some existing global variable we can do it this way:

File 1, has this code:

File 2, has this code:

 

JS: good practices

  1. Be careful when returning some values

Ex:

function test(){

return

{

cacheable: true;

}

}

console.log(test()) ;

 

 

// console.log will return undefined, all of this is happening because the js compiler puts ; (semicolon) after the return statement. Therefore, it’s good practice to have curly braces directly after the return statment

ex: return {

cacheable: true;

}

2. Writing comments

The JS compiler will ignore the white spaces and comments so we can do something like this:

var

// this is the name of the user

firstName,

// this is the surname of the user

surname,

 

// this should be always more than 0

balance;

How to: Missing commit (error: unpack failed: error Missing commit) on Smargit

Go to the folder where your project is stored, then open the Git bash and run below commands:

  1. git gc
  2. git pull –rebase
  3. git push

In case you have this error:

error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

Then, just commit the files first. You can do it in the smart git.

 

Check the tomcat app.log in case of unexpected error

If you encounter some unexpected error with your web app, the best place to check what’s wrong is Tomcat’s app.log file.

1. Firstly, ssh to the server containing your app:

$ssh -A xxx.dev.XXX ( you can set shortcuts like in this file  ~/.ssh/config)

2. Get the root access

$sudo su –

3. Get the tail of the log. Tail command is useful to see the latest log entries and -f helps to keep refreshing it.

$tail -f app.log

Then replicate the bug, you should see the error message there. It can be null pointer exception for instance.

JavaScript, why are you like this?

JavaScript have some weird quirks. And one of them is how pointer “this” works.

look at the code below:

[code language=”javascript”]
var c = {
name: "this is c object",
log : function(){
this.name = "updated c object";
var setName = function(newname){
this.name = newname;
}
setName("Updating again! The c object");
console.log(this);
}
}
[/code]

Look at the line 6, you might think that name has been set to value “Updating again! The c object”, because this is pointing to c object. BUT, if you run the code, you will find out that actually line 6, is creating new Global variable name, and setting it’s value to “Updating again! The c object”. This happens because of how the execution context is created in JavaScript. In JS, the interpreter will hoist all the variables and methods first. Meaning before real execution line 6 is interpreted and variable is set to global variable(sort of being pushed all the way up the code).

The solution would be to store the reference to c object then pass it.

[code language=”javascript”] var c = {
name: "this is c object",
log : function(){
var self = this;
// note some developers use "that" instead of self
self.name = "updated c object";
var setName = function(newname){
self.name = newname;
}
setName("Updating again! The c object");
console.log(self);
}
} [/code]

This way, we will not set the name to be global variable and it will be set to c object. So c.name will return “Updating again! The c object”