Git: Two Sights, One Vision.
Human beings are social species that rely on cooperation to survive and thrive. As we know, collaboration lies at the heart of human lives and society. This means that we can’t work individually, and truly yes, it would never work if done, no matter how clever or good he/she is. Even a leaf without its tree would be nothing, and earth without “art” is only “eh.”
Collaboration is the key to success and creating an astounding workplace. But a collaboration wouldn’t “smoothly running,” if there are too many visions are meant to be won. So, what is a good start to collaborate efficiently? What we actually need to achieve our milestones, goals in the working environment?
Every “difference” will result in different changes. Changes must also be stored and known, so we can easily track the changes when something doesn’t go well. Changes are easily saved with Git, where what you change matters.
What is Git?
Git is a Distributed Version Control System(DVCS) that able to track changes, record what has been done, and revert to a specific version we want. Git is also allowing changes by multiple people, so that means it makes collaboration easier. Git is running locally in our system, which means changes history are stored locally too. But, the Git we’re going to talk about is the online Git. We can use online hosts Git, such as GitLab or GitHub, to store a copy of the files and their changes history.
As the definition implies, Version Control are software tools that help teams to manage changes to a file or set of files over time. Version Control systems also help teams to reduce development time and increase successful deployments, and so Git able to do those jobs anyway.
Another Day, Another Way.
In this article, I’m going to share the way we use GitLab as our online host repository for our project named Gerobak.
But, before we do the steps, we need to learn about the commands in Git.
Staging and Committing
Staging is a step before the commit process in Git.
Commit command is used to save your changes to the repository with a commit message.
A commit in Git is performed in two steps: Staging and Actual Commit.
$ git add .
$ git commit -m"[Commit Messages]"
Git status is used to find out information regarding what files are modified and what files are there in the staging area, including information about which branch are we on now.
To see the status:
$ git status
Git log will print out all the commits we have done. It usually shows the author, date, and the commit message of each commit.
To see the commit logs:
$ git log
Branch in Git is creating copies of programs or objects in development to work in parallel versions, retaining the original and working on the branch or making different changes to each. The default branch in a repository is branch master.
To create a new branch:
$ git checkout -b [branchName]
To switch between branches:
$ git checkout [branchDestination]
Merging in Git is the way of putting a forked history back together again. Merging lets you integrate branches into a single branch. For example, when you work in development branch and you want to merge it to the master. It means you will create changes in master based on changes on the development branch.
I recommend you to create a merge on the GitLab or GitHub websites because I’ve never done it with command line. But I’ll show you the command.
Assume you are in branch master.
$ git merge [branchDestination]
$ git push [yourBranchNow]
Rebase in git is a way to “modify” our existincommit history. Rebase is similar to merge, but it has a different effect. While merge operation will generate a new commit, rebase won’t. Thus, rebase produces a tidier history without any branching. But rebase should not be used if the branch is pushed and used by other teammates. Usually, rebase operation is only for tidying up local branches in our machines and have not been pushed to the Git server.
Remote in Git is a common repository that all team members use to exchange their changes. A remote typically does not provide a file tree of the project’s current state. It is like, you can give a changes to your online repository based on your local directory. For example, when you created an online repository named online_repository, you can remote it to your local folder named theRepo.
$ git init$ git remote add origin https://github.com/[username]/[new_repo]
After you initiate it, your local directory is ready to use as a remote.
Push in Git is used to upload local repository content to a remote repository. For example, when you have some changes in local, you need to push them to your online repository, so that the online repository will get the latest changes of your repository
To push, first, you need to do the Staging and Commit steps, then you can push the changes.
$ git push origin [branchName]
Pull in Git is used to fetch and download content from a remote repository and immediately update the local repository to match the content, while merging is updating the online repository based on changes from local repository.
To pull from online repository:
$ git pull origin [branchName]
The Setup Steps.
First, we need to install Git to our system. You can just simply download it here.
A New Local Repository
Create a directory to contain the project and go into the directory. Type git init
$ git init
Write some code you and then type git add .
$ git add .
Last is you can type git commit to save the changes
$ git commit -m"First Change"
A New Online repository
Basically, you just need to continue the local repository initiation steps, with some additional steps written here. This step will make your local folder able to remote the online repository in your GitLab or GitHub
So, after you finished follow the local repository steps, you need to go to GitHub or GitLab and log in to your account. Click the new repository button. You can start to initialize the repository with a README file (optional), and you need to click “Create Repository” button.
Next is, type those command using command line.
$ git remote add origin https://github.com/[username]/[new_repo]
$ git push -f origin master
Note: If you’re in online repository, don’t forget to do Pull>Staging>Commits>Push.
Clone Online Repository
You can also create the online repository directly from the GitLab or GitHub, and copy the https link and type git clone [https] in your desired directory
$ git clone https://github.com/[username]/[repository].git
Go into your online repository directory
$ cd [repository]
Here it is! You can finally add some files or make changes in your online repository.
Thanks For Reading!
Follow my medium for more articles.
Also, if you want to browse other content, here are my Social Media😁:
What Is Git & Why Should You Use It?
From web developers to app developers, Git is useful to anyone who writes code or track changes to files. So what's it…