Saturday, November 5, 2016

Git from scratch in a nutshell

After downloading Git from https://git-scm.com/downloads and installing it on your machine, you should open the terminal and configure the username (the author) and the email within the global configuration:



Then, create a new project and convert it to a Git repository (versioned project) issuing git init command:



Or, by using Spring Tool Suite as shown below:







Now, let's create a new file within the project directory, then add it to staging area using git add <file> command. Afterwards, commit these changes to the repository's database using git commit -m <message> command:


Or, by using Spring Tool Suite as shown below:






In order to check your repo status, you might find the command git status -s very useful with the param s standing for short. On the other hand, you have the command git log --oneline with the param oneline for brevity's sake to check commit history.



One of the most brilliant features of Git is branching. Branching allows you to work on a different directory rather than working on the production's one, which is a bad practice. Now let's suppose you want to do some development and commit changes within a different branch named development. The following commands are useful for this purpose:
$ git branch development
$ git checkout development
or simply:
$ git checkout -b development
These equivalent commands above, create a new branch named development and switch to it.
After making and committing your changes, you might want to merge these changes to the production directory(master branch) and delete the development branch. In order to do so, you could issue the following commands:
$ git checkout master
$ git merge development
$ git branch -d development























Pushing into GitHub
using
git pull origin <branch>
git push origin <branch>
commands:


Or, by using Spring Tool Suite as shown below:






Setting Up The Central Repository, And Multiple Development Environments









GitHub via SSH
In order to communicate with GitHub via SSH protocol rather than HTTPS(default), you should generate encryption keys using ssh-keygen -t <type> -b <lenght> - C <comment> command.
Afterward, you should start the SSH agent on your machine using eval "$(ssh-agent -s)" command and then add the SSH public key into it using ssh-add ~/.ssh/id_rsa.pub command.
Now copy the content of you public key file(~/.ssh/id_rsa.pub), head to GitHub.com, create new SSH key and paste that content into it.
You might verify the SSH connection using ssh -T -p 443 git@github.com command.
Now, in order to switch the protocol to SSH for the remote connection you should use git remote set-url origin git@github.com:<USERNAME>/<REPOSITORY>.git command. Afterward, you might check the effect of the previous command using git remote -v :


No comments:

Post a Comment