Git for Beginners Study Notes

The Science of Computing III: Living with Cyber - Git for Beginners

Introduction to Git

  • Many programming projects previously written likely involved simple files on a personal computer.

  • As projects grow, it becomes essential to manage multiple copies and versions, ideally requiring a Distributed Version Control System (DVCS).

  • Definition: A DVCS is a system that helps track versions of a project across multiple systems (computers).

  • Git is the most prevalent DVCS. It allows tracking of changes, reverting to previous file versions, and collaborating with others on the same project.

Key Features of Git

  • Git tracks changes in files, enabling users to revert to previous versions if new changes lead to issues.

  • Any version can be retrieved and examined since Git starts tracking files.

  • Git coordinates work among multiple collaborators by allowing each programmer to work independently on copies without interference.

  • Example: If a feature added breaks functionality, Git enables reverting to the last stable version.

  • Git integrates with servers for remote storage and collaboration, with Github, Gitlab, and Bitbucket being the most commonly used.

  • Most servers offer free accounts, but they may be public by default unless a private account is purchased.

  • Command-Line Interface (CLI): While graphical user interfaces (GUIs) are available for Git, familiarity with command-line usage is advised to understand the underlying commands and processes.

Installing Git

  • For Linux: Run the command:
    sudo apt install git-all

  • For Windows: Consider Git Bash or other clients like GitHub Desktop. Check for:
    https://gitforwindows.org/

Using Git as an Individual

  • Git enables individual users to maintain version control over projects locally.

  • Cloning a Repository: To create a local copy of a remote repository: git clone <ADDRESS>

    • Example: git clone https://github.com/ankunda/WelcomeToGit.git

  • Creating a Local Repository from Scratch:

  mkdir mygitfolder  
  cd mygitfolder  
  git init
  • Outcome: Initializes Git in a new directory called mygitfolder.

Tracking Files with Git

  • After setting up the repository, you need to explicitly allow Git to track specific files:

    1. Create a file (e.g., HelloWorld.py) with basic content (i.e., print("hello world")).

    2. Use the command git status to check the state of the folder, which will show untracked files.

    3. To track the file:

    • Run git add <filename> to add a specific file, or

    • Run git add . to add all files in the current directory and its contents.

      • Stage Area: The added file is now in the "stage area" before being committed.

Committing Changes

  • Commit Command: Moves files from the stage to the project timeline.

  • Each commit requires a descriptive message:

    • git commit -m "short descriptive message"

    • Or git commit, which opens an editor for a detailed description.

  • Configuration: Users must set up their Git credentials on first commit:

  git config --global user.email <EMAIL>
  git config --global user.name "<FULL NAME>"  
  • Frequent Commits: Encourages good workflow practices; avoiding too many commits helps prevent unnecessary overhead.

Reverting Changes and Logs

  • To view commit history: git log

    • Displays all commits with hash, user, date, and message.

  • A hash is a unique identifier used by Git for each commit.

Checking Out Previous Versions

  • To revert to a specific commit: git checkout <hash>

    • To return to the latest version:
      git checkout master

Git Commands Summary so far

  • git init - Initialize the Git folder (run once).

  • git clone <address> - Create a local copy of a repository.

  • git add <filename> - Track specific file changes.

  • git add . - Track all changes in the directory.

  • git commit -m "message" - Save your changes to the timeline with a message.

  • git log - View all commits in the history.

  • git checkout <hash> - Move to the state of project at a specific commit.

Collaboration with Git

  • Git facilitates teamwork and coordination without sending files back and forth.

  • Creating a Remote Repository on GitHub:

    1. Create an account on GitHub.

    2. New Repository: Select initialize with a README file.

  • Adding Collaborators: Required for team contributions; done through the repository settings by adding usernames.

Working with the Remote Repository

  • Post-creation, the remote folder will only have the README. Team members can:

    1. Clone the repo: git clone <address>.

    2. Add files and commit changes locally:
      git add . git commit -m "initial files"

    3. Push changes to the remote repository using:

    • git push or git push <source-name> <branch-name>.

    • Users must authenticate via GitHub credentials/tokens.

Personal Access Tokens on GitHub

  • Tokens for security instead of passwords:

    1. Verify email and log in to GitHub.

    2. Access Settings → Developer Settings → Personal access tokens.

    3. Generate new token, specifying permissions like repo.

    4. Use the token in place of your password in terminal.

Syncing with Remote Changes

  • Once changes are pushed, colleagues can update their copies using:
    git pull - Default to latest branch.

  • Workflow: Always pull before starting new work to minimize conflicts.

Managing Merge Conflicts

  • Conflict Scenarios:

    1. Different files edited simultaneously.

    2. Different parts of a single file edited.

    3. Same lines in a single file altered.

  • Git attempts automatic merges unless conflicts arise.

  • Resolve conflicts manually by editing files with conflict markers.

  • Use commands:

  git add -A  
  git commit -m "merged conflicts"

Using .gitignore

  • Files to be excluded from tracking can be specified using a .gitignore file.

    • Create using: touch .gitignore.

    • Populate with file patterns to ignore, such as:

    • *.class

    • *.pyc

    • myownnotes.txt.

  • Create .gitignore before adding files intended to be ignored.

Branching and Merging

  • Branching allows you to work on features without affecting the main project.

  • Creating a New Branch:
    git checkout -b <branch-name>.

  • Pushing the Branch to remote server allows visibility for teammates:
    git push origin <branch-name>.

  • Once a feature is complete, merge it back to the master branch:

  git checkout master
  git merge <branch-name>
  • Deleting Branch once merged:
    git branch -d <branch-name>.

Practice and Learning Curve

  • Git can be complex initially, and frequent use enhances familiarity and proficiency.

  • Expect to consult documentation as needed; it improves coordination and productivity in team environments.

GUI Clients for Git

  • GUI resources exist for users preferring a non-command-line approach:

    • GitKraken is one example, offering a user-friendly interface.

  • Familiarity with Git commands remains beneficial, even while using a GUI.

Final Thoughts

  • The process of learning and implementing Git will skillfully manage collaborative programming projects.

  • Cultivate good practices, consistent committing, and navigational understanding of Git commands to enhance version control fluency.