Essential Git Commands Every Developer Should Know

Tools
Essential Git Commands Every Developer Should Know

August 2, 2019

Git is a crucial tool for any developer. Mastering its basic commands can significantly improve your workflow. Here's a guide to the most common commands and how to manage your remotes.

Basic Commands

  1. git init: Initializes a new Git repository.

  2. git clone [url]: Clones an existing repository.

  3. git add [file]: Adds a file to the staging area.

    • git add .: Adds all modified files.
  4. git commit -m "[message]": Creates a new commit with the staged changes.

  5. git status: Shows the current state of the repository.

  6. git log: Displays the commit history.

Branches

  1. git branch: Lists all local branches.

  2. git branch [branch-name]: Creates a new branch.

  3. git checkout [branch-name]: Switches to the specified branch.

    • git checkout -b [branch-name]: Creates and switches to a new branch.
  4. git merge [branch-name]: Merges the specified branch into the current branch.

Synchronization

  1. git pull: Fetches changes from the remote repository and merges them.

  2. git push: Sends local commits to the remote repository.

Managing Remotes

Remotes are versions of your project hosted on the Internet or network. Working with remotes is crucial for collaborating on projects.

  1. git remote -v: Lists all configured remotes.

  2. git remote add [name] [url]: Adds a new remote.

  3. git remote remove [name]: Removes a remote.

  4. git remote set-url [name] [new-url]: Changes the URL of an existing remote.

How to Change Remotes

To change the remote of your repository, follow these steps:

  1. Verify current remotes:

    git remote -v
    
  2. If you want to change the URL of an existing remote:

    git remote set-url origin https://new-repository-url.git
    
  3. If you want to add a new remote:

    git remote add [name] https://new-remote-url.git
    
  4. To remove a remote:

    git remote remove [name]
    
  5. Verify the changes:

    git remote -v
    

Mastering these commands will help you manage your projects more efficiently. Remember that Git offers many more features, so don't hesitate to explore its official documentation to learn more.