{"id":4132,"date":"2024-01-27T16:59:46","date_gmt":"2024-01-27T11:29:46","guid":{"rendered":"https:\/\/pczippo.com\/?p=4132"},"modified":"2024-01-27T16:59:46","modified_gmt":"2024-01-27T11:29:46","slug":"50-basic-git-commands-developer-should-know-read-here","status":"publish","type":"post","link":"https:\/\/pczippo.com\/tech\/50-basic-git-commands-developer-should-know-read-here\/","title":{"rendered":"50 Basic Git Commands Developer Should know – Read Here!"},"content":{"rendered":"\n

From Git init To Git\u00a0merge, We have listed all the essential Git Commands you should know.<\/strong><\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

<\/p>\n\n\n\n

Hey, Developer! I have concluded 50 basic Git commands in this blog<\/a>. I have covered all the git commands which are required for any functionality in GitHub. In this Blog, you will find out what is Git commands, its features, and 50 basic commands.<\/strong> Version control is an integral part of modern software development, and Git stands out as the go-to tool for managing source code.<\/strong> Git provides a powerful set of commands that enable developers to track changes, collaborate seamlessly, and maintain code integrity. In this blog post, we’ll explore the top 12 most used Git commands that every developer should master.<\/p>\n\n\n\n

Git is both a distributed version control system and open-source software. It allows developers to maintain many versions of source code with ease. You can use it to determine who did what, when, and why. Git has become a must-have tool for any developer, and developers must be familiar with Git commands in order to properly utilize it. There are hundreds of Git commands, but just a few are used frequently.<\/p>\n\n\n\n

What Is Git Command<\/strong><\/h2>\n\n\n\n

Git is a distributed version control system (VCS) that is widely used for tracking changes in source code during software development. It was created by Linus Torvalds in 2005 and has since become the de facto standard for version control in the software development industry.<\/p>\n\n\n\n

Here are some key concepts of Git Commands:<\/strong><\/h2>\n\n\n\n
    \n
  1. Version Control System (VCS):<\/strong> Git allows multiple developers to work on a project simultaneously without interfering with each other. It keeps track of changes made to the source code, allowing developers to collaborate, roll back to previous versions, and merge changes seamlessly.<\/li>\n\n\n\n
  2. Distributed System:<\/strong> Unlike centralized version control systems, Git is distributed. Each developer has a complete copy of the entire project with its history. This makes Git more robust and flexible, as developers can work offline and commit changes locally before pushing them to a central repository.<\/li>\n\n\n\n
  3. Repositories:<\/strong> A Git repository is a storage location for a project, containing all the files, folders, and the history of changes. There can be a central repository, often hosted on services like GitHub, GitLab, or Bitbucket, and each developer can have their local repository.<\/li>\n\n\n\n
  4. Branching:<\/strong> Git makes it easy to create branches, which are separate lines of development. This allows developers to work on new features or bug fixes without affecting the main codebase. Branches can be merged back into the main code when the changes are ready.<\/li>\n<\/ol>\n\n\n\n

    key Features <\/strong>of Git Commands:<\/strong><\/h2>\n\n\n\n
      \n
    1. Commit:<\/strong> A commit in Git represents a snapshot of the project at a specific point in time. Each commit has a unique identifier (hash) and contains information about the changes made, the author, and a timestamp.<\/li>\n\n\n\n
    2. Merge:<\/strong> Git provides tools to merge changes from one branch into another. This is particularly useful when working on separate features or bug fixes that need to be combined into a single codebase.<\/li>\n\n\n\n
    3. Pull Request (PR):<\/strong> In Git-based platforms like GitHub and GitLab, a pull request is a way to propose changes to a repository. It allows developers to review, discuss, and collaborate on code before merging it into the main codebase.<\/li>\n\n\n\n
    4. Remote:<\/strong> A remote in Git refers to a copy of the repository stored on a server, such as on GitHub. Developers can push changes to a remote repository or fetch changes from it.<\/li>\n<\/ol>\n\n\n\n

      1. Git\u00a0clone<\/strong> <\/h2>\n\n\n\n

      Git clone is a command that downloads existing source code from a remote repository (such as Github). In other words, Git clone creates an identical copy of the most recent version of a project from a repository and saves it to your computer.<\/p>\n\n\n\n

      There are a couple of ways to download the source code, but I like the clone with https method.<\/p>\n\n\n\n

      git clone <https:\/\/name-of-the-repository-link><\/strong><\/p>\n\n\n\n

      For example, if we want to download a project from Github, all we have to do is click the green button (clone or download), copy the URL from the box, and paste it after the git clone command that I demonstrated earlier.<\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      <\/p>\n\n\n\n

      This will create a copy of the project in your local workspace so you can begin working with it.<\/p>\n\n\n\n

      2. Git init<\/strong><\/h2>\n\n\n\n

      The git init command allows us to build a new Git repository. This is the initial command that initiates a new project in a GitHub repository. Run the git init command from the directory containing your project files. It will be expanded to include a hidden.git subdirectory.<\/p>\n\n\n\n

      $ git init<\/strong><\/p>\n\n\n\n

      You can also specify a repository name using the git init command.<\/p>\n\n\n\n

      $ git init <your repository name><\/strong><\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      <\/p>\n\n\n\n

      3. Git\u00a0branch<\/strong><\/h2>\n\n\n\n

      Branches are quite significant in the Git ecosystem. Branches allow many developers to work on the same project in parallel. We can use the git branch command to create, list, and delete branches.<\/p>\n\n\n\n

      Create New Branch<\/strong><\/p>\n\n\n\n

      git branch <branch-name><\/strong><\/code><\/p>\n\n\n\n

      This command will generate a branch locally. To push the new branch into the remote repository, run the following command:git push -u <remote> <branch-name><\/code><\/p>\n\n\n\n

      View branch:<\/strong> <\/code><\/p>\n\n\n\n

      git branch or git branch --list<\/strong><\/code><\/p>\n\n\n\n

      Delete branch:<\/strong><\/p>\n\n\n\n

      git branch -d <branch-name><\/strong><\/code><\/p>\n\n\n\n

      Also read About:<\/em><\/strong> How to make custom keyboard for gaming at home<\/strong><\/em><\/a><\/p>\n\n\n\n

      4. Git checkout<\/strong><\/h2>\n\n\n\n

      The git checkout command allows us to swap between current branches or create and switch to new ones. To accomplish this, the branch you wish to switch to must be present in your local system, and any modifications in your current branch must be committed or stashed before making the transition. You can also use this command to verify the files.<\/p>\n\n\n\n

      Usage<\/strong><\/p>\n\n\n\n

      Switch to an existing branch:<\/p>\n\n\n\n

      git checkout <branch-name><\/strong><\/p>\n\n\n\n

      Create and switch to a new branch:<\/p>\n\n\n\n

      git checkout -b <new-branch-name><\/strong><\/p>\n\n\n\n

      5. Git\u00a0status<\/strong><\/h2>\n\n\n\n

      The Git status command provides all relevant information about the current branch.<\/p>\n\n\n\n

      git status<\/strong><\/code><\/p>\n\n\n\n

      We can collect information such as:<\/p>\n\n\n\n

        \n
      • Is the current branch up to date?<\/li>\n\n\n\n
      • Is there anything to commit, push, or pull?<\/li>\n\n\n\n
      • Whether the files are staged, unstaged, or untracked.<\/li>\n\n\n\n
      • Whether any files are created, changed, or removed.<\/li>\n<\/ul>\n\n\n\n
        \"\"<\/figure>\n\n\n\n

        <\/p>\n\n\n\n

        6. Git\u00a0add<\/strong><\/h2>\n\n\n\n

        When we create, modify or delete a file, these changes will happen in our local and won’t be included in the next commit (unless we change the configurations).<\/p>\n\n\n\n

        We need to use the git add command to include the changes of a file(s) into our next commit.<\/p>\n\n\n\n

        To Add a single file:<\/p>\n\n\n\n

        git add <file><\/strong><\/code><\/p>\n\n\n\n

        To Add everything at once:<\/p>\n\n\n\n

        git add -A<\/strong><\/code><\/p>\n\n\n\n

        When you look at the screenshot above in the fourth area, you’ll notice that there are red file names, which signifies they’re unstaged files. Unstaged files will not be included in your commits.<\/p>\n\n\n\n

        \"\"<\/figure>\n\n\n\n

        <\/p>\n\n\n\n

        7. Git\u00a0push<\/strong><\/h2>\n\n\n\n

        After you’ve committed your modifications, you’ll want to transmit them to the remote server. Git push transfers your commits to a remote repository.<\/p>\n\n\n\n

        git push <remote> <branch-name><\/strong><\/code><\/p>\n\n\n\n

        However, if your branch is just formed, you must upload it using the following command:<\/p>\n\n\n\n

        git push --set-upstream <remote> <name-of-your-branch><\/strong><\/code><\/p>\n\n\n\n

        8. Git\u00a0pull<\/strong><\/h2>\n\n\n\n

        The git pull command is used to retrieve updates from a remote repository. This command is a combination of git fetch and git merge, which means that when we use git pull, it retrieves updates from the remote repository (git fetch) and immediately applies the most recent changes to your local repository (git merge).<\/p>\n\n\n\n

        git pull <remote><\/strong><\/code><\/p>\n\n\n\n

        9. Git\u00a0revert<\/strong><\/h2>\n\n\n\n

        Sometimes we have to undo the changes we’ve made. There are several ways to undo our modifications, both locally and remotely (depending on our needs), but we must use these commands with caution to avoid accidental deletion.<\/p>\n\n\n\n

        The safer approach to undo our commits is to use git revert<\/strong>. To view our commit history, execute the following command: git log–oneline.<\/strong><\/p>\n\n\n\n

        \"\"<\/figure>\n\n\n\n

        <\/p>\n\n\n\n

        Then we just specify the hash code next to our commit that we want to undo:<\/p>\n\n\n\n

        git revert 3321844<\/strong><\/code><\/pre>\n\n\n\n

        After that, you will see the screen shown below; <\/p>\n\n\n\n

        simply hit shift + q to exit:<\/strong><\/p>\n\n\n\n

        \"\"<\/figure>\n\n\n\n

        <\/p>\n\n\n\n

        The Git revert command will undo the supplied commit, however, it will produce a new one without deleting the previous one:<\/p>\n\n\n\n

        The advantage of using git revert is that it does not change the commit history. This means you can still see all of your commits, including those that have been reverted.<\/p>\n\n\n\n

        Another safeguard is that everything happens on our local system unless we push it to the remote repository. That is why git reverse is the safer and preferred method for undoing our commits.<\/p>\n\n\n\n

        10. Git\u00a0merge<\/strong><\/h2>\n\n\n\n

        When you’ve finished developing your branch and everything is working properly, merge it with the parent branch (dev or master). This is accomplished with the git merge<\/strong> command.<\/p>\n\n\n\n

        Git merge basically merges your feature branch and all of its commits back into the development (or master) branch. It’s vital to remember that you must first be on the branch you want to combine into your feature branch.<\/p>\n\n\n\n

        For example, if you wish to combine your feature branch with the development branch:<\/p>\n\n\n\n

        \u00a0switch to the dev branch:<\/p>\n\n\n\n

        git checkout dev<\/strong><\/p>\n\n\n\n

        Update local branch of dev:<\/p>\n\n\n\n

        git fetch<\/strong><\/code><\/pre>\n\n\n\n

        Merge feature branch to dev:<\/p>\n\n\n\n

        git merge <branch-name><\/strong><\/code><\/pre>\n\n\n\n

        Before merging your branches, ensure that your development branch has the most recent version; otherwise, you may encounter conflicts or other issues.<\/p>\n\n\n\n

        List Of 50 Basic Git Commands You Should Know\u00a0<\/h2>\n\n\n\n\n\n\n \n \n