Source Control with Git

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is a core benefit of using source control systems in software development?

  • Direct deployment to production environments.
  • Real-time user interface design.
  • Automated code compilation.
  • Tracking modifications to source code over time. (correct)

What is a key advantage of using Git for collaborative work among multiple developers?

  • Simultaneous editing of the same file.
  • Reducing the need for code reviews.
  • Enforcing strict coding standards.
  • Facilitating collaboration through branching and merging. (correct)

A team lead asks you to explain the concept of DVCS. Which of the following would be the MOST accurate way to describe a Distributed Version Control System (DVCS)?

  • A system where each developer’s workstation has a complete copy of the project's history. (correct)
  • A system that focuses primarily on code review and collaborative editing.
  • A system that stores all versions of files on a central server, requiring continuous network access.
  • A system designed for managing documentation rather than code.

In Git, what does the term 'snapshot' refer to?

<p>A record of the entire state of all files in the project at a specific point in time. (C)</p> Signup and view all the answers

What are the three primary states that a file can exist in, within Git?

<p>Modified, Staged, Committed. (A)</p> Signup and view all the answers

Which command is used to create a new Git repository in an existing directory?

<p><code>git init</code> (C)</p> Signup and view all the answers

When you have modified files in your working directory, which command do you use to add these changes to the staging area?

<p><code>git add</code> (A)</p> Signup and view all the answers

What is the purpose of the git clone command?

<p>To copy a repository from a remote URL to your local machine. (D)</p> Signup and view all the answers

After making several changes to a file, you want to see the differences between your current version and the last committed version. Which Git command would you use?

<p><code>git diff</code> (B)</p> Signup and view all the answers

Which command is used to save your staged changes with a descriptive message in Git?

<p><code>git commit</code> (B)</p> Signup and view all the answers

You've made a commit, but immediately realize you made a mistake in the commit message. Which command can you use to modify the most recent commit?

<p><code>git commit --amend</code> (A)</p> Signup and view all the answers

How can you send your local commits to a remote repository like GitHub?

<p><code>git push</code> (C)</p> Signup and view all the answers

What is the primary function of the .gitignore file in a Git repository?

<p>To specify intentionally untracked files that Git should ignore. (A)</p> Signup and view all the answers

If you want to remove a file from your Git repository and also from your file system, which command should you use?

<p><code>git rm</code> (C)</p> Signup and view all the answers

After modifying a file, you decide you want to discard the changes and revert to the last committed version. What Git command can you use?

<p><code>git checkout</code> (B)</p> Signup and view all the answers

What does the command git reset HEAD <file> do?

<p>It moves the specified file from the staging area back to the working directory. (D)</p> Signup and view all the answers

What is the purpose of Git branching?

<p>To create isolated environments for developing new features or fixing bugs. (D)</p> Signup and view all the answers

Why is 'Git branching' considered lightweight?

<p>Branching operations in Git are nearly instantaneous. (D)</p> Signup and view all the answers

In Git, what is a 'commit object'?

<p>A snapshot of the staged content along with metadata like author and message. (A)</p> Signup and view all the answers

What information does a Git commit object contain?

<p>A pointer to the snapshot of the content, author details, and a message. (A)</p> Signup and view all the answers

What is the 'HEAD' in Git?

<p>A pointer to the current branch. (B)</p> Signup and view all the answers

To create a new branch named featureX and switch to it immediately, which command can you use?

<p><code>git checkout -b featureX </code> (C)</p> Signup and view all the answers

You are on the main branch and need to switch to an existing branch called development. Which command should you use?

<p><code>git checkout development</code> (A)</p> Signup and view all the answers

What happens to your local files when you switch branches in Git?

<p>They are modified to reflect the state of the branch you switch to. (D)</p> Signup and view all the answers

Which command displays a simplified output view of the repository’s commit history, including the graph of commits?

<p><code>git log --oneline --decorate --graph --all</code> (B)</p> Signup and view all the answers

What type of information is stored within a Git branch?

<p>The 40-character SHA-1 checksum of the commit it points to. (C)</p> Signup and view all the answers

How do you create a new branch and switch to it using the git switch command (available since Git 2.23)?

