Git til absolutte begyndere

Hvis du er ny i programmeringsverdenen, bør læring af Git være noget oven på din prioritetsliste.

Git er et sådant værktøj, som du vil støde på dagligt som en del af dit job.

Hvad du kan forvente i dette indlæg

I dette indlæg giver jeg et overblik over Git og hvordan man kommer i gang med det.

  • Hvad er Git?
  • Terminologier forbundet med Git
  • Interagerer med Git ved hjælp af kommandolinjen

Jeg lover at forklare emnerne på den mest forenklede måde, jeg kan.

Så lad os begynde med at forstå, hvad er Git?

Git er et versionskontrolsystem.

Hvad er der så et versionskontrolsystem (VCS)?

En VCS overvåger og holder styr på alle de ændringer, der er foretaget i de filer, der overvåges af den.

Det giver også flere udviklere mulighed for at dele og arbejde sammen om det samme sæt filer uden at komme i konflikt med hinandens arbejde.

Det holder ikke kun styr på, hvilke filer der blev ændret, men det holder også styr på

  • Hvilke ændringer blev der foretaget?
  • Hvem ændrede disse?
  • Da disse ændringer blev gjort.

For at du kan dele og arbejde sammen med andre udviklere, skal du have adgang til en Git-baseret hostetjeneste.

Nogle af de populære Git-hostede tjenesteudbydere er:

  • GitHub
  • Bitbucket
  • Microsoft Visual Studio Team Services

Alle tilbyder en lignende slags funktionalitet.

Hvad er et arkiv i Git?

Et lager er en mappe, hvis indhold spores af Git. Det er også kendt som en repo i enkle vendinger.

En repo kan have flere filer og undermapper til stede i den. Normalt indeholder filerne, der findes i arkivet, kildekode.

Inden for hver repo er der en .git-mappe . Denne mappe indeholder alle de filer og mapper, der kræves af Git for at holde styr på alle ændringer, der er foretaget i filerne i denne repo.

Hvis vi sletter denne .git-mappe, identificerer Git ikke denne mappe som en repo eller sporer dens indhold.

Repo til stede på den lokale computer kaldes et lokalt lager , og lageret på en hostet Git-platform kaldes et eksternt lager .

Download og installer Git

Download og installation af Git er en ret ligetil proces.

Du kan downloade Git herfra.

Når Git er downloadet, kan du henvise til denne vejledning om, hvordan du får den installeret.

Initialisering af et Git-arkiv

Inden vi går videre og begynder at spore vores filer ved hjælp af Git, skal vi initialisere Git til den mappe, som vi vil have, at Git skal overvåge.

Enkelt sagt konverterer Git en mappe til et lager, så dets indhold kan spores af den.

For at initialisere en mappe til et Git-arkiv:

På et Windows-baseret system skal vi højreklikke på mappen (vi vil gerne blive sporet af Git) og derefter klikke på “Git Bash Here”. Dette åbner en kommandoprompt som et vindue, der giver os mulighed for at interagere med Git ved hjælp af Git-kommandoerne.

Bemærk: Når vi gerne vil interagere med Git, interagerer vi ved hjælp af Git-kommandoer gennem dette Git Bash-vindue. Bemærk også, at Git-kommandoerne ikke adskiller sig for Windows- og Unix-baserede systemer.

Inden for Git Bash-vinduet skal vi skrive kommandoen:

git init

Denne kommando initialiserer mappen. Dybest set konverterer den denne mappe til et Git-arkiv.

Som en del af denne initialiseringsproces opretter den også en .git-mappe (som er en skjult mappe) i dette lager. Dette indeholder alle de filer, der kræves af Git for at spore alle de ændringer, der er foretaget i dette lager.

Men dette er bare en normal mappe som andre mapper, vi har på systemet. I Git-terminologi henviser vi stadig til dette som et lager eller et lokalt lager, for at være mere præcis.

On a Unix-based system, we just navigate to the directory (which you would like to be tracked by Git), and run the git init command, that’s it. This converts this directory into a Git repository.

Repository Status

At any point in time if we want to see what is being tracked by Git within a repository, we can do that by typing the command below:

git status

We will be looking at this command in more detail at some point later in the post.

For now just remember, if we want to see what is being tracked within a repository by Git, we can do that using that command.

Tracking a Repository

Even though we have initialized the folder as a Git repository, its contents won’t get tracked automatically. We need to instruct Git to monitor its contents.

For at gøre det bruger vi kommandoen git add . Syntaksen for denne kommando er som vist nedenfor:

git add file [file] [file..]
Bemærk: Alt, der er lukket inden for de firkantede parenteser [], er valgfri. Dette gælder for alle Git-kommandoer, der er anført i dette indlæg.

Vi kan enten angive en enkelt fil eller flere filer, der skal spores af Git.

Hvis vi ønsker, at Git skal overvåge specifikke filer, der findes i arkivet, kan vi gøre det ved at specificere det individuelle filnavn for hver fil, vi gerne vil spore.

Hvis vi vil spore filer, der tilhører en bestemt filtype, kan vi gøre det ved at specificere dets filtypen, som vist nedenfor. Dette sporer alle de filer, der slutter med .txt-udvidelsen.

