Thursday, November 21, 2024
HomeTutorialGIT TutorialGIT Tutorial: Understanding GIT and Basic Commands

GIT Tutorial: Understanding GIT and Basic Commands

Git is an excellent solution for managing various versions of the code and presenting a final version of the code.

Understanding Git and Version Control

Git was originally created by Linus Torvalds in 2005, who envisioned it as a free and open source system for managing all the versions of the source code of a project. Git saves the entire history of the code by creating a repository on the local machine(s) and online servers. Thus, a developer could easily add, commit, merge code and view the logs of the changes.

Git supports branching of code with different test branches during the development phase that are then merged into a single, final master version. The system is ideal for collaboration between teams of all sizes where the pull requests are viewable by all members. All the branches are finally merged into a master branch.

Three Tree Architecture

Git works on the following three-tree architecture:

1. Working Directory

This is the local directory where the developer develops the code and creates different versions of the code by committing the version. This adds to the version history of the code.

2. Staging Directory

Staging directory contains the committed code of the project. This branch keeps track of the changes made in the code. This state also tracks the changes in various branches and notify the developer of conflicts in the code. These conflicts are then resolved and the code could be pushed to the deployment directory.

3. Repository

Repository is the place for deploying the final code after all the merging of the code. Git creates a final copy on the local machine and also create repositories on both GitHub and Bitbucket. I personally prefer Github, as it is more developer-friendly.

Here is the visual representation of the three-tree architecture along with the commands that are used on every stage.

Basic GitHub Commands

Working with local repositories

git init

This command turns a directory into an empty Git repository. This is the first step in creating a repository. After running git init, adding and committing files/directories is possible.

Usage:

# change directory to codebase
$ cd /file/path/to/code
# make directory a git repository
$ git init

In Practice:
# change directory to codebase
$ cd /home/test/firstgit
# make directory a git repository
$ git init
Initialized empty Git repository in /home/test/firstgit/.git/

git add

Adds files in the to the staging area for Git. Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). There are a few different ways to use git add, by adding entire directories, specific files, or all unstaged files.

Usage:

$ git add <file or directory name>

In Practice:
# To add all files not staged:
$ git add .
# To stage a specific file:
$ git add index.html
# To stage an entire directory:
$ git add css

git commit

Record the changes made to the files to a local repository. For easy reference, each commit has a unique ID.

It’s best practice to include a message with each commit explaining the changes made in a commit. Adding a commit message helps to find a particular change or understanding the changes.

Usage:

# Adding a commit with message
$ git commit -m "Commit message in quotes"

In Practice:
$ git commit -m "My first commit message"
[FoxuTech 3453c3d] My first commit message
1 file changed, 0 insertions (+), 0 deletions (-)
create mode 100644 homepage/index.html

git status

This command returns the current state of the repository.

git status will return the current working branch. If a file is in the staging area, but not committed, it shows with git status. Or, if there are no changes it’ll return nothing to commit, working directory clean.

Usage:

$ git status

In Practice:
# Message when files have not been staged (git add)
$ git status
On branch FoxuTech
Untracked files:
(use "git add <file>..." to include in what will be committed)
homepage/index.html
# Message when files have been not been committed (git commit)
$ git status
On branch FoxuTech
Your branch is up-to-date with 'origin/FoxuTech'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file:   homepage/index.html
# Message when all files have been staged and committed
$ git status
On branch FoxuTech
nothing to commit, working directory clean

git config

With Git, there are many configurations and settings possible. git config is how to assign these settings. Two important settings are user user.name and user.email. These values set what email address and name commits will be from on a local computer. With git config, a –global flag is used to write the settings to all repositories on a computer. Without a –global flag settings will only apply to the current repository that you are currently in.

There are many other variables available to edit in git config. From editing color outputs to changing the behaviour of git status. Learn about git config settings in the official Git documentation.

Usage:

$ git config <setting> <command>

In Practice:
# Running git config globally
$ git config --global user.email "my@emailaddress.com"

