Git

Table of Contents
  1. Get­ting S­tart­ed§1
    1. Set­up Ter­mi­nal§1.1
      1. Set­up User§1.2
        1. Set­up De­fault Ed­i­tor§1.3
        2. The Nor­mal Oper­a­tions§2
          1. Clone the repos­i­to­ry.§2.1
            1. Cre­at­ing branch­es.§2.2
              1. S­ta­tus, Stag­ing files, and Com­mit­ing§2.3
                1. Merge Re­quest­s.§2.4
                  1. Fetch­ing and Re­base§2.5
                  2. Ad­vanced Us­age§3
                    1. Cre­at­ing tags§3.1
                      1. Pack­age Up a Git Repos­i­to­ry§3.2
                        1. The Mailmap file§3.3

                        Git is one of the most im­por­tan­t tool­s at y­our dis­pos­al. Code is not on­ly about what it is but al­so what is was.

                        A word of warning. Us­ing the built-in VC­S of VS Code and In­telli­J, of­ten lead­s to un­forseen con­se­quences. It is se­ri­ous­ly worth it to learn use and the com­mand line tool­s.

                        Get­ting S­tart­ed §1

                        For more in­for­ma­tion, y­ou can look at the Of­fi­cial Guide.

                        Set­up Ter­mi­nal §1.1

                        Y­ou should be able to ac­cess y­our git tool­s us­ing the ter­mi­nal. If y­ou are on Win­dows and have not in­stalled Git Bash yet, y­ou should.

                        Set­up User §1.2

                        To get s­tart­ed with git, y­ou first have to set­up y­our user.

                        $ git config --global user.name "Your Name"
                        $ git config --global user.email sXXXXXX@student.dtu.dk

                        To see that it has been con­fig­ured correct­ly y­ou should be able to run the fol­low­ing com­mand and

                        $ git config --list

                        Set­up De­fault Ed­i­tor §1.3

                        It is al­so a good idea to set­up VS Code as ed­i­tor so that y­ou can ed­it y­our mes­sages. This on­ly work­s if y­ou set­up shel­l tool­s for VS Code.

                        $ git config --global core.editor "code --wait"

                        To see that it worked y­ou can run:

                        $ git config --global -e

                        The Nor­mal Oper­a­tions §2

                        As part of y­our nor­mal de­vel­op­men­t rou­tine y­ou on­ly need 7 com­mand­s git add, git restore, git commit git switch, git fetch, git merge, and git push.

                        To learn more about this y­ou should take a look at https://learngitbranching.js.org

                        Clone the repos­i­to­ry. §2.1

                        To down­load the repos­i­to­ry (if y­ou have set­up y­our ssh-key Git­lab (§2)), y­ou can sim­ply run (where YY is the year, and XX is y­our group-num­ber):

                        $ git clone git@gitlab.gbar.dtu.dk:02325/sYY/fordel-XX.git

                        This will cre­ate a fold­er with the repos­i­to­ry in. Y­ou can nav­i­gate to the fold­er with cd

                        $ cd fordel-XX

                        Cre­at­ing branch­es. §2.2

                        When y­ou s­tart out the first thing y­ou wan­t to do is to cre­ate a fea­ture branch, this a branch in y­our git tree that track­s y­our changes.

                        $ git switch -c 'my-feature'

                        Y­ou can push this to the serv­er for back­up us­ing the fol­low­ing com­mand:

                        $ git push --set-upstream origin

                        S­ta­tus, Stag­ing files, and Com­mit­ing §2.3

                        Now y­ou are ready to work on the code. Af­ter y­ou have done a lit­tle bit of work, y­ou wan­t to check the s­ta­tus of the code:

                        $ git status

                        Now y­ou can choose what files to ad­d to y­our nex­t com­mit (called staged):

                        $ git add <file>

                        Or sim­ply every­thing:

                        $ git add -A

                        If y­ou have added to much, y­ou can run to remove that file a­gain.

                        $ git restore --staged <file>

                        And then com­mit it (if y­ou do not ad­d the -m an ed­i­tor will be opened):

                        $ git commit -m "My cool commit"

                        Af­ter this y­ou can push the branch to the serv­er.

                        $ git push

                        Merge Re­quest­s. §2.4

                        When y­ou are done with the fea­ture, or wan­t to in­clude y­our so­lu­tion in­to main y­ou open a pul­l-re­quest (see Git­lab (§1))

                        Fetch­ing and Re­base §2.5

                        Fi­nal­ly, y­ou prob­a­bly wan­t to use some of y­our group-mem­ber­s code as well. The best ap­proach is not to write to each-other­s branch­es but in­stead down­load their branch and ap­ply y­our changes on top of theirs in y­our own branch. This is called re­bas­ing.

                        First we down­load the repos­i­to­ry.

                        $ git fetch

                        Then y­ou can re­play y­our changes on-­top of for exam­ple the main branch. This pre­tend­s like y­ou al­ready knew the cur­ren­t s­tate of the main when y­ou be­gan cod­ing.

                        To get an idea of how it work­s; please take a look at https://www.youtube.com/watch?v=f1wnYdLEpgI.

                        Ad­vanced Us­age §3

                        This sec­tion con­tain­s some of the ad­vanced fea­tures of git.

                        Cre­at­ing tags §3.1

                        A great way of keep­ing track of com­mit­s across branch­es are git com­mit­s. To tag a com­mit, y­ou need to run the fol­low­ing com­mand.

                        $ git tag <your-cool-tag-name>

                        Tags are like branch­es but they do not up­date when we make changes to the repos­i­to­ry. Forexam­ple the fol­low­ing get­s y­ou back:

                        $ git switch <your-cool-tag-name> 

                        To push al­l branch­es to y­our re­mote (like Git­lab), y­ou sim­ply have to run:

                        $ git push --tags

                        Pack­age Up a Git Repos­i­to­ry §3.2

                        When it is time to hand in y­our code, y­ou can can bun­dle up y­our git repos­i­to­ry, us­ing the fol­low­ing com­mand:

                        $ git bundle create f2.bundle --all

                        This should make a f2.bundle which y­ou can hand in on­line.

                        To be sure that every­thing is correc­t, y­ou can clone the result­ing bun­dle, and run y­our ac­cep­tance test on it.

                        $ git clone -b main f2.bundle fordel-final

                        The Mailmap file §3.3

                        Some­times we com­mit have with a wrong e­mail. To stil­l get the right attri­bu­tion, we can ad­d oth­er i­denti­ties to the .mailmap file.

                        Y­our file should look like this and con­tain one line per wrong e­mail.

                        Student Name <sXXXXXX@student.dtu.dk> <old.wrong.email@example.com>
                        Student Name <sXXXXXX@student.dtu.dk> <my.other.wrong@email.com>
                        ...

                        To check that every­thing work­s please run this com­mand, and it should not con­tain an wrong e­mail.

                        git log --pretty=format:"%h (%as) - %aE / %aN : %s"

                        Check out the ful­l doc­u­men­ta­tion in https://git-scm.com/docs/gitmailmap.