Git & Github

Git & Github

·

9 min read

GIT-Global Information Tracker

Tracking the history of the project is called GIT. GIT allows us to maintain the history of the project.

So to save all history you have to initialize that folder first

git init
#now your folder is initialized

By this, your repository/folder is initialized and '.git' file is created in your folder and all history will be started tracking in that folder

'.git' is a file from which all history is tracked and saved

I think you might know some basic Linux commands, If you don't know then :

Check out this blog(click here)

touch names.txt
#names.txt file is created by this

To check what you have recently changed in the folder

git status 
#check the status of the folder

Here you see the 'Untracked files' 'names.txt' (red color) file means this file history doesn't start tracking yet.

To start tracking this file type :

git add names.txt
#from here git start tracking this file
#or
git add .
#means start tracking all files those are untracked yet

When you make some changes it will notify you, Means 'GIT' took a snapshot of the file in that state.

Now this file appears in Green Color means a snapshot has been taken.

git commit -m "names.txt file added"

If you first time using GIT, You may get some errors while using this command like you haven't set up user email and username like this. To solve this :

git config --global user.email "Here Your Email"
#after this 
git config --global user.name "Your Name"
#done

Now you can commit to the file using 'GIT'.

git commit -m "names.txt file added" 
#means detail about that file you saved snapshot

Commit means when you took the snapshot of the file give a specific detail about it, So when next time you see this you will remember what you have done with this file.

git status

Now here you see the 'working tree clean' that means -

There's no one 'Untracked file', no 'tracked file' and no file to left 'commit'.

Now if you made some changes to files only after then they appear here.

vi names.txt
#an editor open by this and write something into it
cat names.txt
#displays the file content

Now You see we made some changes in the file So to check that :

git status

Now you see the file is modified (red color) means this current state of the file's snapshot hasn't been taken yet, Because we made a few changes in the file, So it notifies us like - 'hey, you made a few changes in the file'

git add .
#took the snapshot of the file again
#now it apeears in green color

Here if you have done this by mistake and you want to get back to Unstaged use -

git restore --staged names.txt
#by this you get back to the previous state

Now you see it goes back to the unstaged area. (modified in red color)

git add .
#staging the file
git commit -m "names.txt file modified"
#took a snapshot and saved to the history by this

To see the entire history of the project-

git log

Now if I deleted this file

rm -rf names.txt
#rm -rf  'remove forcefully"
#names.txt is deleted now

Now deleted file shows in 'red color'.Git maintains the history so it says -hey, names.txt has been deleted.

git add .
#now file has been staged

and the file is ready to commit :

git commit -m "names.txt file deleted"

To show the history of the project we use 'git log'. Here we know what changes have been made to the project

git log
#show all commits

Removing a commit-

If you want to remove a 'commit' you simply copy the below 'commit' id

and use the 'git reset' command -

for e.g - If you deleted the 'names.txt' file by mistake so just copy the below commit id and use the command-

git reset 8d5bf5f594efbf5349c478f2533879e76be86895

Now you see there are only two commits left

git status

Now 'deleted names.txt' below the stage(unstaged)

If we deleted this file by mistake

git add .

Now names.txt appears in 'Green color'

git stash

This means going back and sitting somewhere, Whenever i want you i bring you back

Now make a new file

touch surname.txt
git add.

Now make some changes to the 'surname.txt'

vi surname.txt
#write some thing into it 'modified file'
touch houses.txt
git add .
git status

Now I don't want to commit & not want to lose these files. So I staged them back & whenever I want them I bring them back by -

git stash

git status
#now working tree clean and nothing to commit

Now you be like where these all files have gone to bring back them all to the staging area by -

git stash pop

to send them again back to the stash area -

git stash

Now If you don't want these files and wanted to delete these files try -

git stash clear

Those changes that are not committed & are on backstage are gone

That's all about GIT.

GITHUB

GitHub is an online software development platform. It's used for storing, tracking, and collaborating on software projects. It makes it easy for developers to share code files and collaborate with fellow developers on open-source projects.

