What is Git? How to use it?

What is Git and how could I use it to upload my website?

Git is a version control tool that tracks changes in your code and facilitates collaboration on projects that may involve many developers. You can find an excellent introduction to the basics of Git on their website as well.

First you can download Git from their website if it hasn’t already come installed with your operating system. If you already have Git installed, when you type git into the command line it will return a list of commands.

In general, working with Git will depend on whether you are working from a local copy of your code or interacting with a remote repository, such as when your code is actually on Platform.sh or GitHub.

Interacting with a local repository

  1. git init

    If you have a directory on your computer that is filled with files, you can track the changes more easily by initializing it as a Git repository.

    $ git init
    Initialized empty Git repository in /Users/chadcarlson/myapp/.git/
    

    Now it’s no longer simply a directory, but is now a repository. The new hidden directory .git has been added to the project, which contains files that label and keep track of whatever changes you make:

    $ ls -a .git
    .		            HEAD		hooks		logs		refs
    ..		            config		index		objects
    COMMIT_EDITMSG	    description	info		packed-refs
    
  2. git branch

    Initializing a repository made one very important change to your project, which is that it creates the concept of branches to your workflow. Once you initialize it, all of the files are now considered to live on the main branch of your repository. You can view this branch with the command

    $ git branch
    * main
    

    Here, only main is listed, because the repository was only just now initialized. If you want to make changes to your code, however, it is recommended that you make those changes on a new branch, which will make them easier to track. Create a branch using the previous command, only this time include the name of the new branch.

    $ git branch updates
    $ git branch
    * main
      updates
    

    Now when you list the branches in your repository, main and updates are both present.

    You will notice that the asterisk is still located next to main which means that you are still on main, and any changes you make will be applied to the main branch.

  3. git checkout

    Since you don’t want to make changes to main necessarily, you will need to switch to the updates branch. A switch to another branch is called checkout.

     $ git checkout updates
     Switched to branch 'updates'
     $ git branch
       main
     * updates
    

    Now any changes you make to your files will be changed on the branch updates, but will retain their old versions on main.

  4. git commit

    When you’ve made some changes to your files, there is one more important step required to essentially save those changes to the updates branch, and it is the primary unit of change in Git. It is called the commit.

     $ git add .platform.app.yaml 
     $ git commit -m "Update project root name."
     [updates f3b74e4] Update project root name.
     1 file changed, 3 insertions(+), 3 deletions(-)
    

    On Platform.sh, you can modify how an application is built and deployed inside the .platform.app.yaml file. Make a change to that file on updates and then commit that change with a message. Git will commit those changes to updates and then label that change with an identifier, which in this case is f3b74e4.

    This means that main remains in its old version, whereas updates has an additional commit of your changes. Another way to say this is that updates is one commit ahead of main.

  5. git merge

    You’ve made a commit on updates and you’re satisfied with it. You want all changes in the future to include that change. The best way to make sure that this happens is by merging updates, the branch that contains that commit, into main:

    $ git checkout main
    Switched to branch 'main'
    $ git merge updates
    Updating 440e085..f3b74e4
    .platform.app.yaml | 6 +++---
    1 file changed, 3 insertions(+), 3 deletions(-)
    

    First, checkout main. Then using the form git merge <branch to be merged into currently checked out branch>, which in this case is updates. You will see that main is now updating based on commit f3b74e4.

Interacting with a remote repository

Once you start collaborating with other developers, it’s best to create a remote version of your repository that others can download and commit changes to.

  1. git remote

    The first step is to define that remote repository’s location. To set the GitHub repository myapp as remote:

    $ git remote add origin git@github.com:chadwcarlson/myapp.git
    

    or for the Platform.sh project myapp with the project ID wiqte7at4yg22:

    $ platform project:set-remote wiqte7at4yg22
    
  2. git push

    Now that you have set a remote repository, you can push your local copy to the remote location with

    $ git push origin main
    

    on GitHub, or for Platform.sh

    $ git push platform main
    

    Now other people can download your files and contribute to your project. It will be good practice from here on out to push your new branches to the remote repository before merging, rather than merging on your local copy.

    $ git push platform updates
    

These are only some of the import Git commands, so be sure to explore them all by typing git in your terminal.