Git essential, 20 August 2017

My brother start a really great project. I’ll link to it when he’ll finish if he’ll approve.

But he told me that he lost a few day work because he doesn’t got any version control…

So I decided to write another git essential post, first to make sure everything is clear to me, and second to try and help others …

What is git

Git is a free and open source distributed version control system [source]


How to use git

  1.   First we need a repository, we can build it by our self

git init

or we can use github or gitlab to init it for us and then just clone it

  1. Checking that the git is fine.

It’s healthy to run git status often. Sometimes things change and you don’t notice it.

git status

  1. After any new file creation we need to add it to the repository

Later we’ll see that we can ignore some files or folder using .gitignore

git add

4.Run the commit command with a message describing what we’ve changed

git commit -m “description what we’ve changed”

  1. Git’s log is a journal that remembers all the changes we’ve committed so far, in the order we committed them

git log

  1. Add a remote name and a repository URL

git remote add origin

  1. The push command tells Git where to put our commits.

The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do

git push -u origin master

  1. Check for changes on our GitHub repository and pull down any new changes

git pull origin master

  1. what is different from our last commit by using the git diff

The HEAD is a pointer that holds your position within all your different commits. By default HEAD points to your most recent commit.

git diff HEAD

run git diff with the –staged option to see the changes you just staged

git diff –staged

  1. You can unstage files by using the git reset

git reset [file name]

Files can be changed back to how they were at the last commit by using the command: git checkout —

git checkout — [file name]

  1. You can create a copy (aka. branch) of the code so you can make separate commits to it.

git branch [branch name]

  1. You can switch branches using the git checkout command

git checkout [branch name]

13 You can remove files by using the git rm command which will not only remove the actual files from disk, but will also stage the removal of the files

git rm ‘*.txt’

  1. After you finish the changes switch back to master and merge your changes from the branch into the master branch

git checkout master

git merge [branch name]


  1. You can use git branch -d [branch name] to delete a branch

git branch -d [branch name]

  1. All that’s left for you to do now is to push everything you’ve been working on to your remote repository, and you’re done

git push

global commands :

  • Set the user name across all repos

git config –global “user name”

  • Find who changed what in a file

git blame [file name]

  • Set git dad

git config –global ‘!curl && echo && git add’