Git is a distributed Version Control System (VCS).
- Many open source geospatial projects are developed using Git.
- Common tool to develop software.
- Better project management.
- No more project1.py, project2.py, project_final, project_final2 etc.
- Versioning and tracking changes are systematic, hence, easier.
- GitHub
- Many online tutorials ease learning.
- Download Git from https://git-scm.com/
- Leave the default options until Choosing the default editor
- Leave the rest options in their default values:
- Adjusting your Path environment,
- Choosing HTTPS transport etc.
- Git Bash
- Investigate Git GUI - installed with Git.
- Investigate GitHub Desktop - separate software.
- This section will define and describe some of the common Git commands.
- git init initialises a folder.
- Creates the hidden .git folder. In order to see it, go to Folder Options and select Show hidden files, folders and drivers.
- Provides the configuration of the project.
- Adds the file(s) to index.
- With
git add hello.py
we index our first file - hello.py. git add .
will add all the files and folders to the index.
- Checks the current state of the working directory. Which files are tracked, which of them were updated etc.
- We can observe the:
- branch we are working on -i.e. master.
- Updated files.
- Here we observe that we need a commit to make changes recorded for future use.
- Captures the snapshot of the project's staged changes.
- Safe version of the project, which can be used to refer to later on.
- This would open VSCode as it was chosen as the default Git Editor. Here, we will prompt a commit message.
- A shorter way of doing this is to provide the commit message within the Git CLI:
git commit -m "An explanatory message of this commit
.
- Displays the past commits and their messages.
- Commit identifiers (long arbitrary text) would be useful to visit a previous commit.
- Once the project has many commits, it might be useful to track the milestones. Therefore, commit messages are important.
git log --oneline
provides a simplified view of the previous commits - each in one line.
- We might need to remove a file that was previously added from the index.
git rm -cached <file>
- Copied a remote repo to your local machine.
- Clone the content of our course to your computer:
git clone https://github.com/banbar/GMT-456-GIS-Programming
- If we working on a new feature, it might be better to open a new branch in order to make sure that the master branch is intact.
- Provides an isolated environment to test a new feature.
- Two or more developers can work on different branches at the same time.
- To view the branches of a repo:
git branch -a
. This would result something like:- The * indicates the current branch we are on.
- The red lines indicate the remote branches (i.e. the branches on GitHub).
- Create a new branch entitled feature-1:
git branch feature-1
. - To change our branch to feature-1:
git checkout feature-1
. - Now, let's add a new file to our branch: Git_102.md. You may see the added file with
git status
- Next steps:
git add Git 102.md
- add the new file to the stating area.git commit -m 'New markdown file - Git 102.md'
- Some files are sensitive (e.g. database passwords, private keys etc.) or they are simply not directly related to our project.
- Do not track those files.
- We need the .gitignore folder.
- xxx: touch gitignore
xxx
xxx
xxx
- This section is open to discuss some specific examples, which would hopefully be useful to our overall understanding of how Git works.
- In Git there is a two-stage commit process. We first add the files to the staging area (index), and then we commit these changes.
- The benefits of this process are described in several resources 1, 2 and summarised here:
- One large change of multiple files -> multiple commits instead of a single one. Use the
git gui
for a better management of this.
- One large change of multiple files -> multiple commits instead of a single one. Use the
- Fork is done on GitHub Account while clone is done on Git.
- Changes made to the forked repo can be merged with the original repo via a pull request.
This section briefly describes the useful patterns while using git.
git diff
before committing.