Let’s GIT Things Done!

Sarah
Pilar 2020
Published in
6 min readOct 18, 2020
Photo by Markus Spiske on Unsplash

When working on applications, people with different expertise collaborate with each other. Git helps manage small-scaled to big-scaled projects and web-applications to mobile applications. Since its introduction 2005, Git has helped a lot of developers across the world get things done. Git stores our source code in a remote repository but every team member can also have a copy of the source code in our computer. Hence, git speeds up our development.

Let’s Start!

A quick introduction to the commands that I’ll be using:

  • git init: used to initialize a working directory on your PC or laptop.
  • git remote: connect our local repository to the remote repository. We can connect our local repository to multiple repositories. We can do this by using the command git remote add origin <url> or git remote add upstream <url>.
  • git pull: update the local repository from a remote repository.
  • git push: upload contents from local repository to a remote repository.
  • git add: adds file(s) to be staged on the next commit.
  • git commit: commits the staged changes to the project history.
  • git checkout: switches to the specified branch and updates the working directory.
  • git branch: list all of the branches.

First, we initialize the repository in our local laptop or PC using git init.

Then, connect our local repository to the online repository using the alias origin. Type git remote add origin <your-url>

Next, let’s create a file that we want to commit to our remote repository. For example, I created file README.md.

Now is time to add and commit our file! We add the file first using the command git add .and we commit this change using git commit -m <your commit message>. Don’t forget to use a descriptive commit message!

Hooray! You have created your first modification of your project in your local repository. If you want to make this change visible on your remote repository, you can use git push origin <branch name>. Because currently I only have the master branch, I will push it to master.

Check your remote repository and see that it has been updated.

That’s it. You’re done! But, what if you want to create a new branch? Easy, you can use the command git checkout -b <your branch name>. For example, I want to create a new branch called staging.

I’m currently in branch staging. To switch to another branch, we can use the command git checkout <branch-name>. Let’s switch back to master.

To see all branches, we can just type git branch. This command is equal to git branch --list. The green text indicates the current branch that we’re currently working on.

One last important thing, to get the latest update from a specific branch in our remote repository, we can use the command git pull. On the example below, I pulled the latest changes from branch master.

Other Commands that You Should Know

There are some other important commands that can help your development process with git.

  • git clone: create a copy of the target repository.

For example, I want to clone my IT Project backend repository. I only have to type git clone <url> on my terminal. Simple, right?

  • git fetch: download history from the other remote branches.

On my IT project repository, I can get contents from other branches by using git fetch origin

  • git revert: undo a commit. However, it doesn’t delete any data, but creates a new commit that reverts the changes back to the commit we specified before. We don’t change the commit history.

Before reverting a commit, we have to know the commit id that we want to revert. There are 2 ways to check our commit id, from our online repository or to use git log. I’ll demonstrate it using git log because this article, of course, talks about git.

Which one is the commit id? The sequence of random characters in yellow is the the commit id. Then, we only have to execute git revert <commit id> if we want to revert commit.

  • git rebase: combine commits into a new base commit. Here’s a visualization when we want to rebase a feature branch onto the master branch:
source: https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

What we have to do is to execute these commands:

git checkout feature
git rebase master

These commands will move the entire feature branch on top of the master branch. The project history is re-written by the new commits of each commit in the original feature branch.

  • git merge: takes contents of a source branch and integrates them with a target branch. The commit history of the source branch remains the same, only the target branch is changed.

Let’s say we want to merge branch master into staging. There are 2 ways to do this:

git checkout staging
git merge master

or we can use one direct command:

git merge master staging
  • git stash: takes your uncommitted changes, saves them away for later use and then reverts them from your working copy. This command is useful on some different cases. First, when we have to fix an urgent problem or bugs but you’re currently working on something and it’s not finished yet. Instead of committing the unfinished work, you can just stash it with by executing git stash. To restore the work that we stashed we can use git stash pop.

Conclusion

We love git. Git has helped us a lot during our IT project especially because we are working together as a team. I definitely recommend you to use git!

Source:

https://www.atlassian.com/git/glossary

https://training.github.com/downloads/github-git-cheat-sheet/

https://www.atlassian.com/git/tutorials/undoing-changes?section=git-revert

https://www.atlassian.com/git/tutorials/rewriting-history?section=git-rebase

https://www.atlassian.com/git/tutorials/saving-changes/git-stash#re-applying-your-stashed-changes

https://www.freecodecamp.org/news/an-introduction-to-git-merge-and-rebase-what-they-are-and-how-to-use-them-131b863785f/

https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

--

--