<p><code>git switch -c &lt;newbranchname&gt;</code> (A)</p> Signup and view all the answers

In the context of forking a project on GitHub, what does 'forking' primarily achieve?

<p>It creates a personal copy of the project in your GitHub namespace. (D)</p> Signup and view all the answers

After forking a project and making improvements, how do you propose your changes to the original project on GitHub?

<p>By submitting a 'Pull Request'. (C)</p> Signup and view all the answers

What is a key characteristic of the 'GitHub Flow' workflow?

<p>All new work is done in separate branches. (D)</p> Signup and view all the answers

In the typical 'GitHub Flow', what action does the project owner take after reviewing a pull request?

<p>Merges the pull request or closes it. (B)</p> Signup and view all the answers

After a pull request has been merged in GitHub, what is the next step usually taken by the contributor?

<p>Sync the updated <code>main</code> branch back to their fork. (D)</p> Signup and view all the answers

In a branching workflow, after fixing an urgent bug on a hotfix branch, what is the next common step?

<p>Merging the <code>hotfix</code> branch back into the <code>main</code> branch. (C)</p> Signup and view all the answers

What does a 'fast-forward' merge typically indicate?

<p>A simple merge where the branch tip can be directly moved forward. (C)</p> Signup and view all the answers

What is the significance of a 'three-way merge' in Git?

<p>It uses two branch tips and their common ancestor to merge changes. (D)</p> Signup and view all the answers

Under what circumstances might a merge conflict occur?

<p>When two developers have modified the same lines in the same file differently. (A)</p> Signup and view all the answers

In case of a merge conflict, what does Git do to the affected file?

<p>Adds conflict-resolution markers to the file, requiring manual resolution. (C)</p> Signup and view all the answers

After manually resolving a merge conflict in a file, what is the next step to complete the merge?

<p>Run <code>git commit</code> to finalize the merge. (C)</p> Signup and view all the answers

You have a main branch and a feature branch. After merging the feature branch into main, what should you do with the feature branch?

<p>Delete the <code>feature</code> branch. (C)</p> Signup and view all the answers

Flashcards

What is Source Control?

A system that tracks changes to source code over time for collaborative software development.

What is Git?

A version control system used for tracking changes in source code. It allows collaboration, branching, and reverting to previous versions.

What is a DVCS?

A type of version control where each collaborator has a complete copy of the project's history, enabling offline work and decentralized development.

What is 'Modified' in Git?

The file has been changed but not yet saved locally.

Signup and view all the flashcards

What is 'Staged' in Git?

A modified file marked to be included in the next commit.

Signup and view all the flashcards

What is 'Committed' in Git?

Safely stored in the local database, marking a specific version in the project's history.

Signup and view all the flashcards

What does git init do?

A Git command that initializes a new Git repository in a directory.

Signup and view all the flashcards

What does git clone do?

A Git command that copies a repository from a remote URL to your local machine.

Signup and view all the flashcards

What does git status do?

A Git command that displays the current state of the repository, including staged, unstaged, and untracked files.

Signup and view all the flashcards

What does git add do?

A Git command used to add new or modified files to the staging area.

Signup and view all the flashcards

What does git commit do?

A Git command that records staged changes into the repository's history.

Signup and view all the flashcards

What does git push do?

A Git command that uploads local repository content to a remote repository.

Signup and view all the flashcards

What does git mv do?

A Git command that renames a file and stages the change.

Signup and view all the flashcards

What does git rm do?

A Git command that removes a file from the working directory and the staging area.

Signup and view all the flashcards

What is .gitignore?

A file specifying intentionally untracked files that Git should ignore.

Signup and view all the flashcards

What does git commit --amend do?

A Git command to modify the most recent commit. It can combine staged changes with the previous commit or edit the commit message.

Signup and view all the flashcards

What does git checkout -- <file> do?

A Git command to undo changes to a file, reverting it to the last committed state.

Signup and view all the flashcards

What is Git branching?

The tool for isolating changes; lightweight mechanism to diverge from the main development line without affecting it.

Signup and view all the flashcards

What is HEAD in Git?

A pointer to the most recent commit in a branch; moves forward with each new commit.

