Site icon UnixArena

Git for beginners – Zero to Hero – Basic Operations

Git is a version control system similar to subversion. It is primarily used for source code management in software development. It can be used to keep track of changes in any set of files. With Git, users can track changes to files over time, revert back to earlier versions of the files, and collaborate with other users on developing and editing the files. Git stores this information in a data structure called a repository, which contains the full history of all changes to the files.

Git – Keeping track of the code

Git is a distributed version control system, which means that each copy of the repository is a complete version control system with the full history of the project. This allows users to work offline and collaborate without the need for a central server. Git is a powerful tool that is widely used in the software development community and is available for free. It is supported on a wide range of operating systems and can be used with various programming languages.

Git repository:

A Git repository is a central location where you store and manage the files for a particular project. It contains the full history of all the changes made to the files in the repository, as well as information about who made the changes and when they were made.

Git – Repository – Synchronous Code

A Git repository can be hosted on a central server or stored locally on your computer. In the above picture, developers write the code locally and pushed to the Git repository to synchronous the code with others. When you create a new repository, you initialize it with a “master” branch, which is the main branch where all changes to the files are made. You can then create additional branches for specific features or tasks, and merge them back into the master branch when they are complete.

How to create a new Git repository in Github?

GitHub is a free platform for setup your public personal repository to store the codes. GitHub enterprise is paid version available to large organizations. To create your first GitHub repository, you need to sign-up at GitHub.com

1. Login to GitHub.com and navigate to repositories.

2. Click on “New” to create a repository.

New repository – github

2. Enter the repo name. You have the option to select repository type – public vs private. You can initialize the repository by adding a “README” file to it.

git create repository

Once the repository is created, it will have only the “main” branch associated with it.

Git Branch:

In Git, a branch is a separate line of development in a repository. When you create a new branch, you create a copy of the code at a particular point in time, and you can make changes to the code in the branch without affecting the main branch. This allows you to work on new features or bug fixes in isolation, and then merge your changes back into the main branch when they are ready to be released.

git branch

How to create a new Git branch?

1. Login to GitHub and open the repository where you need to create new branches. Click on “branch” to list the branches and create a new branch.

GitHub – Branch

2. Click on “New branch” to create a new git branch. Enter the new branch name and select the branch source to clone the contents from it.

New GIT branch – GitHub

At this point, Branch “main” = “frank”.

Git Tools:

Here are some common tools that can be used with Git:

Git basic operations:

There are many different git operations you can perform using the command line. Here is the list of basic git operations. I tried to provide the git operations in sequence starting from initializing the repository, adding new files, and new commits, and pushing to a remote repository.

1. git init: Initialize a new git repository

$ git init app1
Initialized empty Git repository in C:/Users/Lingesh/UnixArena/app1/.git/

$ ls -lrta app1
total 4
drwxr-xr-x 1 Lingesh 197121 0 Jan  6 21:04 ../
drwxr-xr-x 1 Lingesh 197121 0 Jan  6 21:04 ./
drwxr-xr-x 1 Lingesh 197121 0 Jan  6 21:04 .git/

2. git add: Add files to the staging area. I have created a new file called “README.md” and added to the staging area.

$ cd app1/
$ vi README.md
$ cat README.md
<h1>Welcome to UnixArena</h1>
$ git add -A

3. git commit: Save changes to the local repository.

$ git commit -m "Adding README.md"
[master (root-commit) fe9f1a1] Adding README.md
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

4. git remote add: To set the origin remote for a git repository. For example, if you have a git repository on your local machine and you want to connect it to a remote repository on GitHub, you can use the following command:

$ git remote add origin https://github.com/UNIXARENA/app1.git

app1 is an existing repository in GitHub.

5. git push: Send changes to a remote repository

$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 257 bytes | 128.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: This repository moved. Please use the new location:
remote:   https://github.com/UnixArena/app1.git
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/UnixArena/app1/pull/new/master
remote:
To https://github.com/UNIXARENA/app1.git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.

Let’s make some new commits on the GitHub master branch on the portal directly (remote). I have added the new text “Remote commit #1” on the README.md file.

Add remote commit – GitHub

Here you can see the new commit.

New commits – on Remote

6. git fetch: Downloads new data from a remote repository. It retrieves information about new commits, branches, and tags from the remote repository but it does not merge those changes into the local repository.

$ git fetch
remote: Enumerating objects: 22, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 20 (delta 2), reused 11 (delta 2), pack-reused 0
Unpacking objects: 100% (20/20), 2.82 KiB | 41.00 KiB/s, done.
From https://github.com/UNIXARENA/app1
   fe9f1a1..0f98c53  master     -> origin/master
 * [new branch]      main       -> origin/main

$ git branch
* master


$ cat README.md
<h1>Welcome to UnixArena</h1>

7. git diff: command to compare the local branches to the remote branches. Here you can see that the origin/master has a new commit.

$ git diff master origin/master
diff --git a/README.md b/README.md
index dfe7312..06b6246 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,3 @@
 <h1>Welcome to UnixArena</h1>
+
+Remote commit #1

8. git merge: Merge changes from one branch into another. I have merged the new commits into the local master branch from remote.

$ git merge master origin/master
Updating fe9f1a1..0f98c53
Fast-forward
 README.md | 2 ++
 1 file changed, 2 insertions(+)


$ git diff master origin/master


$ cat README.md
<h1>Welcome to UnixArena</h1>

Remote commit #1

9. git pull: It is used to retrieve new data from a remote repository and immediately merge it into the local repository. It performs “git fetch” + “git merge” in one go. Let me add a new commit to the remote repo.

$ git branch
* master

$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 670 bytes | 67.00 KiB/s, done.
From https://github.com/UNIXARENA/app1
   0f98c53..686412c  master     -> origin/master
Updating 0f98c53..686412c
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

$ cat README.md
<h1>Welcome to UnixArena</h1>

Remote commit #1
Remote commit #2

$

10. git checkout: Switch to a different branch. Once you have checkout the other branch, you can add new commits and push to remote.

$ git branch
  main
* master

$ git checkout main
Switched to branch 'main'

$ git branch
* main
  master

You can also create a new branch using “-b” flag.

$ git checkout -b frank master
Switched to a new branch 'frank'

$ git branch
* frank
  main
  master

11. git branch: List, create or delete branches.

To list all the branches locally and remotely.

$ git branch -a
* frank
  main
  master
  remotes/origin/main
  remotes/origin/master

To create a new branch locally,

$ git branch rahul

Lingesh@DESKTOP-CNO710F MINGW64 ~/UnixArena/app4 (frank)
$ git branch -a
* frank
  main
  master
  rahul
  remotes/origin/main
  remotes/origin/master

To push the locally created branch to the remote repo.

$ git push origin rahul
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: This repository moved. Please use the new location:
remote:   https://github.com/UnixArena/app1.git
remote:
remote: Create a pull request for 'rahul' on GitHub by visiting:
remote:      https://github.com/UnixArena/app1/pull/new/rahul
remote:
To https://github.com/UNIXARENA/app1.git
 * [new branch]      rahul -> rahul

To delete the branch locally & remotely.

$ git branch -d rahul
Deleted branch rahul (was 686412c).

Lingesh@DESKTOP-CNO710F MINGW64 ~/UnixArena/app4 (frank)
$ git push origin --delete rahul
remote: This repository moved. Please use the new location:
remote:   https://github.com/UnixArena/app1.git
To https://github.com/UNIXARENA/app1.git
 - [deleted]         rahul

12. git stash: Stash changes in a temporary area. It allows you to switch between branches without losing your work.

$ git branch
* frank
  main
  master

$ vi README.md
$ cat README.md
<h1>Welcome to UnixArena</h1>

Remote commit #1
Remote commit #2

Local commit for #Stash exmaple

$ git stash
Saved working directory and index state WIP on frank: 686412c Update README.md

Let me switch to the master branch and add some commits.

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ vi README.md

$ git add .
$ git commit -m "New Commit on Master Branch"
[master 1954ba2] New Commit on Master Branch
 1 file changed, 2 insertions(+)

$ git push origin origin/master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: This repository moved. Please use the new location:
remote:   https://github.com/UnixArena/app1.git
To https://github.com/UNIXARENA/app1.git
 * [new reference]   origin/master -> origin/master

$

Let me switch to branch “frank” where I have a stash.

$ git checkout frank
Switched to branch 'frank'

$ git stash list
stash@{0}: WIP on frank: 686412c Update README.md

$ git add .
$ git stash apply stash@{0}
On branch frank
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   README.md

$ git commit -m "Stash commit"
[frank 3ae775f] Stash commit
 1 file changed, 2 insertions(+)

$ git push origin frank
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 325 bytes | 162.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: This repository moved. Please use the new location:
remote:   https://github.com/UnixArena/app1.git
remote:
remote: Create a pull request for 'frank' on GitHub by visiting:
remote:      https://github.com/UnixArena/app1/pull/new/frank
remote:
To https://github.com/UNIXARENA/app1.git
 * [new branch]      frank -> frank

Lingesh@DESKTOP-CNO710F MINGW64 ~/UnixArena/app4 (frank)
$ cat README.md
<h1>Welcome to UnixArena</h1>

Remote commit #1
Remote commit #2

Local commit for #Stash exmaple

$

12. git clone: Clone an existing repository.

$ git clone https://github.com/UNIXARENA/app1.git
Cloning into 'app1'...
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 29 (delta 3), reused 17 (delta 2), pack-reused 0
Receiving objects: 100% (29/29), done.
Resolving deltas: 100% (3/3), done.

To clone specific branches, use “-b branch_name

$ git clone -b frank https://github.com/UNIXARENA/app1.git
Cloning into 'app1'...
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 29 (delta 3), reused 17 (delta 2), pack-reused 0
Receiving objects: 100% (29/29), done.
Resolving deltas: 100% (3/3), done.

We have a walk-through of various basic git operations. Apart from that, there are many git commands used to manage code conflicts, Pull requests, approvals, etc..

Hope this article is informative to you.

Exit mobile version