Complete Git Tutorial for Beginners with Examples
Last Updated on December 25, 2022 by Editorial Team
Author(s): Muttineni Sai Rohith
Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.
Git is a version control system that lets us track the changes we make to our files over time. With Git we can revert to various states of our files. We can make a copy of our files and make changes to that copy and then merge these changes to the original copy.
We can install git using this officialΒ website.
Configuring git:
To verify git installation, we can open git bash and type the gitβββversion command. This shows the latest version of git installed on ourΒ PC.
git --version
The first thing we have to do is to set our username and email address. Git uses this information to identify who made changes to specific files. To set our username and email id, we can use the below commandsΒ –
git config --global user.name βOur_usernameβ
git config --global user.email βOur_emailβ
This will configure our Git environment. To check whether the configuration is succeeded, we can still check our configuration in the local machine using this commandΒ β
git config -l
We can also store credentials in the cache, Just to avoid giving them each time using the below commandΒ –
git config --global credential.helper cache
Cloning existing git repo to local –
We can clone the existing git repo into the local machine using the commandΒ –
git clone https://ourrepo
Creating and Initializing a new project in Git
Create a new folder or navigate to the folder where you want to create your first project. I have used git bash to create a folder test_calculator using the below commandsΒ β
mkdir test_calculator
cd test_calculator
Alternatively, you can Open the folder you like through File Explorer, Right click on it and select Git BashΒ Here
Now once the folder is ready, we need to initialize the project, For this, we need to run the git init command. This will tell git to start watching our files, for every change thatΒ occurs.
Now letβs add a small block of code to our Folder.
I am writing a small python program calculator.py for that, which contains a function, that will take two numbers, a and b as arguments and return theΒ result.
def add(a, b):
return a+b
a = int(input())
b = int(input())
print(add(a, b))
Now that we have added our changes to the folder, letβs check whether git has tracked our changes. This can be done using the git statusΒ command.
git status
We can see our branch name, if there are any past commits to our branch done us, And then any updated/untracked files that are not synced with theΒ git.
To add the updated changes or untracked files, we need to use the commandΒ β
git add .
This will add all the untracked changes.
Now after performing git addΒ .Β ; We can see that our change is tracked with the git. And we can see the changes that are yet to be committed.
To commit our changes, we can use this command git commit -m βfirst commitβΒ command.
git commit -m βfirst commitβ
-m is the shorthand for the message and the text inside the parentheses is the commitΒ message.
Now Our code is ready and committed. Now letβs push our code to theΒ repo.
For that to happen, letβs create a Repo in GitHub. This can be done by logging in to GitHub and clicking on Repos -> new -> Enter Repo Name and click on create. This is my Repo linkβββhttps://github.com/muttinenisairohith/test_calculator.git
Later we need to create a connection between our local repo and the remote repo in Github. For that, we can use this commandΒ –
git remote add origin https://github.com/muttinenisairohith/test_calculator.git
As we have our remote connection ready now. By default, git will point to the Master branch, but instead of the master branch, I want to push my changes to the main branch. For that, we can use the commandΒ β
git branch -b main
Now We have our repo and branch ready, Letβs push our modified code to our Branch in the Repo Created. For this, we can use the following commandΒ –
git push -u origin main
This will push our changes to the main branch, and if it is our first time, it will ask us to Login into Git. We can log in to git using our email id and password.
Git vsΒ GitHub
Generally, people think Git and Github are similar, But they are not. As said, Git is a version control system that tracks our changes to the repo and provides us various functionalities like time traveling etc., Whereas Github is an Online hosting service for Git Repositories. Similar to Azure DevOps Services, bitbucket, etc., Github helps us to store our repo in their platform and allow other developers to contribute at the same time from any location.
As we have pushed our first git project to Github, Moving further, Letβs understand the stages of a file being tracked byΒ Git.
Committed Stateβββ
A file is in the committed state when all the changes made to the file have been saved in the local repo. Files in the committed stage are files ready to be pushed to the remote repo (onΒ GitHub)
Modified stateβββ
A file in the modified state has some changes made to it, but itβs not yet saved. This means that the state of the file has been altered from its previous state in the committed state
Staged stateβββ
A file in the staged state means it is ready to be committed. In this state, all necessary changes have been made, so the next step is to move the file to the commitΒ state.
Now Moving Further letβs add a functionality subtraction to our code and save that againΒ –
def add(a, b):
return a+b
def sub(a, b):
return a-b
a = int(input())
b = int(input())
print(add(a, b))
print(sub(a, b))
As we have made some changes, letβs now see the git statusΒ –
So as we can see, Our file is in the modified state now as we didnβt save it to our local repo using gitΒ add.
Now letβs save this using the git addΒ command.
Our Code is now saved, and now this is in the staged state, which means it is ready to be committed. Letβs commit our changes to our branchΒ –
And Later push it to ourΒ Repo
Some Important commandsΒ β
Renaming files inΒ git:
git mv oldfile newfile
Using this command, we can rename the file in our local repo and, stage the changes, then add and commit theΒ changes.
Removing tracked files inΒ git:
git rm filename
Using this command, we can delete the file and stage the changes, then add and commit theΒ changes.
Git commitβββamend:
git commit --amend
It allows us to modify and add changes to the most recent commit. Fixing up a local commit with amend is great, and you can push it to a shared repository after youβve fixed it. But you should avoid amending commits that have already been madeΒ public.
Git remote
Using git remote, we can add a remote repository to our local repository using the commandΒ –
git add remote https://repo_here
We can see all the remote repositories for our local repository using the commandΒ –
git remote -v
We can get more info about the remote repository using the commandΒ –
git remote show origin
We have more topics to cover, but I think it is becoming lengthy, so letβs break our next topics into two sub-articles. You can find the linksΒ below.
All about GitHub BranchesΒ β
All About Git Branches… And Git pull vs fetch
GitHub Rebase, Merge, Stash, Revert and ResetΒ β
I hope this is helpful, as it will help me going forward with all the topics about Git in oneΒ place.
Happy Learningβ¦.. StayΒ Tunedβ¦.
Complete Git Tutorial for Beginners with Examples was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Join thousands of data leaders on the AI newsletter. Itβs free, we donβt spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI