November 24, 2019

Git Going

Git

Git is a modern verson control system used mostly by developers. Initially authored by Linus Torvalds, git is actively being developed by many of the open-source contributors!

If you're wondering, what is a version control system(vcs); it is a piece of software which would allow you to manage your code versions. Version control software keeps track of every modification to the code in a special kind of database. Using a version control system, a group of developers can collaborate super easily.

GitHub

GitHub is a online platform where you will store codes managed by a version control system.

Git manages your code locally, in your computer. But what if you need to send your code to other developers? and they want to collaborate too?

You can't email your source code to your friend everytime you make a change. Github solves this issue. When you change something in the project and push to github, your friend could easily in their local system(pc) easily and almost instantly. And then you both have the same code version.

Setup

  • Git

    Installing git is pretty easy. It's a software, just like other thousand of them. Here, I am listing ways to install it(although you can figure it out on your own).

    • Linux

    Comes mostly pre-installed. To verify, open your terminal and run git --version. If installed, you should see a version number. If it's not installed:

    • Arch and similar: pacman -Syu git
    • Debian and Similar: apt install git
    • Or Download from git-scm
    • Windows

    Download from git-scm website, and follow the on-screen instructions after running the installer. Make sure to install git-bash as well(comes with git installer).

  • Github

    Go to https://github.com and create a new account. Once done creating the account, go to https://github.com/new and create a new repository.

Git Commands

  • Starting a new Project

    1. To start a fresh project, create a directory. Open git-bash or terminal and move to that directory. For example, if you create the directory inside 'Desktop/Projects/your-project-dir', then inside git-bash, run the this command: cd Desktop/Projects/your-project-dir.
    2. Run git init to initialize your project locally.

    Before pushing codes to a remote branch, you need to tell git who you are. To do that, run these commands with appropriate data: git config --global user.email "you@email.com" and git config --global user.name "Your name". Here, email and name could be anything. But it's good to provide the email you have used in github.

  • Uploading codes to Github

    • Add/change some codes in project-directory and run: git add . to stage all changes. Staging are is the location from where git will start tracking your changes.
    • Once you add changes, it's time to commit. Commit is a way to tell git that: 'I am done editing this code. Make it ready to push it to remote repository(i.e. github)'. Each commit is a checkpoint and could be reverted later on. Run git commit --m 'A commit message' to commit. A Commit message is a short description of what you did. It's required to provide commit message with each commi.
    • Finally, time to publish our changes to github or any such platforms. For that we need to tell git where to push it. To do that: go to github and click on the repository you created a while ago. You'll find a section called '…or push an existing repository from the command line'. Copy those commands and run inside git-bash.

    For your reference, here are the commands: git remote add origin git@github.com:USERNAME/REPO_NAME.git(for telling github where to push and pull) and git push -u origin master(for pushing code to master)

  • Collaborating with others

    It's easier to manage a git repository if you're the only one working on the project. But it gets relatively tough when you have other collaborators and you're a newbie.

    To pull latest changes from git, run git pull origin master(make sure to commit any changes before running this). Then edit codes if needed and follow these steps again.

Conclusion

A few points worth keeping in mind, always run git pull before git push. It's common to mess-up a repository among newbies. If you think that something went wrong and you cant fix it anymore, open project folder and delete the folder called .git and start from the beginning again.

If you find any issue, please drop me a message [using this form]({{ '#message' | relative_url }}).