Day 8: Basic Git & GitHub for DevOps Engineers

Day 8: Basic Git & GitHub for DevOps Engineers

What is Git?

Git is a distributed version control system widely used for tracking changes in source code during development. Let’s imagine a scenario: You’re working on a project with your classmate, and both of you are burning the midnight oil to get it done. When you make modifications, you send the file to your classmate, asking them to use it as the latest version. Later, your classmate does the same to you.

You might think, “Why not use something like Google Docs for collaboration?” But then comes the challenge: What if you need to roll back to the content from an hour ago, and you both realize that the version from an hour ago was the best? Do you rely on your impressive memory to redo it? That sounds like an insane task.

Git, in this case, comes to the rescue, allowing you to track changes, collaborate seamlessly, and easily revert to previous versions without the need for superhuman memory or redoing your work.

Version Control

Git allows developers to track their modifications to the code over time. It records a history of every change, including who made it and when.

Instead of saving each file as a separate version, Git uses the commits you make to record the changes. You can think of it as a snapshot of the code at a specific moment, and you don’t need to save each file individually.

Distributed

I assume that you have used Google Docs before. It operates in a centralized manner. In Google Docs, documents are stored on Google’s servers (in the cloud), and users access and collaborate on the same document through their web browsers. However, similar to Git, it creates copies of the content. Every developer of the codebase can have a local copy and make modifications to the code without interfering with other developers.

Why do we prefer distributed version control over centralized version control?

  1. Better collaboration: Imagine there are Developer A and Developer B. By using a distributed version control system, they both have copies of the content, enabling them to work independently without interfering with each other.
  2. Improved Speed: Since changes are made locally, developers do not need to communicate with the centralized server to make modifications. This speeds up the development process.
  3. Enhanced Security: Consider the centralized system: if the server saves all the code and files but then crashes, all the effort developers put into their work becomes meaningless.

What is Github?

GitHub is a web-based platform and hosting service designed for version control using Git. It serves as a platform where developers can collaborate on and manage software projects.

You can use GitHub to manage your code using Git’s version control capabilities. It’s an excellent platform for starting side projects with your friends or, in many cases, for companies to host and maintain their code for various services.


Tasks

install Git on your computer :

If you are using Windows, you can download the Git for Windows installer from the official website. Run the installer and follow the instructions. You can choose to use Git from the command line, or from a graphical user interface (GUI) such as Git Bash or Git GUI.

If you are using Mac , you can download the Git for Mac installer from the official website. Run the installer and follow the instructions. You can also use the Homebrew package manager to install Git. To do that, open the Terminal app and type:

brew install git        

If you are using Linux , you can use your distribution’s package manager to install Git. For example, if you are using Ubuntu, you can open the Terminal app and type:

sudo apt-get update && sudo apt-get install git        


To verify that Git is installed correctly, open the command line or terminal and type:

git - version        

You should see the version of Git that you have installed.

2-Create a free account on GitHub

To use GitHub, you need to create a free account on their website. Go to https://github.com/ and click on Sign up. Enter your username, email address, and password. You can also choose a plan for your account. The free plan allows you to create unlimited public repositories and up to three private repositories.

After you sign up, you will receive a verification email from GitHub. Click on the link in the email to verify your account. You can also set up some additional settings for your profile, such as your name, bio, avatar, and location.

Some basic commands of Git

Git has many commands that you can use to perform various tasks with your repository. Here are some of the most common ones:

  • git init: This command initializes a new repository in the current directory. It creates a hidden folder called .git that contains all the files and information related to your repository.
  • git status: This command shows the status of your repository. It tells you which files are staged, modified, untracked, or ignored by Git.
  • git add: This command adds files to the staging area, which is a temporary area where you prepare your files before committing them to the repository.
  • git commit: This command creates a new commit with the files in the staging area. A commit is a snapshot of your code at a specific point in time. You need to provide a message that describes what changes you made in the commit.
  • git log: This command shows the history of your commits. It displays information such as the commit hash, author, date, and message for each commit.
  • git branch: This command shows the list of branches in your repository. A branch is a parallel line of development that diverges from the main branch. You can also use this command to create, delete, or rename branches.
  • git checkout: This command switches between branches or commits. You can use it to move to a different branch or revert to a previous state of your code.
  • git merge: This command merges two branches together. It applies the changes from one branch to another branch, creating a new commit in the process.
  • git push: This command pushes your local commits to a remote repository. A remote repository is a copy of your repository that is hosted on another server, such as GitHub.
  • git pull: This command pulls updates from a remote repository to your local repository. It fetches and merges the changes from the remote branch to your current branch.

