Learning GIT
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Bash means Bourne Again Shell. Ohhh, I didn't know that. 😂. Bash is a command processor that typically runs in a text window where the user types commands that cause actions....okay.
Combine the two and you get...Git Bash! Yey!
Setup
Admit it. The colors are cool right? Haha. Let me run you through the initial setup on your machine.
cd ~/.ssh is the first command to run. It basically lets you go to the .ssh directory
ls doing ls here will respond known_hosts. Ok, you don't really need to know that one so let's skip. That's more of an advanced topic.
ssh-keygen -t rse -C "yoda" This will generate your ssh key. When you do ls again, it should come back with your private and public key which you will later use on your repository.
Now. Run cat id_rsa.pub to view the contents of your public key. Ooooh, look at that cool random key, you're feeling like a real coder know aren't you?
Other important configurations for later is to setup your profile with an email and name. Below is the syntax for that:
git config --global user.email "coderkaye@github.com"git config --global user.name "coderkaye"
Now...you can create a folder anywhere and make it as your workspace. I like to put mine in the home (~) directory and just name it Workspace. That's it! You're fully setup!
Checkout, Make Changes, and Commit it Back!
Github is the remote repository. It is located in a remote place somewhere out there which can be accessed by many coders like you and me! Now, your computer is the local repository. You would usually get a copy of the files from the remote repository to your local repository (computer), do some edits, then put it back in the remote repository for everyone else to see. Cool right? So how exactly is this done?
1. First step is to get the copy of the files in Github to your computer. You do that by running:
Syntax: git clone [ssh path]
Syntax: git clone [ssh path]
Actual Command: git clone https://github.com/coderkaye/testrepo.git
$ git clone https://github.com/coderkaye/testrepo.git
Cloning into 'testrepo'...
warning: You appear to have cloned an empty repository.
Oops, wala pang laman from remote. Hehehe.
Advanced: Clone a specific branch only
Syntax: git clone --branch [branch name] [ssh path]
Actual Command: git clone --branch master https://github.com/coderkaye/testrepo.git
2. Then, go inside the folder. It will say that you are in master. Master is the default branch always, unless you specify otherwise. Wag na natin palitan muna.
3. Then try to list all the current branches in your local repository.
Syntax: git branch -a
Syempre wala pa tayong ginawang branch kaya master lang sa ngayon. (-a) pala means "all". Sana all. Hahaha!4. Let's make a new branch where we will make our changes. Usually, while working on a feature/new stuff for software, we do it on a feature branch so that we don't disturb other people's work. Since master is the master of all branches and common to everyone, we only put our changes there once we complete the work on our feature.
Syntax: git checkout -b "[feature branch name]"
Actual: git checkout -b "feature/myfeature"
This will change your working branch to "feature/myfeature" in your local repository. Now you're ready to do some work!
5. Now, I want to create a test file. Haha, sundan nyo na lang yung nasa screenshot. Basically, I made a readme file in there. It usually contains some description about the repository.
6. Now, let's put our changes to the master branch so that everyone can see. It requires a few steps:
a. git add [file] or git add --all
This will mark the files that you want to commit to master. It can be per file, or it can be all your changes in one go!
b. git commit -m "comment"
This will add a comment to the group of files you're putting in. Para madaling tandaan, pag may ibibigay ka...sure ka na ba? Ready ka na bang i-commit? If yes, add affirmation via comment.
c. git push -u origin feature/myfeature
This will push your changes to the remote repository in the feature/myfeature branch. Notice that initially, the remote repository does not have a "feature/myfeature" branch kaya mo sinabing (-u) meaning upstream papunta sa remote repository (somewhere sa cloud) and sinabi mong ilagay sa feature/myfeature na branch dun. Sa first time mo lang kailangan lagay yung "-u origin feature/myfeature". Next time, pwedeng git push ka na lang nang git push.
After that, expect to see a new branch in the remote repository (Github)! Now everyone else can see your changes!
Pull Request
Now, of course, you now want your change to be part of the master branch! Master branch being the master of all changes which eventually get's deployed in production, you should raise a Pull Request from your feature branch to the master branch. A pull request simply means you want to pull your changes to the master branch but you want others to review your code first.
To do this, in GitHub, go to Pull Requests Tab, then click on New Pull Request. Select the base as master then select the feature branch (feature/myfeature) that you're trying to merge to master. Add some title and comments, then click on Create Pull Request.
To do this, in GitHub, go to Pull Requests Tab, then click on New Pull Request. Select the base as master then select the feature branch (feature/myfeature) that you're trying to merge to master. Add some title and comments, then click on Create Pull Request.
Code Review and Merge!
Depending on the repository settings, your Pull Request can be merged to the master branch as long as it has the required minimum number of approvals. In my case, it requires 0 approvals, so the merge button immediately appears!
This is now complete. Inaantok na kasi ako, hahaha! I'll soon write about a few more advanced commands commonly used in GIT.
GIT Delete
GIT Revert
GIT Reset
GIT Squash
Resolving Conflicts in GIT
GIT Revert
GIT Reset
GIT Squash
Resolving Conflicts in GIT
I believe there's a lot more that you can do with Git, but really these are the essentials. See you soon!
Comments
Post a Comment