User Tools

Site Tools


it:github

Git file version control system

Introduction

  • Git is a free distributed file version control system which stores version change snapshots and allows offline use of, and editing and committing, files on your local machine
  • Git was originally written by the inventor of Linux and despite being a big difficult to use at times, Git source code control dominates the open source and professional development ecosystem - it's now almost impossible to avoid Git if you are a programmer
  • it requires your user name and email
  • Git utilises Git repositories to store these file version snapshots
    • these can be local or remote on a git server (by having your code repositories on a remote server it makes your source code almost loss proof and tornado-proof!)
    • popular cloud-based Git hosting servers are:
      • GitHub
      • GitLab
      • BitBucket
  • Git makes it easy to create branches to a project
    • let's say you have created a project called main version and you need this maintained as is but also need to work on a new updated version, you just create a branch and use that branch for your updated version
      • if you need to edit the main version such as to do a bug fix you can still do this as if the branch didn't exist, but the changes in the main version will seemlessly propagate to your new version (of course if you don't want this, you could put your bug fixes into a separate branch from the main version)
      • you can then merge branches as needed
      • you can roll back to an earlier version if needed

Git local repositories

  • all Git files are text files viewable in Notepad
  • these are stored in hidden sub-folders of the same location as your project files:
    • files in your main folder:
      • hidden files:
        • .gitattributes
        • .gitignore
          • you can edit this file to tell Git which files you do NOT want in the repository or uploaded to the cloud
          • for example, in Delphi, you can ignore _history/*.* folder contents and *.~pas and *.~dfm (these are Delphi's history tracking system) and *.exe
      • README.md (this is a MarkDown format text file similar to RichText)
      • LICENSE
    • hidden subfolders: .git and .github
  • once a repository is created for a folder, any changes you make will be tracked from then on as well as pre-existing files

GitHub

  • GitHub is the single largest host for Git repositories, and is the central point of collaboration for millions of developers and projects. A large percentage of all Git repositories are hosted on GitHub, and many open-source projects use it for Git hosting, issue tracking, code review, and other things.
  • GitHub is now owned by Microsoft and is very secure
  • to simply clone public projects, you don’t even need to sign up
  • however to create fork projects, or backup or publish your local repository, etc you need to set up a free user account which gives 500Mb space ($US48/yr for 2Gb).
    • go to https://github.com, choose a user name that isn’t already taken, provide an email address and a password, and click the big green “Sign up for GitHub” button.
    • GitHub provides almost all of its functionality with free accounts, except some advanced features.
    • the way that GitHub maps your Git commits to your user is by email address
    • you should also set up Two-factor Authentication
    • clicking the Octocat logo at the top-left of the screen will take you to your dashboard page.
    • you’re now ready to use GitHub
    • click on the + to create a new repository or import a repository
      • provide a project name and then you choose public or private
      • once created it can be accessed via https://github.com/<user>/<project_name>
      • if you’re working with other people who you want to give commit access to, you need to add them as “collaborators“ - doing so will give them “push” access, which means they have both read and write access to the project and Git repository

Git editors

  • Git’s native environment is in the terminal. New features show up there first, and only at the command line is the full power of Git completely at your disposal
  • The legacy command-line terminal on Windows (cmd.exe) isn’t really capable of a customized Git experience but you can use PowerShell with a package called posh-git https://github.com/dahlbyk/posh-git which provides powerful tab-completion facilities, as well as an enhanced prompt to help you stay on top of your repository status.
    • before you’re able to run PowerShell scripts on your machine, you need to set your local ExecutionPolicy to RemoteSigned (basically, anything except Undefined and Restricted).
  • many use Notepad ++
  • can use PyCharm:

Git in Visual Studio Code

  • Visual Studio Code has Git support built in. You will need to have Git version 2.0.0 (or newer) installed.
  • The main features are:
    • See the diff of the file you are editing in the gutter.
    • The Git Status Bar (lower left) shows the current branch, dirty indicators, incoming and outgoing commits.
  • You can do the most common git operations from within the editor:
    • Initialize a repository
    • Clone a repository.
    • Create branches and tags.
    • Stage and commit changes.
    • Push/pull/sync with a remote branch.
    • Resolve merge conflicts.
    • View diffs.

Git workflow GUIs

GitHub GUI for Windows and macOS

  • can be downloaded from https://desktop.github.com/
  • when the applications are first run, they walk you through all the first-time Git setup, such as configuring your name and email address, and both set up sane defaults for many common configuration options, such as credential caches and CRLF behavior
  • on Windows, the client includes a shortcut to launch PowerShell with Posh-git
  • the client shows you a list of the repositories you have access to on GitHub, and can clone them in one step. If you already have a local repository, just drag its directory from the Finder or Windows Explorer into the GitHub client window, and it will be included in the list of repositories on the left
  • once it’s installed and configured, you can use the GitHub client for many common Git tasks.
    • the intended workflow for this tool is sometimes called the “GitHub Flow”
      • the general concept is that
        • (a) you’ll be committing to a branch, and
        • (b) you’ll be syncing up with a remote repository fairly regularly.

Git and Delphi

  • Git is built into Embarcadero's Delphi / RAD Studio (as is Subversion (SVN) - another version control system)
  • install 64bit Git for Windows and accept all the defaults (note it also installs Subversion!)
  • install 64bit GitHub Desktop and when you run it, create a username and email
  • record where Git is installed - usually c:\Program files\Git\bin\git.exe if 64bit
  • in Delphi / RAD Studio IDE:
    • menu: select Tools, Options, Version Control, and select Git and enter the above path, your Git username and email
    • you can then clone a Git repository from the cloud to store on your machine via menu File:Open from Version Control:Git
      • enter the repos URL (get this from GitHub Code button clone with https) and your folder destination (may need to manually create this prior?)
  • Creating a new repository:
    • in Windows Explorer, if not already exiting, create a new project folder
    • in GitHub Desktop, select the folder then create a repository:
      • give it a name and description, local folder path, check the Initialise with a README
      • select your GitIgnore file “Delphi”
      • create repository locally
      • now to publish it onto GitHub:
        • click on Publish repository button
          • choose title and description
          • ensure Keep this code private is checked
          • click Publish repos button
  • Editing Delphi project located within the repo folder
    • Git automatically tracks changes but these are not automatically “saved” or “committed” - so to do this for a batch of changes:
      • within Delphi IDE, right-click the changed file or the project in Project manager and select “commit”, or,
      • from Windows terminal cmd line on that folder: git commit -a -m “your comments of what changes you made” (-a = add everything, -m = commit message), or,
      • from GitHub Desktop, Commit to master
    • Now you need to push the committed changes to the cloud server via either:
      • Windows terminal cmd line in that folder: git push origin master (origin means originator which is you), or,
      • in Delphi project manager, right click select Git:Push:From Repository Root (NOT from Project Directory), or,
      • in GitHub Desktop, push the “Push origin” button
  • If you are collaborating as a team member:
    • use Fetch to get any changes from a shared repo
    • use pull request to request the originator to merge changes made on a shared repo into the main branch of the code - lots of etiquette involved in public repos
    • you may need to resolve merge conflicts and choose which version to keep
it/github.txt · Last modified: 2023/08/27 05:18 by gary1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki