# Git essential

Date: 2017-08-20

My brother started a great project. I'll link to it when he finishes if he'll approve.

But he told me that he lost a few days of work because he didn't get any version control.

So I decided to write a "yet another git essential" post to make sure everything is clear to me and try and help others. 

<!--more-->

## What is git

Git is a free and open-source distributed version control system [[source]](https://git-scm.com/)

## How to use Git

1.   First we need a repository, we can build it by ourself
```shell
git init 
```
or we can use GitHub or GitLab to init it for us and then clone it

2. Checking that the Git is OK.

_It's healthy to run git status often. Sometimes things change, and you don't notice it._
```shell
git status
```
3. After any new file creation, we need to add it to the repository

Later we'll see that we can ignore some files or folders using .gitignore
```shell
git add <file>
```
Or
```shell
git add *
```

4. Run the commit command with a message describing what we've changed
```shell
git commit -m "description what we've changed."
```
5. Git's log is a journal that remembers all the changes we've committed so far, in the order we committed them
```shell
git log
```

6. Add a remote name and a repository URL
```shell
git remote add origin <http link>
```

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

The name of our remote is the origin, and the default local branch name is master. 
The -u tells Git to remember the parameters so that next time we can run ``` git push ``` and Git will know what to do.
```shell
 git push -u origin master
```
8. Check for changes on our GitHub repository and pull down any new changes
```shell
git pull origin master
```
9. 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.
```shell
git diff HEAD
```

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

10. You can unstaged files by using the git reset
```shell
git reset [file name]
```
Files can be changed back to how they were at the last commit by using the command: ``` git checkout <target> ```
```shell
git checkout [file name]
```

11. You can create a copy (aka. branch) of the code so you can make separate commits to it.
```shell
git branch [branch name]
```

12. You can switch branches using the git checkout <branch> command
```shell
git checkout [branch name]
```

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

14. After you finish the changes switch back to master and merge your changes from the branch into the master branch
```shell
git checkout master
git merge [branch name]
```

15. You can use git branch -d [branch name] to delete a branch
```shell
git branch -d [branch name]
```
16. 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
```shell
git push
```
* * *

global commands :

  * Set the user name across all repos
```shell
git config "global user.name =<user name?"
```
  * Find who changed what in a file
```shell
git blame [file name]
```
  * Set git dad
```shell
git config global alias.dad "!curl https://icanhazdadjoke.com/" && echo && git add
```