git remote -v

shows all the URLs attached to this folder

There will be no changes in the GitHub project. To make some changes

git push origin master

Ist time asks for your username and password, and after inputting the data we can push our repository to GitHub.

You can see this in GitHub also by refreshing the page

Branches-

After initializing the repository you have only one branch. But you create branches as much as you can

Here you can see there's only one branch 'master'.

Now you might be asking why we need branches. what is the use case of branches?

So whenever you are working on a project, you have to create a separate branch always because our code is not finalized and you're not only the person who is working on that project. So when our code was finalized the maintainer merge our branch to the main branch and then it appears to everyone.

Creating a new branch-

git branch feature
#feature branch is created
git checkout feature
#enter into the feature branch

'HEAD' is now on the 'feature' branch, HEAD is just a pointer that says all new commits/changes that you will be added on the 'feature' branch, Because 'HEAD' is pointed towards the feature branch now.

Now Let's do something in this branch-

touch 1.txt
#file created
git add.
git commit -m "message"
#commit on this file

Now you see there's the last commit goes to the feature branch because 'HEAD' is towards 'feature'

Get back to the main branch

git checkout master

Here you see there's no commit showing from the 'feature' branch because it is not merged yet to the 'Master' branch, By means say that is 'our code is not finalized yet'.

Let's assume our code is finalized so type-

git merge feature

By this your 'feature' branch will be merged to 'master' and all commits that only appear in the 'feature' branch will be visible in the 'master' branch also

To push all the changes(new changes)

git push origin master

Refresh the GitHub page and changes will be appearing there

git log

Now you see 'HEAD' is towards 'master' and the 'feature' branch commit also appears because the feature branch is merged to 'master' now

Working With Existing Project On GitHub -

Let's take a project on GitHub, You are not able to make changes directly on that project, Because you don't have access to that account. The ways by which you can contribute to that project are as follows-

Now fork this project(means adding a copy of the project into our account) -

  • Create a copy of that project on your account (means fork it)

  • Now you see there's your name shown on the project name before

  • Now go to the 'https' link and copy it

  • Now in Terminal use the command

git clone 'that https url'

Here you see the empty folder, Now we adding a project to it

Now a copy of the project is available on our local machine

Now the copy of that project will appear on your local machine. You cannot make changes directly to any project you have to fork it. You can make changes in the repository that you forked in your account & changes will not be reflected in the main repository until you make the PR and the maintainers merge those changes to the 'main' branch. The origin URL is the URL from where you have forked this project and is called the 'upstream URL by convention'.

git remote add upstream 'that https url from where you copied the project'
#that url from where you copied it
git remote -v 
#shows the url attached to this project/folder

Copy https URL -

This is the main project from where I forked it so add this file URL in the upstream URL.

Here you see the upstream and origin URL

NOTE: always make sure the origin URL should be the URL of the main project

Now you know that never commit to the main branch so here we make a new branch

Pull Request -

"git push origin 'Abhii', 'Abhii' is the branch here, Now your GitHub repository is updated as a local repository.

Sometimes you have to push forcefully and then use-

git push origin Abhii -f

NOTE: These changes are only visible in your repository till now. This is not visible in the 'main' repository. To do that you have to send a 'pull request'.

After this send a pull request -

Here you see our PR got merged and changes appears

Now you see all done. You make your first contribution.

NOTE ✒️ :

  • Never commit to the main branch

  • If you are working on a bug/feature/etc always create a separate branch

  • for every feature or bug create a separate branch

  • 1 PR = 1 branch

  • For any particular thing on which you are working create a branch on your local folder and make a PR from that

  • make sure your work doesn't mixed up

  • we can only open PR by a new branch

  • commits are interlinked

  • git fetch and git pull do the same thing internally

git fetch --all --prune
#prune means once they deleted also fetch
git reset --hard upstream/main
#to reset the origin master

That's the all basic about GIT and GITHUB ✒️.

I hope you guys like this ❤️

Did you find this article valuable?

Support Abhishek by becoming a sponsor. Any amount is appreciated!