$ git config --global user.name "Mr Fox"

# Running git config on the current repository settings
$ git config user.email "my@emailaddress.com"

$ git config user.name "Mr Fox"

git branch

To determine what branch the local repository is on, add a new branch, or delete a branch.

Usage:

# Create a new branch
$ git branch <branch_name>

# List all remote or local branches
$ git branch -a

# Delete a branch
$ git branch -d <branch_name>

In Practice:
# Create a new branch
$ git branch new_feature

# List branches
$ git branch -a
* FoxuTech
new_feature
remotes/origin/stable
remotes/origin/staging
remotes/origin/master -> origin/FoxuTech

# Delete a branch
$ git branch -d new_feature
Deleted branch new_feature (was 0842c3d).

git checkout

Handling File Deletion in the Staging

The biggest fear of any developer is the deletion of a code file by mistake. However, if you use Git, you do not have to worry! You could get the deleted file back by running the following command:

git checkout <deleted file name>. In my case, I first deleted test.php and then used git checkout test.php to get the file back. Now remember that if you really need to delete a file, you must commit the file name to the staging by using the git commit test.php. If you are deleting the file from Git, you can quickly move it to staging environment without adding it through the following command:

git rm <file name>. In my case, the command will be git rm test.php.

If you still want the file back, run the command, git reset HEAD test.php.  Your file will be back again.

More;

Usage:

# Checkout an existing branch
$ git checkout <branch_name>

# Checkout and create a new branch with that name
$ git checkout -b <new_branch>

In Practice:

# Switching to branch 'new_feature'
$ git checkout new_feature
Switched to branch 'new_feature'

# Creating and switching to branch 'staging'
$ git checkout -b staging
Switched to a new branch 'staging'

git merge

Integrate branches together. git merge combines the changes from one branch to another branch. For example, merge the changes made in a staging branch into the stable branch.

Usage:

# Merge changes into current branch
$ git merge <branch_name>

In Practice:
# Merge changes into current branch
$ git merge new_feature
Updating 3453c3d..4c0f37c
Fast-forward
homepage/index.html | 297 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 297 insertions (+)
create mode 100644 homepage/index.html

Working with remote repositories

git remote

To connect a local repository with a remote repository. A remote repository can have a name set to avoid having to remember the URL of the repository.

Usage:

# Add remote repository
$ git remote <command> <remote_name> <remote_URL>
# List named remote repositories
$ git remote -v

In Practice:
# Adding a remote repository with the name of foxutech
$ git remote add origin git@account_name.git.foxutech.com:/acccount_name/repository_name.git

# List named remote repositories
$ git remote -v
origin git@account_name.git.foxutech.com:/acccount_name/repository_name.git (fetch)
origin git@account_name.git.foxutech.com:/acccount_name/repository_name.git (push)
Note: A remote repository can have any name. It’s common practice to name the remote repository ‘origin’.

git clone

To create a local working copy of an existing remote repository, use git clone to copy and download the repository to a computer. Cloning is the equivalent of git init when working with a remote repository. Git will create a directory locally with all files and repository history.

Usage:

$ git clone <remote_URL>

In Practice:
$ git clone git@account_name.git.foxutech.com:/acccount_name/repository_name.git
Cloning into 'repository_name'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), 3.08 KiB | 0 bytes/s, done.
Checking connectivity... done.

git pull

To get the latest version of a repository run git pull. This pulls the changes from the remote repository to the local computer.

Usage:

$ git pull <branch_name> <remote_URL/remote_name>

In Practice:
# Pull from named remote
$ git pull origin staging
From account_name.git.foxutech.com:/account_name/repository_name
* branch            staging    -> FETCH_HEAD
* [new branch]      staging    -> origin/staging
Already up-to-date.

# Pull from URL (not frequently used)
$ git pull git@account_name.git.foxutech.com:/acccount_name/repository_name.git staging
From account_name.git.foxutech.com:/account_name/repository_name
* branch            staging    -> FETCH_HEAD
* [new branch]      staging    -> origin/staging
Already up-to-date.

