Git essential

Eran Goldman-Malka · August 20, 2017

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.

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 ourself
    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.

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 folders using .gitignore

git add <file>

Or

git add *
  1. Run the commit command with a message describing what we’ve changed
    git commit -m "description what we've changed."
    
  2. Git’s log is a journal that remembers all the changes we’ve committed so far, in the order we committed them
    git log
    
  3. Add a remote name and a repository URL
    git remote add origin <http link>
    
  4. 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.

 git push -u origin master
  1. Check for changes on our GitHub repository and pull down any new changes
    git pull origin master
    
  2. 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 unstaged 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 <target>

    git checkout [file name]
    
  2. You can create a copy (aka. branch) of the code so you can make separate commits to it.
    git branch [branch name]
    
  3. 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 the 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]
    
  2. You can use git branch -d [branch name] to delete a branch
    git branch -d [branch name]
    
  3. 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 =<user name?"
    
  • Find who changed what in a file
    git blame [file name]
    
  • Set git dad
    git config global alias.dad "!curl https://icanhazdadjoke.com/" && echo && git add
    

Twitter, Facebook