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
-
git init: Initializes a new Git repository. -
git clone [url]: Clones an existing repository. -
git add [file]: Adds a file to the staging area.git add .: Adds all modified files.
-
git commit -m "[message]": Creates a new commit with the staged changes. -
git status: Shows the current state of the repository. -
git log: Displays the commit history.
Branches
-
git branch: Lists all local branches. -
git branch [branch-name]: Creates a new branch. -
git checkout [branch-name]: Switches to the specified branch.git checkout -b [branch-name]: Creates and switches to a new branch.
-
git merge [branch-name]: Merges the specified branch into the current branch.
Synchronization
-
git pull: Fetches changes from the remote repository and merges them. -
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.
-
git remote -v: Lists all configured remotes. -
git remote add [name] [url]: Adds a new remote. -
git remote remove [name]: Removes a remote. -
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:
-
Verify current remotes:
git remote -v -
If you want to change the URL of an existing remote:
git remote set-url origin https://new-repository-url.git -
If you want to add a new remote:
git remote add [name] https://new-remote-url.git -
To remove a remote:
git remote remove [name] -
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.