Signup and view all the flashcards

What does git checkout <branch> do?

To switch from one branch to another, updating the files in your working directory to match the selected branch.

Signup and view all the flashcards

What does git merge do?

To integrate changes from one branch into another, either by fast-forwarding or creating a merge commit.

Signup and view all the flashcards

What are conflict-resolution markers?

Markers that Git inserts into files when it encounters conflicting changes during a merge, requiring manual resolution.

Signup and view all the flashcards

What is Forking Projects?

If you want to contribute to an existing project on GitHub, to which you don't have push access, you can fork the project.

Signup and view all the flashcards

Pull Request

Opens up a discussion thread with code review, and the owner and the contributor can communicate about the change until the owner is happy with it, at which point the owner can merge it in.

Signup and view all the flashcards

Study Notes

Source Control with Git

  • Source control, also known as version control, is crucial for the software development lifecycle.
  • It enables tracking of code changes, collaboration, detailed history, and reversion to prior versions.
  • Source control integrates with tools like IDEs, build automation, and CI/CD platforms.
  • It is applicable to code, documentation, and configuration files.
  • Git, Subversion, Mercurial, Perforce, and CVS are popular SCM systems.

Types of VCS

  • Version Control Systems can be local, central, or distributed.

Git Overview

  • Git is a Distributed Version Control System (DVCS) for tracking code changes in software development
  • It facilitates collaboration, branching, merging, and reverting.
  • Git operates using snapshots rather than deltas.
  • Files can be in three states: modified, staged, or committed.
  • Modified files have been changed but not committed to the database.
  • Staged files have been marked to go into the next commit snapshot.
  • Committed data is safely stored in the local database.

Git Getting Started

  • A Git repository is obtained by initializing a local directory or cloning an existing repository.
  • To initialize a repository in an existing directory, use command git init
  • Files are Staged using the git add command.
  • Files are committed to a repository with the command, git commit.
  • An existing repository is cloned using the git clonecommand.
  • The command git status reports the status of the repo.
  • A file's status lifecycle starts as untracked, then moves to unmodified, modified, and staged.
  • New files are tracked using the git add command.
  • Modified existing files are staged also using the git add command.
  • git status -s provides a short status report.
  • Changes are viewed using git diff (--staged).
  • Changes are committed using git commit (-m).
  • Use the command git push to push changes to GitHub
  • To move a file use git mv commmand
  • To remove a file use git rm a file

Git Ignore

  • The .gitignore file ignores files not needed in the repository, such as log files, build files, & temporary files.
  • Simplified regular expressions are used, using #, *, ?, [0-9], and [abc].
  • Many .gitignore templates available on GitHub.

Undoing, Correcting and Restoring

  • Some undos cannot be undone in Git.
  • A commit is corrected using git commit --amend.
  • A staged file is unstaged with git reset HEAD <file>.
  • A modified file is un-modified using the command git checkout -- <file>.
  • The command git restore, introduced in Git version 2.23.0, is an alternative to git reset
  • A staged file can be unstaged using the command git restore --staged <file>
  • A modified file can be un-modified using git restore <file>

Important Commands

  • git log command views the commit history
  • git config --global user.name "Name Surname" command sets the Git user full name.
  • git config --global user.name command gets the Git user full name.
  • git config --global user.email "[email protected]" command sets the Git user email
  • git config --global user.email gets the Git user email

Git Branching

  • Branching creates a divergence from the main line of development safely.
  • Git branching is lightweight, with near-instantaneous operations and fast switching.

How Git Branching Works

  • Git stores data as a series of snapshots and commit objects.
  • Each Git commit creates a pointer to the staged content's snapshot.
  • Commits include the author's name, email, and a message.
  • Commits include a pointer(s) to the parent commit(s).
    • 0 parents: initial commit
    • 1 parent: normal commit
    • 2+ parents: result of merge of 2+ branches
  • An initial commit to a new repository is created using the git add README LICENSE test.rb command and the git commit -m "Initial commit" command.
  • There is a link to a parent commit in each next commit.
  • A branch in Git is a lightweight, movable pointer to a commit.
  • The default branch name in Git is master (or main on GitHub).
  • Creating a new branch is done using the git branch command, e.g., git branch testing.
  • HEAD is a special pointer that knows what branch you're currently on.
  • The command git checkout <branch> switches to another branch.
  • The HEAD branch moves forward when a commit is made.
    • nano test.rb
    • git commit -a -m "made a change"
  • A new command since Git 2.23 is the command git switch
  • git switch <branchname>
  • git switch -c <newbranchname>

