Git Labs
[vc_row][vc_column][vc_tta_accordion][vc_tta_section title=”Installing Git” tab_id=”1654158114383-80913f00-5244″][vc_column_text]https://git-scm.com/book/en/v2/Getting-Started-Installing-Git[/vc_column_text][/vc_tta_section][vc_tta_section title=”Setting up a Git repository” tab_id=”1654158114389-fbde403c-ec8e”][vc_column_text]
- Open command prompt or terminal
- Let’s create a project directory
mkdir pythonapp
- Let’s get inside the project directory
cd pythonapp
- Now, let’s read a sample file
echo "this is a Pythonapp" > app.py
- And now, let’s initialize local git repository
git init
- To checking what’s being tracked by git
git status
Right now git is not tracking changes to any files
[/vc_column_text][/vc_tta_section][vc_tta_section title=”Tracking and committing changes to Git repository” tab_id=”1654189655456-2ef3392a-f503″][vc_column_text]
- In last lab we initiated or created a git repository
- In this lab we will track the changes are done to the files inside the project folder
- Will do get commit will do a git add first which will basically add the changes inside a staging area
git add .
Inside the git add command.
represents all the files inside the current repository - Let’s check what is git tracking now
git status
- Then we’ll do a git commit which will basically commit the changes or else in simple words you can say it will create a snapshot of the changes which we have done
git commit -m "Initial commit"
-m
helps us to put the comment - Lets check is commit done proper and no changes felt to be snapshotted
git status
- Let’s do some more changes
echo "some changes added" >> app.py
- Now let’s do git add
git add .
- and then let’s look at commit
git commit -m "updated app.py code"
[/vc_column_text][/vc_tta_section][vc_tta_section title=”Finding difference in commits of Git repository” tab_id=”1654189657601-87df0aa2-3561″][vc_column_text]
- In last lab we did two commits to our git repository
- Let’s look at those two commits
git log
- Let’s compare the difference between these two commits
git diff <commit-id-1> <commit-id-2>
Usinggit diff
command followed by the commit IDs you can compare the differences between the files which has been done between 2 commits
[/vc_column_text][/vc_tta_section][vc_tta_section title=”Reverting back the changes in Git repository” tab_id=”1654189658319-778d7b6f-09df”][vc_column_text]
- In last lab be created two commits inside repository
- In this lab we will revert back to an older commit
- To revert back to an older commit
git checkout <commit-id-1>
You can put any commit ID which you want to be reverted too
[/vc_column_text][/vc_tta_section][vc_tta_section title=”Creating a repository on Github” tab_id=”1654193829333-d4e4c671-c6b3″][vc_column_text]
- Create a repository on GitHub go to github.com
- Log into your account
- Click on new repository
- Enter the repository name
- Keep the repository public for now
- Then finally click on create repository
[/vc_column_text][/vc_tta_section][vc_tta_section title=”Pushing changes to remote Git repository – Github” tab_id=”1654189659364-7de9d2d6-cea0″][vc_column_text]
- In last lab we created a GitHub repository
- In this lab will push the changes to that Github repository
- Copy the Github repository URL
- Now open command prompt and run following command
git remote add origin <url-copied-from-step-3>
- Now we will push the changes to Github
git push origin master
- Let’s verify the changes has been pushed to Github by going the repo page
[/vc_column_text][/vc_tta_section][vc_tta_section title=”Ignoring files and folders from tracking in Git repository” tab_id=”1654194115809-4cfb8cbc-1a19″][vc_column_text]Inside your project directory or git repository let’s say you have a file named as password.txt. Now. let’s say that you don’t want to commit this file or you don’t want to take snapshot of this file whenever you are doing git commit, well you can ignore this file from being committed by using something known as it ignore
- Go to your project folder or git repository
- Create a password.txt file
echo "dbpassword" > password.txt
- Create a
.gitignore
file
echo "password.txt" > .gitignore
- Check the status of your git repository
git status
You will not see any files that are not being tracked even though you have added a password file but it is not tracking that password file since you have added that password file inside get ignored[/vc_column_text][/vc_tta_section][vc_tta_section title=”Creating and doing changes on branches in Git repository” tab_id=”1654194143528-f4d69944-ec97″][vc_column_text]
- Go to your project directory or git repository
- Lets create a branch
git checkout -b development
This command will create a development and copy all files from master branch - List of all branch
git branch
- Switch across branch
git checkout master
- Now lets switch back to development branch
git checkout development
- Lets do changes and commit them
echo "Development Code" >> app.py
- Adding and committing the changes
git add .
git commit -m "Updated development code"
- You switch back to master and check app.py file it wont have development code
[/vc_column_text][/vc_tta_section][vc_tta_section title=”Merging Git branches” tab_id=”1654194163752-f597d3c3-a2c6″][vc_column_text]
- Create a project directory
mkdir pythonapp
- Initialize Git
git init
- Create a branch called as
production
git checkout -b production
- Create some file to be committed
echo "This is a python app" > app.py
echo "flask" > requirements.txt
- Add and commit the changes
git add .
git commit -m "Production Code"
- Now lets create a development branch
git checkout -b development
- Get the list of branches
git branch
- Lets do some changes in application file
echo "Adding a new feature" >> app.py
cat app.py
- Add and commit the changes
git add .
git commit -m "New Feature Added Code"
- Now switch to
production
branch
git checkout production
- Now lets merge changes in
development
branch toproduction
branch
git merge development
[/vc_column_text][/vc_tta_section][/vc_tta_accordion][/vc_column][/vc_row]