Academind Logo

You Have to Know These Git Commands

git clone | git fetch | git branch -r | git branch --track | git switch | git pull | git push | git remote add origin

Created by Manuel Lorenz
#

Efficient Git Usage: Essential Commands for 2024

Git & GitHub remain a crucial tools for developers, offering powerful version control capabilities. With these key Git commands and practices you can easily manage your repositories - locally on your computer or remotely in the cloud.

#
Git Clone

To start working on an existing project, use git clone [URL] to create a local copy of a remote repository. Adding a "." after the URL clones it directly into your current directory. The local directory must not be a git repository already (you didn't run git init).

#
Branch Management

Cloning brings the entire content of the remote repository to your local repository, including the history (git log) and branches (git branch).

By default, cloning checks out the main branch only though. Use git branch -r to see all remote branches and git fetch to update this list. To track a remote branch locally, use git branch --track [branch name] [origin/branch name]

Once the branches are tracked, you can switch between branches as usual with git checkout or git switch.

#
Repository Updates

git pull updates your local repository with latest information from the remote repository and works equally to git fetch.

Additionally, git pull also updates local branch with changes from its remote counterpart and merges this changes into the currently selected local branch. It's vital for keeping your repository current. Conversely, git push uploads your local changes to the remote repository and merges the current branch's changes into the remote branch.

#
Connecting to Remote Repositories

To link your local repository to a remote one, especially when starting new projects, use git remote add origin [URL].

To push local changes and set up tracking, use git push --set-upstream origin [branch name]. With this connection established, you git push or git pull are sufficient to update remote or local branches now.

Recommended Courses