Git Branching and Merging In Practice

  • A real-world scenario starts with a working software product on the master branch and a separate branch for issue #53 (iss53)
  • Checkout the master branch to begin to fix an urgent bug.
  • Create a new hotfix branch from the master branch.
  • Test that everything works as intended; the fix is complete in the hotfix branch.
  • Merge the hotfix branch back into the master branch.
    • This uses the commands -git checkout master -git merge hotfix
  • You can go back to the iss53 branch, and continue the development.
  • It is possible to delete the hotfix branch now that the master points to the same snapshot.
  • Merge the work from issue #53 now that it is complete into your master branch by:
    • First, check out the branch you wish to merge into (master);
    • Run the git merge command
      • In cases of no direct line of ancestry, Git does a three-way merge using the two snapshots pointed to by the branch tips (C4 & C5) and the common ancestor of the two (C2); Also called a merge commit
      • Then, it automatically creates a new commit that points to it;
    • Merge Conflicts Sometimes, merge process do not go smoothly.
      • Merge conflicts may occur if you changed the same part of the same file differently in the two branches you’re merging, Git won’t be able to merge them cleanly
        • Git adds conflict-resolution markers in conflicted files to identify and correct problematic sections. Then run git commit manually

Collaboration and GitHub Workflows

  • If there is no push access to an existing GitHub project, fork it to contribute
  • A fork creates a copy in your namespace where changes can be pushed.
  • Contribute changes back via a Pull Request to discuss code review.
  • The owner can merge Pull Request once the change is agreed on.

GitHub Flow

  • Fork the project.
  • Create a topic branch from master.
  • Commit to improve the project
  • Push this branch to your GitHub project.
  • Open a Pull Request on GitHub.
  • Discuss, and optionally continue committing.
  • The project owner merges or closes the Pull Request.
  • Sync the updated master back to your fork.

Conclusion

  • DevOps practices are implemented via Git in software development for collaboration, automation, continuous integration, and deployment.
  • Working on different codebase branches provides parallel development that is integrated by Git.
  • Continuous integration and deployment pipelines integrate seamlessly with Git for fast and reliable software delivery.
  • Quality, reliability, and efficiency of software development processes are improved by mastering and incorporating Git into DevOps workflows.

Hands on project

  1. Create a remote repository for the project on github.com
  2. Connect the repository with a local repository
  3. Develop a simple TO DO application CRUD API on the main branch (using back-end framework of choice). Commit the code on the main branch. a) Create/Update task b) List all tasks c) Delete tasks
  4. Open a new branch named frontend and switch to the branch
  5. Develop a front-end application which will communicate with the back-end (display tasks, add/edit tasks on the UI)
  6. Create a pull/merge request to merge the branch to the main branch
  7. Switch to the main branch locally
  8. Modify the back-end code to include one new property to the tasks (e.g. LastModifiedTimestamp)
  9. What will happen if you try to merge the branch frontend?

Assignment

  • Create a GitHub account. Add an SSH key pair to GitHub or create a personal access token.
  • Clone a new GitHub repository locally.
  • Add new content, stage files, commit, and push changes.
  • Create develop, test, and production branches.
  • Commit changes to develop, merge into other branches, and push.
  • Merge to main, commit, and push when done.
  • Contribute to another Git repository on GitHub by forking a colleague's repository, create a new branch, making changes, creating a pull requests, and asking them to merge.

References

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Git Version Control Quiz
3 questions

Git Version Control Quiz

FantasticNovaculite avatar
FantasticNovaculite
Git and Version Control Quiz
5 questions
Git Version Control System
5 questions

Git Version Control System

ExceedingAntigorite6154 avatar
ExceedingAntigorite6154
Use Quizgecko on...
Browser
Browser