A very very Hearty Welcome! All of you. And It’s time to bring it on! to complete the rest of the BASICs left in this tutorial of git and github.
But, this is to inform all my readers, who don’t know that this is the last but not the least Blog of mine in Learning Basics of git and github. Before going through this one It is recommended to have a visit on my previous Blogs. 1st “Tutorial (1)” and 2nd. “Tutorial (2)”. And 3rd is this one “Github to Git to Github”.
In this tutorial, we would be starting with Github.com then would switch to Terminal Git with the same Repo. Then would again switch back to Github.com. Alright? We are all set then why are we waiting for…
Open your Terminal with “ctrl + alt + t”. Open another Tab with “ctrl + shift + t”. Then if you have firefox in your system type “firefox github.com”, log in to your account and get started.
1. Create a New Repo. (named it as “LearningTest” in this tutorial), give the description, let it be public, tick the check-box of “Initialize this repository with a README” and If you want to add an existing license for your repo, you may select an available license from a drop down option given below. But for this tutorial, we are not adding it. If you want to know more about adding a license to your repo you may read THIS. Finally, hit Create Repository.
2. Create new file, give it’s name ( FirstFile in this tutorial), add some lines to it
This is the First file created in this blog after README. and soon we are going to edit it.
give commit title, description, and Hit Commit new file.
3.again Create new file, name it as SecondFile, add some lines to it
This is the second file created in this Repository after README. And soon I am going to make changes to it.
give commit title, description, and click on the second option which says “Create a new branch for this commit and start a pull request”, give a name to your new branch as “SecondBranch”. And finally hit Propose new file.
– hit Create pull request
– hit Merge pull request
– hit Confirm merge
– now go to the Repo. Front page. –You may go to the front Repo screen/page by clicking the name of your Repo shown in Bold Blue color at top-left.
Now you have 4 commits, 2 branches, 0 releases, and 1 contributor in “master” branch and just 1 commit less in “SecondBranch” branch.
— now we will copy this repo, to our machine using git and would work on it. For this bring your already open terminal in front with “alt + tab”. Switch to the other tab of terminal with “alt + 1” and move as follows.
1. move to a preferable place to test, (“~/Public/” in my case) with :cd ~/Public/ .
2. make sure you have already configured your git by :git config – – list (this must have shown you your username and email.id) if not then, configure it with
git config – – global user.name “your github user name comes here”
git config – – global user.email firstname.lastname@example.org
3. now go to your browser and copy the URL (https://github.com/GeekyShacklebolt/LearningTest in my case) of your repo with ctrl + c. Now give git command:
git clone your_repo_URL
* note:- in Terminal your ctrl + v doesn’t work instead use “shift + insert” (or ctrl + shift + v)
– If everything went alright, your repo is now Cloned (copied/downloaded) on your machine. Check it with command :ls
– and there you would see the LearningTest Directory. Which is nothing but your github repo on your local machine? On which you may work offline. So go to the repo now with command :cd LearningTest, then-
git status – – will give git’s status
git show – – will show you the last commit “Merge pull request” in my case.
git log – – will give you all the commits have done by now.
— to go out (if struck in) try typing :q or just q
git branch – – must be showing only master branch (how to bring all the branches, we’ll see soon)
ls – – must be showing the file in your repo.
4. make changes to the file first file:- nano FirstFile,
This is the First file created in this blog after README. And we have edited it.
[ctrl + o][enter][ctrl + x]
git add FirstFile
git commit -m “made first commit in git”
git show — to check the last commit
//points to ponder:-
You are now working on a repo that has been created on a remote, do you want to know what is the name of that remote? Type command git remote this will list the name of the remote of this repo. Generally named as “origin”. You not only can clone your own repos. But also you may clone other’s repos as well just the URL should be accurate. This is how your Collaboration just started.
In collaboration, it is possible that thousands of contributors would have been working on a project.
But suppose if You have been working on a project on your git offline for a long time. It is a great possibility that someone else might have done a couple of commits on the same repo. Online. Then You are absolutely cut off by those changes. Which means you need to update your repo. regularly while working offline, so that your offline project could have history updates to be parallel with the actual online project. So, how can you update those changes? You just need to pull those changes in this current repo. How? let’s work on it.
– go to github and make changes in SecondFile,
This is the second file created in this Repository after README. And I have edited this second file too.
And commit successfully.
5. then it is obvious. That these changes are not with your offline git project. Right? So let’s bring those changes (not the whole repo.) with the current outdated Repo.
git pull your_repo_URL
git pull origin — since origin = your_repo_URL but it do not work while cloning. Since cloning is the time when origin is set to your_repo_URL
//this way you can create a repo on github and bring it/or any other’s to your machine to work offline, then we can update it with pull command. But now How are you going to send these changes you made offline to the remote. let’s see.
6. now make a change: Create new file in terminal (in this repo. Only) and name it as ThirdFile. Add some text to it, save and exit the text editor.
This is the third file in this tutorial after README. Going to use it in future (if require)
[Ctrl + o][enter][ctlr +x ]
git add -A
git commit -m “added third file”
git show — make sure if it happened
git status — to make sure that working dir. is clean
7. Now you have some changes, here on your machine that are not available with the remote repo. On github. Right? So let’s take these changes from your machine to your remote(origin: github repo).
git push origin master
that is: git push to_which_remote to_which_branch by syntax
// this means git will you please send (push) my local branch (master) of my local repo to its remote (origin).
This will ask your username and password of github account. After submitting which, push operation would take place.
To make sure it happened, go to your github and check. You will find the ThirdFile that you created on the terminal is now available on github. Similarly, you can create folder and other files as well then may send them to your repo. Okay?
But if you have cloned the repo. From another person’s account. Then your username and password won’t do the trick. You have no right to push changes to other’s repo. In this case, what you may do is called to “send a pull request” to the owner of this repo. How? I don’t know… How to send a pull request directly from the terminal. What you can do is to see the manual for command git-request-pull for this: man git-request-pull.
– Now:- what if we need to bring all the branch structure of our repo. On github to our machine (since up till now we were working only on master). let’s see, in this case, we would do as follows:
a) first make sure you have other branches also available on github by: git branch -a
//here your all the branches would be listed (that are available on github) in a hierarchal manner.
Now you don’t need to pull those branches. Do
git pull – – all — might be up-to date by now
git branch -a — to see names of all the branches available
git checkout SecondBranch — any valid available branch name would do it
so, here you have started to work on SecondBranch that you may see with git branch
– Now you may make changes in this second branch and if wish, you may push back to the remote with-
git push origin SecondBranch
Points to ponder:-
Up-till now we have been working on a project that was cloned from our repo. On github. But what if we already have a project on our machine that we want to send on github. Hmm? What do you think?
In this case, our remote won’t be automatically set. We won’t have an origin predefined. Therefore we would need to set a remote with name anything you prefer (origin: most preferably) but How?
1. Suppose you have a project under folder named BlogProject.
go into in: cd BlogProject
git add -A
git commit -m “created this project as a repository”
2. go to your github account and create a new Repository with name “BlogProject” but this time do not add that README file. And Create Repository.
3. after going into this empty Repo. Copy its URL.
4. come back to the terminal and now add “remote” to this local repo. By:
git remote add origin URL_You_copied
syntax: git remote add <name: any name you want but ‘origin’ is preferable> <URL_of_Repo>
now make sure that remote is set by: git remote — it will give the name you did just set
git remote -v — it will list the URL of the remote you did just set
5. once your remote is set, you have configured your git already, you are free to push this repo to your github.
git push origin master
// this is all I could say about these “Github 2 Git 2 Github”, now you can bring your/other’s github project on your machine, can work on it, can update it (if lots of people are working on it), can send (push) it back to github. OR you can create your project on your machine and send to your github, etc.
With this, my Textual Tutorial on git and github is about to end with my words. Any query, any suggestion, any problem you face in these three consecutive blogs. Just let me know that. I would love to hear.
All welcome to my next blog and This, is GeekyShacklebolt
bidding you goodbye!