Skip to content

Instantly share code, notes, and snippets.

@joaomneto
Forked from joninvski/Gitworkshop.md
Created January 13, 2020 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaomneto/787a73ca5c187b62d8950ca49818976f to your computer and use it in GitHub Desktop.
Save joaomneto/787a73ca5c187b62d8950ca49818976f to your computer and use it in GitHub Desktop.

How git works

Word of warning: you don't need to use the command line, but you need to know the basics. Please first know how to do things in their simplest form and only then move to magic GUIs.

Starting (or my very first two commits)

  1. It all starts with a git init
mkdir git_test && cd git_test
git init
tree -a .

It is pretty empty right now, we will see how it changes overtime.

  1. Let us create a file and check the status of the git repository.
echo "echo hello world" > file_one.sh
git status
  1. Let's add that file to the git index to tell git it will include the file in the next commit.
git add file_one.sh
git status
  1. We can now do a commit
git commit

OMG, this is vim, how do I quit?

GIT_EDITOR=emacs25-nox git commit
GIT_EDITOR=gedit git commit

git config --global core.editor "vim"

Please don't try to use learn two things at the same time. If you don't like or know vim, don't use it only for commit messages. Change your editor! https://stackoverflow.com/questions/2596805/how-do-i-make-git-use-the-editor-of-my-choice-for-commits

Back to our example, if you leave an empty line text message, git will not do the commit. But let's write a nice commit message:

Add a welcomer script that says hello

We really want to be greeted. This functionality is provided by script `file_one.sh`
that basically does an echo.

JIRA MYTEAM-1

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#	new file:   file_one.sh

Let's check

git log

Let's understand the output

commit 1c731bf21c2ba51c9355df347a7a4cb9a2feb3a5 (HEAD -> master, origin/master)
Author: Joao Trindade <trindade.joao@gmail.com>
Date:   Fri Mar 16 18:23:08 2018 +0000

...

Ups, if your name or email is wrong, let's configure it correctly:

git config --global user.name "Joao Trindade"
git config --global user.email trindade.joao@gmail.com
  1. Congrats you have your first commit
,-.
|A|
`-'

Note that A stands for the SHA of the commit we saw in git log command. A is the unique identifier for that commit.

Let's do another commit:

echo "echo hello second world" > file_one.sh
git status

git diff

The file is not indexed:

git add file_one.sh
git diff

git diff --cached

git commit
git log
  1. We now have two commits. A is the parent of B
,-.    ,-.
|A| <- |B|
`-'    `-'
  1. Let's stop for a while and think of how many "boxes" we have.

    1. workspace - File system we are working on
    2. index - Lines that will be commited
    3. local repository - Where we store the commits
    4. remote repository - Where other people see your commits, and you see them

Remote repository

Let's add a remote repository

git push git@github.com:joninvski/git_tutorial.git master:master
git push git@github.com:joninvski/git_tutorial.git master:SOME_WEIRD

git remote
git remote add origin git@github.com:joninvski/git_tutorial.git
git remote
git remote show origin

git push origin master:masterABC

Branches

By default a repo starts in a "master" branch. It is just a name, there is nothing special about it.

git branch
git log

Small note: The concept of head is: "a reference to the most recent commit in the current workspace"

,-.    ,-.
|A| <- |B|
`-'    `-'
        |
       master
       head

Now let's say we have a story to work on. We go to JIRA and we notice we have story TD-1 that is to say two hellos.

Let's create a new branch (notice the naming):

git branch
git branch TD-1-say-two-hellos
git branch
git checkout TD-1-say-two-hellos
git branch

# We can do a single command
git checkout -b TD-1-say-two-hellos
,-.    ,-.
|A| <- |B|
`-'    `-'
        |
       master
       head
       TD-1-say-hello-two-hellos
  1. Let's do a commit
echo "echo hello world\nhello world" > file_one.sh
git status
git add file_one.sh
git commit
git log #notice the two branches
git log --graph #notice the two branches
,-.    ,-.             ,-.
|A| <- |B| <---------- |C|
`-'    `-'             `-'
        |               \
       master   TD-1-say-hello-two-hellos
       head
       
  1. Let's push it to the remote branch
git push origin master:TD-1-say-two-hellos

