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 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
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 onmain, and any changes you make will be applied to the main branch.
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.
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.
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.
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 mergingupdates, the branch that contains that commit, into main:
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.
git remote
The first step is to define that remote repository’s location. To set the GitHub repository myapp as remote:
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.
Comments
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
gitinto 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.
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: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 commandHere, 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.Now 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.Now 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.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: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:or for the Platform.sh project
myappwith the project IDwiqte7at4yg22:git pushNow that you have set a remote repository, you can push your local copy to the remote location with
on GitHub, or for Platform.sh
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.
These are only some of the import Git commands, so be sure to explore them all by typing
gitin your terminal.Please sign in to leave a comment.