Signup/Sign In

Top 12 Important Git Commands

Posted in General   OCTOBER 4, 2021

    Top git commands

    Beginning to learn Git commands is confusing. You could list more than a dozen occasions when initially you get totally confused with the Git commands and what they actually do.

    So here we have for you the 12 most frequently used Git commands, that will speed up your development workflow and allow you to go from a brand new repository to create a new branch and merge it with the master branch.

    Top Git Commands for Developers

    1. Git Init
    2. Git Clone
    3. Git Branch
    4. Git Checkout
    5. Git Add
    6. Git Commit
    7. Git Push
    8. Git Pull
    9. Git Diff
    10. Git Stash
    11. Git Status
    12. Git Log

    Let's look at these Git commands in a little more detail.

    1. Git Init

    Mostly, the first command that you would run in any new project that is not a Git repository already is the git init command, which is also called repo.

    You can use it to initialize an empty repo and to convert an existing, un-versioned folder into a Git repository.

    Using Git Init Command

    'cd' into the directory you want to initialize.

    Then, run this command.

     $ git init

    Your current directory will now be transformed into a Git repository. With a git sub-directory added, you could now start recording multiple versions of your project.

    2. Git Clone

    You could use the git clone command to download the source code from a remote repository like Bitbucket, GitHub, or GitLab.

    Using Git Clone Command

    $ git clone <https://url-of-the-repository>

    The code is automatically downloaded to your local machine when you clone a repo.

    The downloaded version is linked to the origin as a convenience because when you are collaborating on the same project you would want to push your changes to a single repo.

    But, there could be instances when you don't want to have this link, in that case, you can run

    $ git remote rm origin

    The current repository would be disassociated from the origin using this code.

    3. Git Branch

    One of the most important functionalities of Git is Branch, which allows the teams to work on the same code base in parallel. Using this, the teams can create Git Workflows and can make their workflows more efficient.

    Let's take, for instance, you are working on "Feature 1" and your teammate is working on "Feature 2". Both of you could work on the same code base in parallel by creating a separate branch for each feature, so you don't have to worry about any conflicts.

    This command could be used to create a new branch, delete a branch or view the existing ones.

    Create a branch using Git Branch

    $ git branch <branch-name>

    A new branch would be created in your local system by using this command. You will still have to push the branch if you want this to be visible to all the members in the repo.

    Command to view all branches

    $ git branch

    or

    $ git branch --list

    Git command to delete a branch

    $ git branch -d <branch-name>

    4. Git Checkout

    You have to switch to the branch with another command after you have created it and that is where the Git Checkout command comes in.

    Using Git Checkout Command

    $ git checkout <branch-name>

    You will automatically be switched to the branch name that you mentioned in the command.

    5. Git Add

    You will have to tell Git to track your new file and add it to the staging area, every time you create it or make a change, or the changes you made would not be added when you try to push your changes.

    Using Git Add Command

    $ git add <file-name>

    You can use this command to add a single file to your next commit, but if you want all the files to which changes were made to be added, you can use this

    $ git add -A

    It's important to remember that your changes will only be recorded in the remote repository when you commit them.

    6. Git Commit

    The Git Commit command could be seen as a checkpoint in your development process.

    You can use this to save your changes after you have completed a specific work item assigned to you.

    You will have to include a message to briefly describe the changes you made, every time you commit your code changes so that the other members of the team can very easily understand what was added, changed or removed.

    Using Git Commit Command

    $ git commit -a

    After you have run this command, you will be prompted to enter a commit message.

    7. Git Push.

    You will have to push your committed changes to the remote origin to make them available to your teammates.

    Using Git Push Command

    $ git push <remote> <branch-name>

    You need to keep in mind that the git push command will upload only the changes you have committed.

    8. Git Pull

    You can use the Git Pull command to find out about the changes that have been pushed by the members of your team and merge them automatically into your local repo.

    Using Git Pull Command

    $ git pull <remote>

    9. Git Diff

    When you want to see the difference between the current branch and another branch quickly, you can use Git Diff as your go-to command.

    Using Git Diff Command

    $ git diff

    All the uncommitted changes in your local repo will be shown to you by this.

    To compare two branches

    $ git diff branch1..branch2

    This could be used to see all the file differences between the two branches.

    10. Git Stash

    If you want to switch to another branch and work on something else, and then come back sometime later, you can use Git Stash to shelve your work like that.

    Using Git Stash Save Command

    $ git stash save “<stash-message>”

    You can use this command to stash your changes with the message. It will help you to come back and restore your stash, especially when you have several stashes.

    But, you need to keep in mind that this will stash only the files that you added using git add and to include the untracked files you can run this

    $ git stash save -u

    11. Git Status

    The Git Status command can tell you all the information that you need to know if you are feeling a bit lost with what has happened in your repo.

    Using Git Status Command

    $ git status

    You can get information like:

    • Your current branch.
    • Whether your present branch is updated.
    • Whether you need to commit, push or pull to anything in the branch.
    • If you have files that are created, deleted or modified.

    12. Git Log

    You wouldn't get the commit history for the repository using the Git Status command and this is where you can use the Git Log command.

    Using Git Log Command

    $ git log

    The entire commit history is now displayed to you.

    These are some of the most frequently used commands on Git Hub which will help you in case you get confused.

    About the author:
    Expert technical writer who simplifies complex technological concepts for lay audiences. Focused on providing insightful analysis and entertaining listicles on a wide variety of topics in the technology sector.
    Tags:listiclegithubgit
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS