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
-
git initIf 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 -
git branchInitializing 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
mainbranch of your repository. You can view this branch with the command$ git branch * mainHere, only
mainis 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 updatesNow when you list the branches in your repository,
mainandupdatesare both present.You will notice that the asterisk is still located next to
mainwhich means that you are still onmain, and any changes you make will be applied to themainbranch. -
git checkoutSince you don’t want to make changes to
mainnecessarily, you will need to switch to theupdatesbranch. A switch to another branch is calledcheckout.$ git checkout updates Switched to branch 'updates' $ git branch main * updatesNow any changes you make to your files will be changed on the branch
updates, but will retain their old versions onmain. -
git commitWhen 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 onupdatesand then commit that change with a message. Git will commit those changes toupdatesand then label that change with an identifier, which in this case isf3b74e4.This means that
mainremains in its old version, whereasupdateshas an additional commit of your changes. Another way to say this is thatupdatesis one commit ahead ofmain. -
git mergeYou’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 mergingupdates, the branch that contains that commit, intomain:$ 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 formgit merge <branch to be merged into currently checked out branch>, which in this case isupdates. You will see thatmainis now updating based on commitf3b74e4.
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.
-
git remoteThe first step is to define that remote repository’s location. To set the GitHub repository
myappas remote:$ git remote add origin git@github.com:chadwcarlson/myapp.gitor for the Platform.sh project
myappwith the project IDwiqte7at4yg22:$ platform project:set-remote wiqte7at4yg22 -
git pushNow that you have set a remote repository, you can push your local copy to the remote location with
$ git push origin mainon GitHub, or for Platform.sh
$ git push platform mainNow 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.