web analytics

Git 101

Git Concepts

Manage projects with Repositories

Clone a project to work on a local copy

Control and track changes with Staging and Committing

Branch and Merge to allow for work on different parts and versions of a project

Pull the latest version of the project to a local copy

Push local updates to the main project

Working with Git

Initialize Git on a folder, making it a Repository

Git now creates a hidden folder to keep track of changes in that folder

When a file is changed, added or deleted, it is considered modified

You select the modified files you want to Stage

The Staged files are Committed, which prompts Git to store a permanent snapshot of the files

Git allows you to see the full history of every commit.

You can revert back to any previous commit. (previous version of a project)

Git does not store a separate copy of every file in every commit, but keeps track of changes made in each commit!

Git Commands

git –version

kanna@tekgenx:~/tgxnetwerks$ git –version

git version 2.25.1

git config –global user.name “tgxnetwerks”

Configure Git

git config –global user.email “na.bharathkanna@gmail.com”

Use global to set the username and e-mail for every repository on your computer.

If you want to set the username/e-mail for just the current repo, you can remove global

Creating Git Folder

mkdir tgxnetwerks

cd tgxnetwerks

Initialize Git

git init

Initialized empty Git repository in /Users/user/myproject/.git/

Git New Files

kanna@tekgenx:~/tgxnetwerks$ git status

On branch master

No commits yet

Untracked files:

(use “git add …” to include in what will be committed)

index.html

nothing added to commit but untracked files present (use “git add” to track)


Now Git is aware of the file, but has not added it to our repository!

Files in your Git repository folder can be in one of 2 states:

Tracked – files that Git knows about and are added to the repository

Untracked – files that are in your working directory, but not added to the repository

When you first add files to an empty repository, they are all untracked. To get Git to track them, you need to stage them, or add them to the staging environment.

Git Staging Environment

One of the core functions of Git is the concepts of the Staging Environment, and the Commit.

As you are working, you may be adding, editing and removing files. But whenever you hit a milestone or finish a part of the work, you should add the files to a Staging Environment.

Staged files are files that are ready to be committed to the repository you are working on. You will learn more about commit shortly.

git add index.html

File should be staged now.

kanna@tekgenx:~/tgxnetwerks$ git status

On branch master

No commits yet

Changes to be committed:

(use “git rm –cached …” to unstage)

new file: index.html

kanna@tekgenx:~/tgxnetwerks$

Add all files in the current directory to the Staging Environment:

git add –all

Using –all instead of individual filenames will stage all changes (new, modified, and deleted) files.

kanna@tekgenx:~/tgxnetwerks$ git status

On branch master

No commits yet

Changes to be committed:

(use “git rm –cached …” to unstage)

new file: README.md

new file: bluestyle.css

new file: index.html

Now all 3 files are added to the Staging Environment, and we are ready to do our first commit.

Note: The shorthand command for git add –all is git add -A

Git Commit

Since we have finished our work, we are ready move from stage to commit for our repo.

Adding commits keep track of our progress and changes as we work. Git considers each commit change point or “save point”. It is a point in the project you can go back to if you find a bug, or want to make a change.

When we commit, we should always include a message.

By adding clear messages to each commit, it is easy for yourself (and others) to see what has changed and when.

The commit command performs a commit, and the -m “message” adds a message

kanna@tekgenx:~/tgxnetwerks$ git commit -m “First release of tgxnetwerks”

[master (root-commit) 9d04da8] First release of tgxnetwerks

3 files changed, 26 insertions(+)

create mode 100644 README.md

create mode 100644 bluestyle.css

create mode 100644 index.html

kanna@tekgenx:~/tgxnetwerks$


kanna@tekgenx:~/tgxnetwerks$ git status

On branch master

nothing to commit, working tree clean

kanna@tekgenx:~/tgxnetwerks$

Git Commit without Stage

Sometimes, when you make small changes, using the staging environment seems like a waste of time. It is possible to commit changes directly, skipping the staging environment. The -a option will automatically stage every changed, already tracked file.

kanna@tekgenx:~/tgxnetwerks$ git status -s

M index.html

kanna@tekgenx:~/tgxnetwerks$ git status –short

M index.html

Note: Short status flags are:

?? – Untracked files

A – Files added to stage

M – Modified files

D – Deleted files

We see the file we expected is modified. So let’s commit it directly:

kanna@tekgenx:~/tgxnetwerks$ git commit -a -m “Updated index.html file with a new line”

