Essential Git Commands You Should Know

Β·

3 min read

It is essential to know. And this justifies knowing a few of the most important git commands you need in your daily work.

1. Initialize a repository

git init

It initializes a new repository in your current folder and starts the version control flow for your repository.

2 . Clone a remote repository

git clone "url"

You don't necessarily need to create a new repository. Sometimes you just want to download an existing repository and contribute to it. This is where git clone comes in.

3 . Stage files for commit

git add "file_name"

Git requires you to tell it which directories and files you want under version control. With git add, you tell git: "The next time I commit, take care of this file/directory."

4 . Commit files

git commit -m "message"

To create a new "version" of your repository, you need to commit your previously staged changes. This creates a unique hash that you can always rewind your repository to.

5 . Add a remote repository

git remote add "Remote_name" "Remote_url"

If you have started with a local repository and now want to upload your code to a remote server, like GitHub or GitLab, you need to add a remote repository.

6 . Fetch remote changes

git fetch "Remote_name"

Git fetch tells git to go to the default remote repository or to one you specified and download all changes you don't have in your local repository yet. It doesn't merge those changes into your local branches.

7 . Fetch and merges remote changes

git pull "Remote_name" "Remote_branch"

git pull is like git fetch, but it also merges remote changes into your local branch directly.

8 . Rebase your changes

git rebase "Local_branch_name/ Commit_hash"

git rebase like telling git: "I actually want you to take my current work and replay all changes based on a branch or commit hash, I tell you." git then replays all your changes, one by one, and applies them, pausing when it encounters conflicts.

9 . Interactive rebase

**git rebase i "Remote/Local_branch_name" ** You can rearrange commits, squash them, and do much more black magic with it.

10 . Reset your local branch

git reset --hard

It throws everything away that you haven't committed yet, and gives you a new chance.

11 . Create a new local branch

git checkout -b

or

git switch -c

Imagine multiple copies of an image where you change different things on different copies. But they are still linked and can be merged.

12 . Push a local branch to a remote repository

git push

or

git push "Remote_name" "Branch_name"

This means that you can upload your work to make it accessible for more developers and to keep it safer.

13 . Forcefully push local changes to a remote repository

git push --force

When you push changes to a remote, but someone made changes that you currently don't have, a normal push fails. This is where the parameters --force and --force-with-lease come in. They let you overwrite the remote.

14 . Rename a branch

git branch -m "old_branch_name" "new_branch_name"

  1. Show a commit log

git log

Shows log of all commit in descending order.

Β