$ git add *.txt

Hvis vi vil have, at Git skal spore alle de filer, der er til stede med depotet, er syntaksen som vist nedenfor.

$ git add .

Let’s say we have the following files present within our repository:

As you can see even the .git folder has been created as part of the initialization process. Originally this folder was hidden — I had to change the folder properties to make it visible (just to show it to you all).

This is how a .git folder looks, immediately after the git init command is executed.

This is how the contents of the .git folder look after a few transactions have been done to the repository.

To check what all files are currently being tracked by Git, we can make use of the git status command:

$ git status On branch master No commits yet Untracked files: (use “git add …” to include in what will be committed) HelloWorld.html Notes.txt README.md nothing added to commit but untracked files present (use “git add” to track)

Looking at the output of the git status command, it indicates that none of the files are currently being tracked by Git.

Let’s go ahead and add these files so that they get tracked by Git.

The command for adding these files is as shown below:

$ git add HelloWorld.html Notes.txt

Now, let’s execute the git status command and check its output.

$ git status On branch master No commits yet Changes to be committed: (use “git rm — cached …” to unstage) new file: HelloWorld.html new file: Notes.txt Untracked files: (use “git add …” to include in what will be committed) README.md

As we can see, we have the HelloWorld.txt and the Notes.txt files present within the staging area that are waiting to be committed.

The README.md file isn’t being tracked at all, as we didn’t include this file within the git add command which we executed earlier.

When we executed the git add command, Git staged all the files which were specified as part of the input to this command.

Until we commit these files, Git won’t start tracking these files.

Committing Staged Files

Let’s commit these staged files by typing the command shown below.

$ git commit -m ‘Initial Commit’

git commit is the command which is used to commit any staged files, -m is used to specify the comments for this commit operation.

If we would like to view all the commit operations that have been performed, we can do it by typing the git log command, as shown below.

$ git log commit 8525b32ffcb92c731f5d04de7fe285a2d0ebe901 (HEAD -> master) Author: shahzan  Date: Sun Apr 28 01:12:20 2019 +0100 Initial Commit

Whenever any change is done to a file which is being tracked by Git, we need to re-stage those files and re-commit them again. Until those files are not re-staged and re-committed, they will be tracked by Git.

I have done some minor changes to the Notes.txt file, let’s see what Git has got to say about these changes by executing the git status command.

$ git status On branch master Changes not staged for commit: (use “git add …” to update what will be committed) (use “git checkout — …” to discard changes in working directory) modified: Notes.txt Untracked files: (use “git add …” to include in what will be committed) README.md no changes added to commit (use “git add” and/or “git commit -a”)

Looking at the above output block, it is clear that the file Notes.txt has been modified and the changes are not staged for commit.

We make use of the same git add command to re-stage the file.

$ git add Notes.txt [email protected] MINGW64 /d/Medium Post Pics/Git/Source Code (master) $ git status On branch master Changes to be committed: (use “git reset HEAD …” to unstage) modified: Notes.txt Untracked files: (use “git add …” to include in what will be committed) README.md

As you can notice from the above output block, the file has been staged and is waiting to be committed.

Again, we make use of the same git commit command to re-commit the staged file.

$ git commit -m ‘Notes.txt file updated’ [master 184fcad] Notes.txt file updated 1 file changed, 3 insertions(+), 1 deletion(-)

Let’s execute the git log command and see if the commit has been successful.

$ git log commit 184fcad4185296103cd9dba0da83520399a11383 (HEAD -> master) Author: shahzans  Date: Sun Apr 28 01:15:38 2019 +0100 Notes.txt file updated commit 8525b32ffcb92c731f5d04de7fe285a2d0ebe901 Author: shahzans  Date: Sun Apr 28 01:12:20 2019 +0100 Initial Commit

As you may notice in the above output block, both the commit operations are being displayed.

Ignoring Files

Within the repository, there may be files that hold sensitive data or log data, which we don’t want to be tracked by Git under any circumstances.

.gitignore is the file within which we can specify all the files we don’t want Git to keep track of.

$ touch .gitignore

The syntax to create this file is as shown above.

Let’s say that I do not want Git to track any file ending with the .md extension.

Before adding *.md to the .gitignore file, have a look at the output of the git status command as shown in the output block below.

$ git status On branch master Untracked files: (use “git add …” to include in what will be committed) .gitignore README.md nothing added to commit but untracked files present (use “git add” to track)

As you may notice, we have .gitignore and README.md being shown as untracked files.

After adding *.md to the .gitignore file, the git status is as shown in the output block below.

As you may notice, we now just have .gitignore being shown as an untracked file.

$ git status On branch master Untracked files: (use “git add …” to include in what will be committed) .gitignore nothing added to commit but untracked files present (use “git add” to track)

You can either specify an individual filename or a wildcard entry within the .gitignore file.

The Wrap-Up

Git is a very powerful tool and there is a lot more you could do with it, such as branching, merging, pull and push request and much more.

Just in case you’re interested in learning more about Git, here is a course that I would recommend you to enroll (affiliate link).

Before you say goodbye…

Let’s keep in touch, click here to enter your email address (Use this link if the above widget doesn’t show up on your screen).

Thank you so much for taking your precious time to read this post.