git push

Sends local commits to the remote repository. git push requires two parameters: the remote repository and the branch that the push is for.

Usage:

$ git push <remote_URL/remote_name> <branch>
# Push all local branches to remote repository
$ git push —all

In Practice:
# Push a specific branch to a remote with named remote
$ git push origin staging
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 734 bytes | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
To git@account_name.git.foxutech.com:/acccount_name/repository_name.git
ad189cb..3453c3d  FoxuTech -> FoxuTech

# Push all local branches to remote repository
$ git push --all
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 373 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To git@account_name.git.foxutech.com:/acccount_name/repository_name.git
0d56917..948ac97  master -> master
ad189cb..3453c3d  FoxuTech -> FoxuTech

Advanced Git Commands

git stash

To save changes made when they’re not in a state to commit them to a repository. This will store the work and give a clean working directory. For instance, when working on a new feature that’s not complete, but an urgent bug needs attention.

Usage:

# Store current work with untracked files
$ git stash -u
# Bring stashed work back to the working directory
$ git stash pop

In Practice:

# Store current work
$ git stash -u
Saved working directory and index state WIP on FoxuTech: 4c0f37c Adding new file to branch
HEAD is now at 4c0f37c Adding new file to branch
# Bring stashed work back to the working directory

$ git stash pop
On branch FoxuTech
Your branch and 'origin/FoxuTech' have diverged,
and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified:   index.html
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (3561897724c1f448ae001edf3ef57415778755ec)

git log

To show the chronological commit history for a repository. This helps give context and history for a repository. git log is available immediately on a recently cloned repository to see history.

Usage:

# Show entire git log
$ git log

# Show git log with date pameters
$ git log --<after/before/since/until>=<date>

# Show git log based on commit author
$ git log --<author>="Author Name"

In Practice:
# Show entire git log
$ git log
commit 4c0f37c711623d20fc60b9cbcf393d515945952f
Author: Mr Fox <my@emailaddress.com>
Date:   Tue Oct 25 17:46:11 2016 -0500
Updating the wording of the homepage footer
commit 3453c3da3add4ebe9d7e1f2e76f015a209e1ef67
Author: Mr Fox <my@emailaddress.com>
Date:   Wed Oct 19 16:27:27 2016 -0500
My first commit message

# Show git log with date pameters
$ git log --before="Oct 20"
commit 3453c3da3add4ebe9d7e1f2e76f015a209e1ef67
Author: Mr Fox <my@emailaddress.com>
Date:   Wed Oct 19 16:27:27 2016 -0500
My first commit message

# Show git log based on commit author
$ git log --author="Mr Fox"
commit 4c0f37c711623d20fc60b9cbcf393d515945952f
Author: Mr Fox <my@emailaddress.com>
Date:   Tue Oct 25 17:46:11 2016 -0500

Updating the wording of the homepage footer

git rm

Remove files or directories from the working index (staging area). With git rm, there are two options to keep in mind: force and cached. Running the command with force deletes the file. The cached command removes the file from the working index. When removing an entire directory, a recursive command is necessary.

Usage:

# To remove a file from the working index (cached):
$ git rm --cached <file name>

# To delete a file (force):
$ git rm -f <file name>

# To remove an entire directory from the working index (cached):
$ git rm -r --cached <directory name>

# To delete an entire directory (force):
$ git rm -r -f <file name>

In Practice:
# To remove a file from the working index:
$ git rm --cached css/style.css
rm 'css/style.css'

# To delete a file (force):
$ git rm -f css/style.css
rm 'css/style.css'

# To remove an entire directory from the working index (cached):
$ git rm -r --cached css/
rm 'css/style.css'
rm 'css/style.min.css'

# To delete an entire directory (force):
$ git rm -r -f css/
rm 'css/style.css'
rm 'css/style.min.css'

 

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments