Deep Dive in Git & GitHub for DevOps Engineers

ยท

4 min read

What is Git and why is it important

Git is a widely adopted and critical version control system that is extensively employed in various domains, extending beyond just software development. It allows developers to track changes to their code over time and collaborate with other developers on the same project by providing features for sharing and merging code changes. When multiple developers are working on the same project, each developer can create a separate local repository and make changes without affecting the main codebase. Git offers tools to integrate these independent changes into a unified codebase and resolve any conflicts that may arise. Some of the key reasons why Git is considered important are: Distributed VCS; Branching & Merging; Code collaboration; Backup & recovery; and Open source community.

Difference Between Main Branch and Master Branch

Main and Master Branch are both the same when it comes to functionality. It is created when you initiate a new repo. There is a subtle difference due to Master having an offensive connotation. They both are the starting point for the development of a Git repo and from there additional branches could be created.

Difference between Git and GitHub

Git is a distributed version control system that facilitates managing changes to source code, whereas GitHub is a web-based hosting service that offers a collaborative platform for hosting Git repositories, along with supplementary features for project management and collaboration.

Creating a new repository on GitHub

Once successfully logged into the GitHub account. Click on the "new" button to create your repo. Check the "Add a README file" to create a readme file in your remote repo and click on the "Create Repository" button. See below a screenshot of creating a new repo on GitHub or see the previous blog.

Difference between local & remote repo

A local repo is a copy of a VC repo that is stored on a local machine.

A remote repo is hosted on a remote server such as GitHub. This is a centralized location where developers can collaborate and share their changes.

Push from a Local to a Remote (GitHub) Repo

To do this, I have created a repo on GitHub. On my local machine, I have created a directory where I have initiated Git using the "git init" command. I then used the "git remote add origin" command to connect the local repo to GitHub repo. I have also used the "git remote -v" command to display the list of remote repos that are connected to the current local repo along with the URL. I have created a file name "push_file.txt" and used the "git add" command to track it and then used the "git commit" command to commit the file. I then used the "git push origin main" command to push from the local to the remote repo. See below the visuals.

ubuntu@ip-172-31-61-13:~/test/local_to_remote$ cd push_local
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:     git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:     git branch -m <name>
Initialized empty Git repository in /home/ubuntu/test/local_to_remote/push_local/.git/
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$ git remote add origin https://github.com/samsamarullah/local_to_remote.git
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$ ls
push_file.txt
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    push_file.txt
nothing added to commit but untracked files present (use "git add" to track)
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$ git add push_file.txt
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$ git commit -m "1st local to remote"
[master (root-commit) 8c8a78d] 1st local to remote
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 push_file.txt
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$ git branch
* master
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$ git push origin master
Username for 'https://github.com': samsamarullah
Password for 'https://samsamarullah@github.com': 
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 214 bytes | 214.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/samsamarullah/local_to_remote/pull/new/master
remote: 
To https://github.com/samsamarullah/local_to_remote.git
 * [new branch]      master -> master
ubuntu@ip-172-31-61-13:~/test/local_to_remote/push_local$

*I ran into an error here while using the "git push" command. It took me a while to figure it out. I searched on Google many times and followed the recommendations as well and however it didn't work. Finally reviewed it again and realized that my PAT expired hence the reason why I couldn't push my local repo to the remote repo. Lesson Learned!

Setting a user name and email address associated with Commits

To set the user name and email address, I will use the "git config --global" command. You can confirm by using the "git config --list" command for the user name and email address.

ubuntu@ip-172-31-61-13:~/test$ git config --global user.name samsamarullah
ubuntu@ip-172-31-61-13:~/test$ git config --global user.email sam@samarullah.com
ubuntu@ip-172-31-61-13:~/test$ git config --list
user.email=sam@samarullah.com
user.name=samsamarullah

I appreciate your busy time reading this short blog. As I continue with my journey to learn and acquire the skill set of a DevOps Engineer, I will share what I learn. Thank you.

Happy Learning!


Sam Samarullah

LinkedIn

Previous Blog

#devops #devopstools #devopsworld #git #github

ย