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
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
.githas 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
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
masterbranch of your repository. You can view this branch with the command
$ git branch * master
masteris 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 * master updates
Now when you list the branches in your repository,
updatesare both present.
You will notice that the asterisk is still located next to
masterwhich means that you are still on
master, and any changes you make will be applied to the
Since you don’t want to make changes to
masternecessarily, you will need to switch to the
updatesbranch. A switch to another branch is called
$ git checkout updates Switched to branch 'updates' $ git branch master * updates
Now any changes you make to your files will be changed on the branch
updates, but will retain their old versions on
When you’ve made some changes to your files, there is one more important step required to essentially save those changes to the
updatesbranch, 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.yamlfile. Make a change to that file on
updatesand then commit that change with a message. Git will commit those changes to
updatesand then label that change with an identifier, which in this case is
This means that
masterremains in its old version, whereas
updateshas an additional commit of your changes. Another way to say this is that
updatesis one commit ahead of
You’ve made a commit on
updatesand 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
$ git checkout master Switched to branch 'master' $ git merge updates Updating 440e085..f3b74e4 .platform.app.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
master. 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
masteris now updating based on commit
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.
The first step is to define that remote repository’s location. To set the GitHub repository
$ git remote add origin email@example.com:chadwcarlson/myapp.git
or for the Platform.sh project
myappwith the project ID
$ platform project:set-remote wiqte7at4yg22
Now that you have set a remote repository, you can push your local copy to the remote location with
$ git push origin master
on GitHub, or for Platform.sh
$ git push platform master
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.