# Check github now and open a PR
  1. Review and merge the PR

  2. Now let's get back to our command line

git branch
git checkout master
git log
git fetch
git log
git log origin/master
git log master

# ,-.    ,-.             ,-.
# |A| <- |B| <---------- |C|
# `-'    `-'             `-'
#         |               \
#        master   TD-1-say-hello-two-hellos
#        head     origin/master

git pull

# ,-.    ,-.             ,-.
# |A| <- |B| <---------- |C|
# `-'    `-'             `-'
#                         \
#                 TD-1-say-hello-two-hellos
#                 origin/master
#                 master
#                 head

How to write good commit messages

A good commit message should do the following three things:

  • It helps you organize what you are developing now (like a rubber duck)
  • It helps the people that will review your code in the PR
  • It helps people that will read your code after it is merged

If it does this, then write it. Otherwise it is not necessary.

  1. For the title use the mnemonic "This commit will... "
  2. For the body think of writing what 2.1. What was before 2.2. Why it was wrong 2.3. Why did we new solution solves it 2.4. Minor explanation how solution works
Change the status code for get call leg endpoint

Endpoint Y returned 200. This was a problem because no content was sent.
We should follow the RFC and send 204.

This commit changes the return code from 200 to 204 for all cases.

JIRA TD-12

Espada: https://github.com/Talkdesk/central/commit/227236646205a3eadec4874a5c0bee3ad783b8ba Raoul: https://github.com/Talkdesk/central/commit/9f016976b158c4bb947986fc605e569f145d2518

What not to do:

Stuff

I did this

Saving because I'm logging out

https://github.com/Talkdesk/hackathon-million-dollar-call-representation/commits/master

References: [1] - https://chris.beams.io/posts/git-commit/ [2] - https://gist.github.com/robertpainsi/b632364184e70900af4ab688decf6f53 [3] - https://github.com/erlang/otp/wiki/writing-good-commit-messages [4] - http://who-t.blogspot.pt/2009/12/on-commit-messages.html

Advanced features

Git blame

In Github and in vim.

Git rebase

#                           TD-2
#                            |
#                           ,-.
#                        -- |D|
#                       /   `-'
# ,-.    ,-.          ,-.
# |A| <- |B| <------- |C|
# `-'    `-'         /`-'
#                   /    \  ,-.
#                master   - |E|
#                           `-'
#                            |
#                           TD-1



#                           TD-2
#                            |
#                           ,-.
#                        -- |D|
#                       /   `-'
# ,-.    ,-.          ,-.    |
# |A| <- |B| <------- |C|   master
# `-'    `-'          `-'
#                        \  ,-.
#                         - |E|
#                           `-'
#                            |
#                           TD-1

#
#
#
#                           ,-.      ,-.
#                        -- |D| ---- |E|
#                       /   `-'      `-'
# ,-.    ,-.          ,-.    |        |
# |A| <- |B| <------- |C|   master   TD-1
# `-'    `-'          `-'   TD-2
#
#
#                           ,-.      ,-.
#                        -- |D| ---- |E|
#                       /   `-'      `-'
# ,-.    ,-.          ,-.    |        |
# |A| <- |B| <------- |C|    |       TD-1
# `-'    `-'          `-'   TD-2    master
#
#
#

Git rebase interactive

# ,-.    ,-.      ,-.     ,-.     ,-.
# |A| <- |B| <--- |C| <-- |D| <-- |E|
# `-'    `-'      `-'     `-'     `-'
#                                  |
#                                  head
#                                  master


# ,-.    ,-.      ,-.     ,-.     ,-.
# |A| <- |B| <--- |C| <-- |D| <-- |E|
# `-'    `-'      `-'     `-'     `-'
#                  |               |
#                 head             |
#                                master

Git add --patch

Only put a subset of the diff on the index

Git bisect

Saved Legacy Central more than one time

# ,-.    ,-.      ,-.     ,-.     ,-.
# |A| <- |B| <--- |C| <-- |D| <-- |E|
# `-'    `-'      `-'     `-'     `-'
#  |                               |
#  |                               |
# good                            bad

Final references

Kudos to Bacao https://learngitbranching.js.org/ http://git.rocks/getting-started/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment