So guys today am going to cover Git and Github as much as i can so that you can start your journey as a software engineer.
Note: am not going to go deep into git. am just going to show you how to use it in a real life project.
Documentation: if you ever want to dive deep into git here is the documentation git
so why do i need a Github you must say, well Github is like your resume this day if you maintain it right all your Github projects might get you a job one day so don't underestimate it.
Its also a way to keep all your projects in one place and maintained and many more uses.
Now lets get into the topic.
#Create a Github account
First you need to create a Github account to start using git.
Here is a link to creating a Github account Create
#Creating a New Repository
Repository tracks all changes made to files in your project, building a history over time.
To create a new Github repository go the main page, then to the top right corner of the page.
After that click on the create a new Repository then fill in the repo name, Description (optional) , add README file if you want the create Repository.
Now you will see the repo you created.
#Clone your repository
Now that you have created a Repository on the cloud you need to get(clone) it to you local machine, so that you can make changes to it.
For this we need to use the git command
git clone
and a Token key so that we can access the repo easily.
First generate a github token.
Go to the main page >> Setting >> developer setting >> Personal access tokens >> Generate new token
Now specify the expire date of the token and Generate the token
caution Don't show other people your personal token, this is a token i created for education and i will be deleting it and always keep it in a safe place.
Now to clone your repo open terminal and type this command
git clone https://{YOUR_PERSONAL_TOKEN}@github.com/{YOUR_USERNAME}/{REPO_NAME}.git
Replace all the specified areas with there respective data
#Edit the Repo and push it
Now that we know how to clone a repo we need to learn how to push the changes to Github using git.
Before we do that,we need to identify your git identity by putting in our global usename and email
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Git has three main states that your files can reside in: modified, staged, and committed.
Before pushing any changes to Github account, we have to add the modified file changes to the staging area by using the command
Fist check if the edited file is in staging area by
git status
Now add it using
git add <FILE_NAME> *to add a specific file*
git add . *to add all to staging area*
README.md file is now added to staging area to commit it use the command.
git commit -m '<commit message>
Tip: to write a good commit commit
write a good commit
There are 7 rules to writing a good commit
keep the commit line to 50 character
Begin all line with a capital letter
Don't put a period (.) at the end of the line
Separate subject from body with a blank line
Use the imperative mood in the subject line(use add instead of added, fix instead of fixed)
Use the body to explain what and why vs. how
Now we have committed it is no longer available in the staging area.
To see all the commit done we can use the command
git log
To go back to the changes you made in the back or revert a commit, just type the command.
git checkout <COMMIT_CODE_ON_THE_LOG>
Finally to push the changes to Github we use the commands
git push *to push to a single branch*
git push origin <BRANCH_NAME> *to push to a specific branch*
Don't worry if you don't know anything about branches we will get into it in the next topic.
#Branching
what is a branch? a branch is a tool used to separate a you personal work and make changes without affecting the original file as you seen in the image below we have 3 different branches little feature, main and big feature.
We use branches when we want to add a new feature or fix a bug.
some commands of branches are
git branch <BRANCH_NAME> *to create a new branch*
git branch *to view all the branches available int the repo*
git branch -D <BRANCH_NAME> *to delete the branch
git checkout <BRANCH_NAME> *to change the branch*
#Branch Merging
now that we have a new branch and we made all the fix or added the new feature and now we want to merge(add) it to the main branch. we use the command
First we need to be on the main branch, so checkout the main branch if you didn't then form that branch use the merge, to merge the branch you want.
git merge <BRANCH_YOU_WANT_TO_MERGE> *to merge the branches* git checkout <THE_BRANCH_YOU_MERGED> *to move it to the main commit*
git merge --abort *to abort a merge if there is a merge conflict*
#MERGE CONFLICT
what is a merge conflict? lets say you have two branches main and add.Now imagine if you started working on the main branch committing changes and some other person is working on add branch and making changes, when you added new changes to the main breach you didn't pass the changes you made to the add branch now what the add branch knows about the main branch is the main branch without the update you made. Now when you want to merge the add branch and the main branch, and you get a merge conflict.
please leave a comment if i made it to complex
So we solve the merge conflict as the main branch by either accepting to changes of the add branch or rejecting the once we did.
now as main branch we accept the changes by (bring his feature to mine) by manually changing the content of the main and adding the add branch feature, then committing the changes.
Example:
we have two branches main and new_branch,Both have README.md.
now we first change the main branch README.md by adding a change 1 text to README.md file then commit the main branch changes.
now from main branch we go to the new_branch and change the README.md file in the new_branch by adding a change 2 text then committing it.
well when we try to merge the new_branch from the main branch we get an error because the new_branch doesn't have the change 1 text change that was made to the main branch and it doesn't know it.
This is how merge conflict happens.
To fix this
accept the changes open vi make the changes and commit it, then try margin it now
some commands helpful for merge conflict are
git log --merge *to produce the list of commits that are causing the conflict*
git merge --abort *to returning back to the state before the merging began*
Tip: for more info youtube
Tip: for more info blog
#pulling a request
when coding if make a change from you Github account and want to push the local repo this will cause an error because the local and the cloud repo are not in-sync. to fix that you must always pull the the repo if you make a change using the gihub website .
we do that using the command:
git pull *to pull the repo form the cloud*
git pull origin <BRANCH_NAME> *to pull for a certain branch*
#.gitignore file
.gitignore is a file that Specifies intentionally untracked files to ignore.
Tip: for more info .gitignore
#update expired token
To update an expired token on the local git repo, use this command
git remote set-url origin https://[APPLICATION]:[NEW TOKEN]@github.com/[ORGANISATION]/[REPO].git
Top comments (0)