Git Course

Created by: Simón Ovidio Miranda Chiri /ovidio.miranda@jalasoft.com

Agenda

Introduction

WHAT IS GIT?

“Git is a distributed version-control system for tracking changes in source code during software development.”

WHAT IS GIT?

  • A system that keep records of your changes.
  • Allows for collaborative development.
  • Allows you to know who made what changes and when.
  • Allows you to revert any changes and go back to a previous state.

http://git-scm.com/downloads

git --version
git version 2.32.0.windows.1
Your Identify:
git config --global user.name "Your Name"
git config --global user.email example@gmail.com
Editor
git config --global core.editor vim
To verify these are set:
git config --global -l
git config --global -e

Creating a Git repo

Creating a Git repo

You first need to go to that project’s directory.
cd C:my_project
Initialize Git.
git init

Git areas

Git areas

Adding a file

Create a file.
vim first.txt
With information.

          Method A
          Method B
          Method C
        

Adding a file


           
git add first.txt
Add all the modified files of your project.
git add .
git add --all
git add -A

Adding a file

Directories nested in a project.

           

           
git add *second.txt

Adding a file


           
Add changes by hunk.
git add --path
git add -p

Adding a file

Interactive interface.
git add --interactive
git add -i

Adding a file

Interactive interface.

          
          

Adding a file

Interactive interface.

          
          

Adding a file

Check the status.
git status
Give the output in the short-format.
git status --short
git status -s

Adding a file

Commit a file.
git commit -m "My first commit in this repo"

Remote repository

Git areas

Git areas

Git areas

Git areas

Git areas

Remote repository

Create the remote repository.

https://gitlab.com/projects/new

Remote repository

Add a Remote Repository.

             

               
Show information about remote.
git remote show origin

Remote repository

Pushing to a remote repository.

           

             

Remote repository

Cloning an existing Repository.

           

             

Remote repository

Pulling changes.

           

             

Branch Management

Branch Management

Branch Management

Branch Management

Branch Management

Branch Management

Branch Management

Creating new Branch.

             
git branch develop
Switch to a different branch.

           
git checkout develop
To create a new branch and switch to that branch.

           
git checkout -b feature-X

Branch Management

To merge changes from a branch into another branch
Checkout the branch you want to update.

             
git checkout develop
Then merge branches.

               
git merge feature-X

                 
git merge origin/feature-X

Branch Management

Create a branch and merge changes.


Branch Management

Create a branch and merge changes.
git checkout -b feature-X

Branch Management

Create a branch and merge changes.
git checkout -b feature-X
git checkout develop
git merge feature-X

Branch Management

Deleting a branch.

             
git branch -d feature-01
Force delete branch.

           
git branch -D feature-01

Branch Management

Renaming the current branch.

             
git branch -m feature-2
Rename another branch.

           
git branch -m feature-2 feature-02

Branch Management

List local branches.
git branch
List remote branches.
git branch -r
List all branches.
git branch -a

Extras

Extras

Change the message of the last commit.

git commit --amend -m "Update commit message"

Add changes to the last commit without changing its commit message.

git add new_file
git commit --amend --no-edit

Git aliases

Git aliases

Git aliases are a powerful tool that create shortcuts to frequently used Git commands.


           
git config --global alias.st status
git st

Extras

Ignore changes in tracked files with Git.


           

To return to normal.


             

Using aliases.

git config --global alias.unwatch 'update-index --assume-unchanged'
git config --global alias.watch 'update-index --no-assume-unchanged'
git unwatch first.txt
git watch first.txt

Extras

See the changes in the working directory.

git diff

Show name of files changed.

git diff --name-only

Show all files in conflict.

git diff --name-only --diff-filter=U

Tags

Tags

Git has the ability to tag specific points in a repository’s history as being important.

Creating a tag.

git tag -a v2.0 -m "My version 2.0"

           
git tag -a v1.0 3643c10 -m "My version 1.0"

Push the tags to the remote repository.

git push origin --tags

Tags

See local tags.

git tag
See the tag data along with the commit that was tagged.
git show v1.0

Checking out Tags.

git checkout v1.0
git checkout -b version2 v2.0.0

Deleting a tag.

git tag -d v1.0

Extras

Delete untracked directories and untracked files.
git clean -fd
Delete a remote branch.

           
Remove the branches that have already been merged in develop.
git branch --merged develop | grep -v '^\*' | xargs -n 1 git branch -d
Cleanup unnecessary files and optimize the local repository.
git gc

Extras

Viewing the Commit History
git log --oneline -5
Pretty git log
git log --oneline -5 --pretty="%C(Yellow)%h  %C(reset)%ad (%C(Green)%cr%C(reset))%x09 %C(Cyan)%an: %C(reset)%s" --date=short
Show the branches
git log --graph

Return to a previous commit

Return to a previous commit

To temporarily jump back to that commit.
git checkout 789abcd
To roll back to a previous commit while keeping the changes(without pushing).
git reset --soft HEAD^
To permanently discard any changes made in the last commit.
git reset --hard HEAD^
Revert some existing commitst(especially when those commits have been pushed to a remote repository.)
git revert HEAD^

Recovering

Recovering

git reflog -5 --date=format:'%Y-%m-%d %H:%M:%S' --format='%C(auto)%h - %an%x08 - %C(green)%ad -%C(blue)%cr -%C(reset) %gs (%s)'

You're back where you started


           
git reset --hard 2be3846

Extras

Copying a commit from one branch to another

           
git cherry-pick 2be3846
Quickly switch to the previous branch.
git checkout -

Stashing and Cleaning

Stashing and Cleaning

To quickly save your work you can use.
git stash
To apply our changes
git stash apply
If you want to recover and delete the stash.
git stash pop

Stashing and Cleaning

Save all changes and put a message to identify.
git stash save -u "Add Main page"
Show list of stashes.
git stash list
stash@{0}: On feature-page: Add Main page
To recover the changes you use the number of stash {0}.

           
git stash apply 0

Stashing and Cleaning

Remove a single stash entry from the list of stash entries.

           
git stash drop 0

Hooks

Hooks

project-name/.git/hooks

Hooks

Blocks a push to master branch.
vim .git/hooks/pre-push

          #!/bin/bash
          protected_branch='master'
          current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

          if [ $protected_branch = $current_branch ]
          then
              read -p "You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty
              echo
              if echo $REPLY | grep -E '^[Yy]$' > /dev/null
              then
                  exit 0 # push will execute
              fi
              exit 1 # push will not execute
          else
              exit 0 # push will execute
          fi
      

Hooks

Alert if you don't have the latest develop branch changes.
rm .git/hooks/pre-push
vim .git/hooks/pre-push

            #!/bin/bash
            current_branch=$(git rev-parse --abbrev-ref HEAD)
            target_branch='develop'

            git_ahead_and_behind=$(git rev-list --left-right --count origin/$target_branch...$current_branch | awk '{print ""$1"-"$2""}')
            behind=$(echo $git_ahead_and_behind | cut -f1 -d-)

            if [ $behind -gt 0 ];
            then
                echo -e "\e[31mAlert:\e[0m The '$current_branch' branch does not have the latest changes from the '$target_branch' branch."
                echo "The source branch is '$behind' commits behind the target branch".
            fi
      

Hooks

Execute the Javadoc task.
vim .git/hooks/pre-push

            #!/bin/bash
            read -p "Do you want to execute the Javadoc task? [y|n] " -n 1 -r < /dev/tty
            echo
            if echo $REPLY | grep -E '^[Yy]$' > /dev/null; then
              echo "** Executing the Javadoc task **"
              gradle javadoc
              echo "** Finished the Javadoc task **"
            else
              echo "** The javadoc task was not executed **"
            fi
      

Hooks

Team
vim .git/hooks/pre-push

            #!/bin/bash
            # Section 1.
            current_branch=$(git rev-parse --abbrev-ref HEAD)
            target_branch='develop'

            git_ahead_and_behind=$(git rev-list --left-right --count origin/$target_branch...$current_branch | awk '{print ""$1"-"$2""}')
            behind=$(echo $git_ahead_and_behind | cut -f1 -d-)

            if [ $behind -gt 0 ];
            then
              echo -e "\e[31mAlert:\e[0m The '$current_branch' branch does not have the latest changes from the '$target_branch' branch."
              echo -e "The source branch is '$behind' commits behind the target branch\n"
            fi

            # Section 2.
            read -p "Do you want to execute the Javadoc task? [y|n] " -n 1 -r < /dev/tty;
            if echo $REPLY | grep -E '^[Yy]$' > /dev/null
            then
              echo -e "\n** Executing the Javadoc task **"
              gradle javadoc
              echo -e "\n** Finished the Javadoc task **"
            else
              echo -e "\n\e[31m** The javadoc task was not executed **\e[0m"
            fi
      

Git Resources

Git Resources

You can get help from a command(add, commit, reset, etc.)
git help add

Git Resources

You can get help from a command(add, commit, reset, etc.)

				git add -h
				

Git Resources

You can get help from a command(add, commit, reset, etc.)

				
				

Git Resources

Visualizing Git

http://git-school.github.io/visualizing-git

http://onlywei.github.io/explain-git-with-d3

Git Resources

Pro Git (Second Edition), Scott Chacon and Ben Straub.

http://git-scm.com/book

Git Resources

Git Notes for Professionals

https://goalkicker.com/GitBook

Sometimes when one starts using git

Any Question?