Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Git: A practical guide

Posted on Jul 28 • Originally published at wizardingweb.hashnode.dev In the previous posts of this series, we covered the basics of version control systems and Git fundamentals. We introduced the advantages of using Git and provided installation guides for Linux, Windows, and macOS. Now, it's time to start using Git and create your first repository. This tutorial will guide you through the process of creating a Git repository and illustrate how to use various Git commands along the way. Whether you're a beginner or you just need a refresher, this tutorial is for you. Let's dive in!While it's possible to create a repository at any point during the development process, it's always a good practice to create one early on. This is especially true if you're working with a team. By creating a repository early, you'll be able to keep your code organized from the beginning and avoid potential headaches down the line.To get started, let's go ahead and create a new folder on the desktop, I'll call it git-demo.To create a git repository, open up a terminal window (CMD or Powershell on Windows) and navigate to the directory where you'd like to create your repository. You can do that either with the cd command, which means Change Directory, like the following:or if you're on Windows, you can also right-click anywhere inside the folder and choose Git Bash Here.Once you're there, run the following command to initialize a new Git repository:This will create a new (hidden) directory named .git in your current directory, which will serve as the backbone of your repository. You can think of this directory as a hidden database that tracks all changes made to your code.Let's write some code, shall we?Create a new Python file called hello.py using your favourite code editor. This file will contain the shortest program in history:Right now, our file is not tracked by Git, meaning any changes we make to it won't be saved in Git's history. To track changes to our file, we need to stage it using the following command in the terminal:The git add command adds our file to Git's staging area, which means Git will start tracking changes to the file. The git add command can take arguments like file names, directory names, or wildcards to add multiple files at once. If we want to track all the files in our directory, we can writeOnce you've staged your changes, you're ready to Commit them to your repository using the git commit command.The git commit command creates a new commit with the changes we've staged. The -m flag allows us to add a commit message describing the changes we've made. Commit messages should be clear and concise and describe the changes made in the commit.To ensure that commits are effective and easy to manage, there are several good practices to follow:Keep commits small and focused: A commit should contain a single logical change or feature. This makes it easier to review and understand the changes made to the codebase.Write clear commit messages: A commit message should be descriptive and explain the changes made in the commit. This helps other developers understand what has been changed and why.Use the present tense and imperative mood in commit messages: Commit messages should start with a verb in the present tense, such as "Add," "Fix," or "Update." This makes it clear what action was taken in the commit.Commit frequently: Committing frequently allows for better tracking of changes and makes it easier to revert to previous versions if necessary.The git status command is a useful tool in Git that shows the current status of your repository. It displays the state of the working directory and the staging area. When you run this command, Git will tell you which files have been modified, which files are staged, and which files are not tracked.The git status command allows you to keep track of your changes and see which files are ready to be committed. It will tell you if the file is modified but not yet staged, or if it has been staged and is ready to be committed.In addition, git status also provides information about untracked files. Untracked files are files that are not yet added to the repository and are not being tracked by Git. This can help you keep track of changes to your code and ensure that everything is properly tracked. Let's run it in the terminal:Let's dive into branches and the git checkout command.Git Branches: In Git, branches are like parallel timelines that allow you to work on different features, fixes, or experiments without affecting the main codebase. You can think of branches as separate workspaces where you can freely create, modify, and delete code.Renaming a branch: If you want to rename an existing Branch, you can use git branch -M new_name.Creating a new branch: To create a new branch, use the git branch command followed by the branch name. For example, git branch new_feature will create a new branch called "new_feature."Listing branches: To see a list of all branches in your repository, you can use git branch -v. It will show you all the branches along with their latest commits.Viewing all branches: To see both local and remote branches, you can use git branch -a. This will display a list of all branches available in your local repository as well as the remote repository.Deleting a branch: To delete a branch that you no longer need, use git branch -d branch_name. If the branch has unmerged changes, you can use git branch -D branch_name to force delete it.Git Checkout: The git checkout command allows you to switch between different branches in your repository. It's like teleporting between different timelines, letting you work on specific branches at different times.Creating and switching to a new branch: To create and switch to a new branch at the same time, use git checkout -b new_branch_name. For example, git checkout -b feature_branch will create a new branch called "feature_branch" and switch to it.Switching to an existing branch: If you want to move to an existing branch, use git checkout existing_branch_name. For example, git checkout main will switch you to the "main" branch.Git Merge: Branches are often used to work on separate features, and once those features are ready, you can merge them back into the main codebase using the git merge command. It brings changes from one branch into another, creating a cohesive and up-to-date project.Let's walk through some examples of using the git merge command.Let's say we have two branches: "main" and "feature_branch." The "feature_branch" has completed its development, and now we want to bring those changes into the "main" branch.In this scenario, if there are no conflicting changes between the two branches, Git performs a fast-forward merge. It moves the "main" branch pointer forward to the latest commit of "feature_branch," incorporating all the changes smoothly.Let's consider a situation where both the "main" and "bug_fix" branches have made changes to the same file, resulting in a conflict.If Git detects conflicts between the changes, it will pause the merge process and prompt you to resolve the conflicts manually. You'll need to open the conflicting file, identify and choose which changes to keep, save the file, and then commit the resolution.You can provide a custom commit message while merging to provide more context about the changes being merged.This way, you can add a descriptive commit message that helps others understand the reason for the merge and what changes were introduced.It's common practice to have your default branch called "main", so let's rename the current branch to "main". In your terminal, write:Now, let's create a new branch to implement the feature "greeting".then switch to the newly created branch:Or we can combine both of the previous commands in one commandThen we'll make some changes in the hello.py file:We can now add and commit the changesgit commit -am is a shortcut to stage and commit changes to tracked files in one step. The -a flag automatically stages modified or deleted files. The -m flag lets you add a commit message directly in the command. Note that it doesn't stage new or untracked files.We can now merge the new feature into the main branch.We see the following output, informing us that a fast-forward merge was performed.We can now safely delete the greeting-feature branch:In this tutorial, we've covered the essential local commands of Git, giving you a solid foundation to start using version control in your development workflow. We learned how to create a repository, track changes using staging, commit changes with informative messages, and manage branches using git branch and git checkout.We also explored the powerful git merge command, which allows you to bring changes from different branches into the main codebase, and we saw how to handle both fast-forward merges and merges with conflicts.By now, you should feel more confident in managing your code with Git and using branches to work on separate features. Congratulations on mastering the basics!I hope you found this tutorial helpful, and I apologize for its length. I understand that it might have been quite detailed, but I believe that the in-depth explanations will benefit your understanding of Git. Your time and attention are greatly appreciated.In the next tutorial, we'll take it a step further and explore the world of remote repositories. We'll show you how to collaborate with others, share your code on platforms like GitHub, and use Git's remote commands to work with repositories hosted on remote servers. Stay tuned for an exciting journey into the world of remote Git!Feel free to leave your feedback or any questions in the comments below. Happy coding!Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse justcare prost - Jul 28 Abel Peter - Jul 28 Generic Pharmacy 24x7 - Jul 28 Preet Suthar - Jul 28 Once suspended, moharamfatema will not be able to comment or publish posts until their suspension is removed. Once unsuspended, moharamfatema will be able to comment and publish posts again. Once unpublished, all posts by moharamfatema will become hidden and only accessible to themselves. If moharamfatema is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Fatema Moharam. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag moharamfatema: moharamfatema consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging moharamfatema will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Git: A practical guide

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×