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?
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:
Create a new repository on GitHub
To create a new repository on GitHub, follow these steps:
Clone it in your machine
To clone a repository from GitHub to your machine, follow these steps:
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:
git status
This will show you which files have been modified, added, deleted, or untracked by Git.
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.
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:
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:
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:
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:
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