Update: This blog was written about two years before Github dropping the so-called divisive "master" terminology, and renamed it to "main". But I'm not going to change anything here. I just want to keep the original context as is. Please read master as main. After all this while I still could not remember how to do git right, and I kept on coming back to this blog, each and every time I wanted to update a Github repo. If you are careless then you may encounter "fatal: Couldn't find remote ref master", and Google will lead you into a pit of old and unrelated discussions.
I use git with Github and mostly to update the master. What the heck is the master? A copy in my laptop or a copy in Github? Git has a plethora of confusing terms. I believe the designers chose to confuse themselves so that others will too. It is important that only a small number of users know how to use git so that they remain to be the masters of git.
I write this post when I am in a middle of a git process. I forgot how I started it. I was focusing on writing my codes.
Step 1 - Create a Github Repository
It is a repository of a master code. Why must the master be in the repository? Should a repository only hold a copy of the master? Anyway, a master is not really a master. It can be copied into many repositories. Anyway, the one on Github is the master. The one on my computer is a local copy.So, the one on my laptop is not really the master even though most major features started there. Even if I start coding on my laptop prior to creating the Github repo I have to consider the copy I started with is not the master. The one I have is called the local working copy. Why is it make sense?
If I started coding directly on Github or initially uploaded my original code into a repository then I need to clone it into my computer:
$ git clone https://github.com/MY-USERNAME/MY-REPOSITORY
A repository folder containing the cloned code will be created on my computer.Use git status
At least with git status I have some idea regarding the copy in my laptop. Most tutorials ignore this.Step 2 - Always Pull from the Github Master into My Laptop
Before touching anything on my local copy I have to pull the master first:$ git pull origin master
Somehow the Gihub master may have changed while I am coding on my laptop. Maybe I updated the README.md file. So, I must make sure I have the latest master.Note: At this moment I forgot how I created the copy in my laptop. Did I clone Github? Or did I run git init with git remote add origin <server>? I will get back to this when I create a new project. See, how forgettable it can be.
Okay. I first created a project on my notebook. I worked on it until it gained a reasonable shape that I thought it will be viable. Then, I created a Github repo for the project. In my local project folder, I ran git init followed with git remote add origin <server>. Then I ran git pull origin master before I added the project files into the repo for initial commit.
Step 3 - Add File
$ git add <filename>
Add my code file into git INDEX each time before commit. But the file doesn't get anywhere useful yet. I need few other steps. I don't want to comment on INDEX or stage.I can use wild-cut for all edited files:
$ git add *
Step 4 - Commit
$ git commit -m "Commit message"
It commited to the HEAD in my laptop. But not to the master in Github yet. I don't need to know what HEAD is, the branches and the trees. And I don't care as long as it works. Maybe I need to know it when my code becomes big and complex. Why must I make my code to become big and complex? Is git promoting big and complex coding?Step 5 - Push Back to the Github Master
$ git push origin master
Now my code becomes the master in Github. And I can go back to coding.Update: You may need to move the master branch to 'main' before pushing it back to Github.
$ git branch -m master main
Comments
Post a Comment