GIT - Distributed version control system. Common practices and usage.

GIT

Common practices and usage

Created by Alexey Novak / @twitanvk

What commands should I use??!!

When should I use "git lost" or "git life" or "git cherry-pick"?

Do not panic, this presentation should give you an idea what to use and when.

First time configuring

You better do it if you have not done it yet

Setting your username and email for future commits


  git config --global user.name "John Doe"
  git config --global user.email johndoe@example.com
            

Helps to find you code in history and tells everyone who wrote that amazing chunk of code

Git autocompletion

Get the script first


  cd ~
  curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
            

Add those lines to your ~/.bash_profile


  if [ -f ~/.git-completion.bash ]; then
    . ~/.git-completion.bash
  fi
            

Windows users can do all those changes through GitBash or other Linux-like consoles

This change is a pure joy for anyone who has to use git CLI

GIT Basics (branches and remotes)

From the little steps to the big git-based projects

Setting up a new directory with a project


  mkdir my-project
  ...
  cd ./my-project
  ...
  git init
  ...
  git remote add origin httplink-to-the-remote
            

Most of the time need to be done only once... unless you messed remote link

Keep track of changes in the repo


  git fetch origin
            

It does not effect your local files. Try to do it often to be aware of changes on the remote.

What remotes and branches do we have?


  git remote
  ...
  git branch
            

List existing branches and remotes


  git checkout localBranch
            

Switching between branches

Keep your local code updated with a remote


  git fetch remoteName
  ...
  git checkout localBranch
  ...
  git pull remoteName remoteBranch
            

Let me show you something more familiar ->


  git fetch origin
  ...
  git checkout master
  ...
  git pull origin master
            

We just merged latest changes from remote master branch into our local master branch

I want to create a local branch based on the existing remote branch


  git checkout -b localBranch remote/remoteBranch
            

Let me show you something more familiar ->


  git checkout -b staging origin/staging
            

You create your branch once until the moment you delete or rename it

Nope! Actually I wanted to create a branch based on a local one

Aha! Gotcha, not a big deal


  git checkout localBranch
  ...
  git checkout -b newBranch
            

Let me show you something more familiar ->


  git checkout master
  ...
  git checkout -b newFeature
            

Branch deletion


  git branch -D localBranch
            

It will remove your branch without any questions. Small d will prevent branch from deletion if its changes are not present in the master

Take a look at my Big D. It is all matter of taste and size does not matter so far if you know what you are doing.

Make only good names for your branches. Reflect your changes in the name.

Keep a separate branch for every feature or every fix.

Use branches. Do not just stick to one "dev" branch.

If not, then maybe it is time to switch back to SVN why to bother with GIT ?

GIT Basics (committing)

The process of creation

Step 0: Make a branch

If you have not worked on "this" feature/fix before then create a branch for it.

Step 1: Make changes

Cannot stress enough - check what branch are you on before making any changes

Step 2:Check status of your local branch


  git status
            

Will show you all untracked, changed, removed and created files


  git diff
            

Will show you all changes you have done. They are better be good!

Step 3a: Mark files for a commit (Add/Stage)


  git add fileName
  ...
  git add path/folder/fileName
  ...
  git add path/folder/
            

This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit.
git add documentation

Step 3b: Unmarking file (Reset)


  git reset path/folder/fileName
            

Will unstage the file so that it would not be commited


  git checkout -- path/folder/fileName
            

Will reset file changes to the last commit

Step 3c: Removing files from git. (Remove)


  git rm fileName
  ...
  git rm path/folder/fileName
  ...
  git rm path/folder/
            

Step 4: Bundle your changes? (Commit)


  git commit -m "some message"
            

"git commit -a" a pure evil or a misunderstood command?

Step 5: It is the time of sharing. (Push)

This command will push your current local branch to the remote.


  git push remoteName localBranch
            

Let me show you something more familiar


  git push origin newFeature
            

Or something even more familiar


  git push origin master
            

of course it is the easiest scenario and there are more complex usecases... we will get there

A general idea for your commits

Think of "git add" as a process of packing your items into a box ready to be sent to your friends.

While "git commit" would be a process of sealing the box.

While "git push" would be you sending.

A general idea for your commits (cont.)

You can put many items in a box. (add)

You look only into one box at a time. (commit)

You can send many boxes. (push)

Reset ALL changes


  git reset --hard
            

It will reset your local file sytem to a branch's HEAD

Maybe stash would work for you better?

The best way to learn is practice

Use GIT often. Use GIT branches.

GIT Ninjitsu (Advanced)

Scenario: Creating a new feature and pushing it to master branch

Scenario:

   origin      master: o-?
   local     * master: o

  git checkout master
            

Switch the branch to master since we want to create our new feature based on it

Scenario:

   origin      master: o-?
   local     * master: o

  git checkout master
  git fetch origin
            

Update your local stack with knowledge about remote

Scenario:

   origin      master: o-o
   local     * master: o

  git checkout master
  git fetch origin
  git pull origin master
            

Update your local master branch with changes from remote

Scenario:

   origin      master: o-o
   local     * master: o-o

  git checkout master
  git fetch origin
  git pull origin master
  git checkout -b newFeature
            

Create new branch for your feature/change

Scenario:

   origin      master: o-o-?
   local       master: o-o
             * newFeature: o-o

  make changes
  git add blah
  git commit -m "blah one more time"
  ...
            

Make and commit new changes

Scenario:

   origin      master: o-o-?
   local       master: o-o
             * newFeature: o-o-x-x-x

  git checkout master
  git fetch origin
  git pull origin master
            

Update your master one more time before push since there might be changes since the last time you updated your local stack

Scenario:

   origin      master: o-o-o
   local     * master: o-o-o
               newFeature: o-o-x-x-x

  git merge newFeature
  git push origin master
            

Merge your branch into master and push it to the remote

If there is a conflict then resolve it.

Resolution will be a separate commit

Scenario:

   origin      master: o-o-o-y
   local     * master: o-o-o-y
               newFeature: o-o-x-x-x

"y" will be a merge commit for your master which represents "x-x-x" changes

Other cool commands worth checking out

  • git stash  (allows to save your changes without a commit)
  • git cherry-pick  (allows to move specific commits from branch to branch)
  • git rebase  (just like cherry-pick but for multiple commits. Also allows to change commit messages)

Ignore files in your Project folder

Add them to .gitignore

Files must be in gitignore and untracked in order to be ignored

Ignore files in your Project folder

Example of .gitignore


  *.exe
  *.db
  Web.config
  App_Code/

  !App_code/MyImportantClass.cs
  !start.exe
            

Use ! to make exceptions

Just like in coding - keep your .gitignore file as minimal as possible

GIT GUI

Your tool to simplify your work even further...

GIT GUI cont.

Helping to visualize your ugly console commands

Well not only that to be honest ;)

  • Speeds up your work (when you understand the basics)
  • Easy to see and resolve conflicts
  • Helps to do very complicated commands like rebase or cherry-pick
  • Makes easier to view changes made by other people
  • And more...

Some apps worth to try

Great interactive tutorial in 15 minutes

GIT interactive

It is fun and educational. Do not be lazy and try it out! But it will require a github account to complete. Perfect timing to create if you do not have one. nerd

THE END

Joker with rainbow

Any questions?