[master 53abd41] Updated index.html file with a new line

1 file changed, 1 insertion(+)

kanna@tekgenx:~/tgxnetwerks$

Warning: Skipping the Staging Environment is not generally recommended.

Skipping the stage step can sometimes make you include unwanted changes.

Git Commit Log

kanna@tekgenx:~/tgxnetwerks$ git log

commit 53abd419526c7c0df12663f67d78d44373ac297b (HEAD -> master)

Author: tgxnetwerks na.bharathkanna@gmail.com

Date: Thu Oct 14 14:35:09 2021 +0200

Updated index.html file with a new line

commit 9d04da8a481a752a99e5bd9dfd6ec50602f4e892

Author: tgxnetwerks na.bharathkanna@gmail.com

Date: Thu Oct 14 14:22:31 2021 +0200

First release of tgxnetwerks 

Git Help

There are a couple of different ways you can use the help command in command line:

git command -help – See all the available options for the specific command

git help –all – See all possible commands

Note: If you find yourself stuck in the list view, SHIFT + G to jump the end of the list, then q to exit the view.

Working with Git Branches

In Git, a branch is a new/separate version of the main repository.

Let’s say you have a large project, and you need to update the design on it.

How would that work without and with Git:

Without Git:

Make copies of all the relevant files to avoid impacting the live version

Start working with the design and find that code depend on code in other files, that also need to be changed!

Make copies of the dependant files as well. Making sure that every file dependency references the correct file name

EMERGENCY! There is an unrelated error somewhere else in the project that needs to be fixed ASAP!

Save all your files, making a note of the names of the copies you were working on

Work on the unrelated error and update the code to fix it

Go back to the design, and finish the work there

Copy the code or rename the files, so the updated design is on the live version

(2 weeks later, you realize that the unrelated error was not fixed in the new design version because you copied the files before the fix)

With Git:

With a new branch called new-design, edit the code directly without impacting the main branch

EMERGENCY! There is an unrelated error somewhere else in the project that needs to be fixed ASAP!

Create a new branch from the main project called small-error-fix

Fix the unrelated error and merge the small-error-fix branch with the main branch

You go back to the new-design branch, and finish the work there

Merge the new-design branch with main (getting alerted to the small error fix that you were missing)

Branches allow you to work on different parts of a project without impacting the main branch.

When the work is complete, a branch can be merged with the main project.

You can even switch between branches and work on different projects without them interfering with each other.

Branching in Git is very lightweight and fast!

New Git Branch

git branch hello-world-images

kanna@tekgenx:~/tgxnetwerks$ git branch

hello-world-images

  • master

We can see the new branch with the name “hello-world-images”, but the * beside master specifies that we are currently on that branch.

checkout is the command used to check out a branch. Moving us from the current branch, to the one specified at the end of the command:

kanna@tekgenx:~/tgxnetwerks$ git checkout hello-world-images

Switched to branch ‘hello-world-images’

kanna@tekgenx:~/tgxnetwerks$ git status

On branch hello-world-images
Changes not staged for commit:

(use “git add …” to update what will be committed)

(use “git restore …” to discard changes in working directory)

modified: index.html

no changes added to commit (use “git add” and/or “git commit -a”)

kanna@tekgenx:~/tgxnetwerks$

So let’s go through what happens here:

There are changes to our index.html, but the file is not staged for commit

img_hello_world.jpg is not tracked

So we need to add both files to the Staging Environment for this branch:

kanna@tekgenx:~/tgxnetwerks$ git add –all

kanna@tekgenx:~/tgxnetwerks$ git status

On branch hello-world-images
Changes to be committed:

(use “git restore –staged …” to unstage)

modified: index.html

kanna@tekgenx:~/tgxnetwerks$

We are happy with our changes. So we will commit them to the branch:

kanna@tekgenx:~/tgxnetwerks$ git commit -m “Added image to Hello World”

[hello-world-images c34a399] Added image to Hello World

1 file changed, 2 insertions(+)

kanna@tekgenx:~/tgxnetwerks$

Using the -b option on checkout will create a new branch, and move to it, if it does not exist

kanna@tekgenx:~/tgxnetwerks$ ls

bluestyle.css img_hello_world.jpg index.html README.md

kanna@tekgenx:~/tgxnetwerks$

We can see the new file img_hello_world.jpg, and if we open the html file, we can see the code has been altered. All is as it should be.

