Complete GIT Guide – Practical Examples

Lets learn how to create a repository and commits.

Joshua González

Estimated reading time: 6 minute(s)
October 29, 2024

Creating a repository and commits

Let’s see a practical example of how to create a GIT repository, add files, make commits and view change history.

  1. Create a new project:
    • Create a folder for your project, for example, my-project. Open a terminal or command line and navigate to that folder:mkdir my-project cd my-project
  2. Initialize the repository:
    • Use the git init command to turn the folder into a GIT repository:git initYou should see a message indicating that an empty GIT repository has been created in the .git folder.
  3. Create files:
    • Create some files in your project. For example:echo “Hello, world!” > index.html echo “This is my first GIT project” > README.md
  4. Add files to the Staging Area:
    • Use git add to add the files you want to include in your first commit:git add index.html README.md
  5. Make the first commit:
    • Use git commit to create the commit with a descriptive message:git commit -m “Initial commit: basic files are added.”
  6. Make more changes and commits:
    • Modify existing files or create new files.
    • Add the changes to the Staging Area with git add.
    • Create new commits with git commit -m “Descriptive message”.
    • Example of additional changes and commits:echo “<h1>Welcome to my project</h1>” >> index.html git add index.html git commit -m “Add title to index.html” touch style.css git add style.css git commit -m “Add styles file”
  7. View commit history:
    • Use git log to view the list of commits you have created:git log
    • You should see something similar to this:commit 6e5c4f3a2b1c3d4e5f6g7h8 (HEAD -> main) Author: Your Name <[email protected]> Date: Sun Sep 3 18:47:00 2023 -0500 Add style file commit 9d8e7f2c1b0a2c3d4e5f6g7 Author: Your Name <[email protected]> Date: Sun Sep 3 18:45:00 2023 -0500 Add title to index.html commit 1a2b3c4d5e6f7g8h9i0j1 Author: Your Name <[email protected]> Date: Sun Sep 3 18:40:00 2023 -0500 Initial commit: basic files are added

Working with and merging branches

Let’s see a practical example of how to create branches, make changes to them and merge them back to the main branch, illustrating a common workflow in GIT.

  1. Create a new branch:Suppose you are in the master or main branch and you want to add a new feature to your project. Create a new branch called new-feature:git checkout -b new-featureThis command creates the branch and automatically switches you to it.
  2. Make changes to the new branch:Modify the files needed to implement the new feature. Add the changes to the Staging Area with git add. Make commits to save your progress:# Modify files… git add . git commit -m “Add basic structure of the new feature.” # Modify more files… git add . git commit -m “Implements core logic of the new feature”
  3. Switch to main branch:Once you have finished implementing the new feature, switch back to the main branch:git checkout main
  4. Merge the changes:Use git merge to integrate the changes from the new-feature branch into the main branch:git merge new-featureIf there are no conflicts, GIT will perform a fast-forward merge and move the main branch pointer to point to the new commits in the new-feature branch.
  5. Delete branch (optional):If you no longer need the new-feature branch, you can delete it:git branch -d new-feature

Process visualization:

main  o---o---o
        \
new-feature o---o---o

After merge:

main  o---o---o---o---o---o

Resolving merge conflicts

Merge conflicts occur when GIT cannot automatically merge changes from two branches because both branches have modified the same lines of code differently. Resolving these conflicts is essential to successfully integrate the changes. Let’s look at an example of how to do this.

Scenario:

  • You have a master or main branch and a new-feature branch.
  • Both you and another developer have modified the same file (index.html) in both branches.
  • Attempting to merge new-feature into master or main results in a conflict.

Steps to resolve the conflict:

  1. Identify the conflict:When you try to merge, GIT will show you an error message indicating that there are conflicts. The git status command will also show you the conflicting files:git merge new-feature # Output (hypothetical): CONFLICT (content): Merge conflict in index.html. Automatic merge failed; fix conflicts and then commit the result.
  2. Inspect the conflicted file:Open the index.html file in a text editor. You will see special markup indicating the conflict areas:<<<<<<< HEAD

    Welcome to my web site

    . =======

    Hello world!

    >>>>>>> new-feature
    • <<<<<<< HEAD: Indicates the start of changes in your current branch (master or main).
    • =======: Separates the changes in both branches.
    • >>>>>>> new-feature: Indicates the end of the changes in the branch you are merging.
  3. Resolve the conflict:
    • Decide which version of the code you want to keep or merge them appropriately.
    • Remove the conflict flags and modify the code according to your choice. For example:<h1>Welcome to my new website</h1>.
  4. Mark the conflict as resolved:
    • Add the modified file to the Staging Area:git add index.html
  5. Complete the merge:
    • Create a commit to finalize the merge:git commit -m “Resolves merge conflict in index.html”

Tips:

  • Use a text editor or visual merge tool to make it easier to identify and resolve conflicts.
  • Communicate with your teammates if you have questions about how to resolve a conflict.
  • Perform thorough testing after resolving conflicts to ensure that the code works as expected.

Collaborate on a project with other developers.

Collaboration is one of the pillars of GIT, and allows you to work as a team in an efficient and synchronized way. Let’s look at a practical example of how to collaborate on a project using a remote repository on a platform such as GitHub.

Scenario:

  • There is a repository on GitHub called collaborative-project.
  • You and other developers want to contribute to the project.

Steps to collaborate:

  1. Clone the repository:
    • Each developer must clone the repository to their local machine:git clone https://github.com/user/collaborative-project.git
  2. Create a branch for your work:
    • Create a new branch to work on your functionality or bug fixes:git checkout -b my-functionality
  3. Make changes and commits:
    • Work on your branch, modify files, add the changes to the Staging Area and make commits:# Modify files… git add . git commit -m “Implements part of my functionality”. # Modify more files… git add . git commit -m “Complete the implementation of my feature set”
  4. Send your branch to the remote repository:
    • Use git push to send your branch to the remote repository:git push origin my-functionality
  5. Open a Pull Request:
    • On GitHub, open a Pull Request from your my-functionality branch to the main branch (or the main branch of the project).
    • Describe the changes you have made and request that other developers review your code.
  6. Code review and discussion:
    • Other developers will review your code, make comments and suggestions.
    • Discuss the changes and make the necessary modifications to your branch.
    • Submit new commits to your branch to address comments.
  7. Approval and merge:
    • Once the reviewers are satisfied, they approve the Pull Request.
    • The repository owner or a contributor with permissions can merge your branch into the main branch.
  8. Update your local repository:
    • After your branch has been merged, update your local repository to include the changes:git checkout main git pull
Share this post: LinkedIn LinkedIn Logo Facebook Facebook Logo

About Aviada

With Aviada’s expertise in staff augmentation, hiring professionals in Mexico has never been easier.
By providing us with a job description, you can harness the benefits of Employer Of Record Services and trust Aviada to locate the ideal candidate to meet your needs, seamlessly taking care of the entire process.

Aviada ranked #1 in Best Place to Code 2021

Best Place to Code® is a program to identify and award companies that make efforts to offer the best work conditions to IT Professionals. This award is operated by Software Guru.
BPTC Logo