Create a new repository on GitHub

To create a new repository on GitHub, follow these steps:

  • Sign in to your GitHub account and click on New in the top left corner.
  • Enter a name for your repository and an optional description.
  • Choose whether you want your repository to be public or private. A public repository is visible to anyone on GitHub, while a private repository is only accessible by you and people you invite.
  • Optionally, you can select some options for your repository, such as adding a README file, choosing a license, or adding a .gitignore file.
  • Click on Create repository.

Clone it in your machine

To clone a repository from GitHub to your machine, follow these steps:

  • Go to the GitHub page of the repository that you want to clone and click on Code.
  • Copy the URL of the repository from the clipboard icon.
  • Open the command line or terminal and navigate to the directory where you want to clone the repository.
  • Type:

git clone [URL]        

Replace [URL] with the URL that you copied from GitHub.

For example:

git clone https://github.com/username/repo-name.git        

This will create a folder with the same name as the repository and download all its files and history.

Make some changes to a file in the repository and commit them using git

To make some changes to a file in the repository and commit them using git, follow these steps:

  • Open the file that you want to edit in your preferred text editor or IDE.
  • Make the changes that you want to the file and save it.
  • Open the command line or terminal and navigate to the directory of your repository.
  • Type:

git status        

This will show you which files have been modified, added, deleted, or untracked by Git.

  • To add the file to the staging area, type:

git add [file]        

Replace [file] with the name of the file that you edited.

For example:

git add index.html        

You can also use git add . to add all the files in the current directory to the staging area.

  • To create a new commit with the file in the staging area, type:

git commit -m "[message]"        

This will create a new commit with a unique hash and your message.

Git log

To view the history of your commits using git log, follow these steps:

  • Open the command line or terminal and navigate to the directory of your repository.
  • Type:

git log        

This will show you a list of all the commits in your repository, starting from the most recent one. You will see information such as the commit hash, author, date, and message for each commit.

You can also use some options to customize the output of git log, such as:

  • –oneline: This will show only the first line of each commit, which includes the hash and the message.
  • –graph: This will show a graphical representation of the branches and merges in your repository.
  • –all: This will show all the commits from all the branches in your repository.
  • –author=[name]: This will show only the commits made by a specific author. Replace [name] with the name of the author that you want to filter by.
  • –since=[date]: This will show only the commits made since a specific date. Replace [date] with a date in any format that Git can understand, such as “2 weeks ago” or “2023–01–01”.
  • –until=[date]: This will show only the commits made until a specific date. Replace [date] with a date in any format that Git can understand, such as “2 weeks ago” or “2023–01–01”.

You can combine these options to get more specific results. For example, if you want to see only the commits made by Alice in the last month on one line, you can type:

git log - oneline - author=Alice - since="1 month ago"        

Push the changes to GitHub

To push your local changes to GitHub, follow these steps:

  • Open the command line or terminal and navigate to the directory of your repository.

git push origin master        

This will push your master branch to the origin remote, which is usually your GitHub repository. You may need to enter your GitHub username and password to authenticate yourself.

you need to use a Personal Access Token (PAT) instead of your password. To add a PAT to your system, you can follow these steps:

  • Click on your profile and then click on Settings.

  • In the left sidebar, you will see the Developer settings. Please click the button.

  • Click on Personal access tokens and then click on Tokens (classic)

  • Click on Generate new token and then click on Generate new token (classic)

  • Please enter the note (it cannot be empty), set it to have no expiration, and then check the following options. Afterward, check the Generate token option.

  • Copy the token and paste it when prompted to enter the password. Now, everything works perfectly! Check in the remote repository, and you will see our newly added file.


git push origin Master          

This will create a new branch on GitHub with the same name and history as your local branch.



thank you for reading I hope this article may be helpful for you ! <3

#Mosad-rashad <3



要查看或添加评论,请登录

社区洞察

其他会员也浏览了