Now, let’s see what happens when we change branch to master

git checkout master

Switched to branch ‘master’

ls

README.md bluestyle.css index.html

img_hello_world.jpg is no longer there! And if we open the html file, we can see the code reverted to what it was before the alteration.

Emergency Branch

Now imagine that we are not yet done with hello-world-images, but we need to fix an error on master.

I don’t want to mess with master directly, and I do not want to mess with hello-world-images, since it is not done yet.

So we create a new branch to deal with the emergency:

git checkout -b emergency-fix

Switched to a new branch ’emergency-fix’

Now we have created a new branch from master, and changed to it. We can safely fix the error without disturbing the other branches.

Let’s fix our imaginary error:

kanna@tekgenx:~/tgxnetwerks$ git status

On branch emergency-fix

Changes not staged for commit:

(use “git add …” to update what will be committed)

(use “git restore …” to discard changes in working directory)

modified: index.html

no changes added to commit (use “git add” and/or “git commit -a”)

kanna@tekgenx:~/tgxnetwerks$

Merge Branches

kanna@tekgenx:~/tgxnetwerks$ git checkout master

M index.html

Switched to branch ‘master’

kanna@tekgenx:~/tgxnetwerks$


kanna@tekgenx:~/tgxnetwerks$ git branch

emergency-fix

hello-world-images

  • master

kanna@tekgenx:~/tgxnetwerks$


Now we merge the current branch (master) with emergency-fix:


kanna@tekgenx:~/tgxnetwerks$ git merge emergency-fix

Already up to date.


From website:

git merge

emergency-fix Updating 09f4acd..dfa79db

Fast-forward

index.html | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)


Since the emergency-fix branch came directly from master, and no other changes had been made to master while we were working, Git sees this as a continuation of master. So it can “Fast-forward”, just pointing both master and emergency-fix to the same commit.

As master and emergency-fix are essentially the same now, we can delete emergency-fix, as it is no longer needed:

kanna@tekgenx:~/tgxnetwerks$ git branch -d emergency-fix

Deleted branch emergency-fix (was 53abd41).


Merge Conflict

Now we can move over to hello-world-images and keep working. Add another image file (img_hello_git.jpg) and change index.html, so it shows it:

kanna@tekgenx:~/tgxnetwerks$ git checkout hello-world-images

Switched to branch ‘hello-world-images’

kanna@tekgenx:~/tgxnetwerks$ git add –all

kanna@tekgenx:~/tgxnetwerks$ git commit -m “added new image to index file”

[hello-world-images 9770758] added new image to index file

2 files changed, 2 insertions(+), 2 deletions(-)

create mode 100644 img_hello_git.jpg

kanna@tekgenx:~/tgxnetwerks$


kanna@tekgenx:~/tgxnetwerks$ git checkout master

Switched to branch ‘master’

kanna@tekgenx:~/tgxnetwerks$ git merge hello-world-images

Auto-merging index.html

CONFLICT (content): Merge conflict in index.html

Automatic merge failed; fix conflicts and then commit the result.


The merge failed, as there is conflict between the versions for index.html. Let us check the status:

kanna@tekgenx:~/tgxnetwerks$ git status

On branch master

You have unmerged paths.

(fix conflicts and run “git commit”)

(use “git merge –abort” to abort the merge)

Changes to be committed:

new file: img_hello_git.jpg

new file: img_hello_world.jpg

Unmerged paths:

(use “git add …” to mark resolution)

both modified: index.html

kanna@tekgenx:~/tgxnetwerks$


This confirms there is a conflict in index.html, but the image files are ready and stagedto be committed.

So we need to fix that conflict. Open the file in our editor:

kanna@tekgenx:~/tgxnetwerks$ git add index.html

kanna@tekgenx:~/tgxnetwerks$ git status

On branch master

All conflicts fixed but you are still merging.

(use “git commit” to conclude merge)

Changes to be committed:

new file: img_hello_git.jpg

new file: img_hello_world.jpg

modified: index.html

kanna@tekgenx:~/tgxnetwerks$ git commit -m “merged with hello-world-images after fixing conflicts”

[master e37b326] merged with hello-world-images after fixing conflicts

kanna@tekgenx:~/tgxnetwerks$


Delete Branch hello-world-images

kanna@tekgenx:~/tgxnetwerks$ git branch -d hello-world-images

Deleted branch hello-world-images (was 9770758).

Leave a Comment