Top 10 Git Commands For Code Newbies!

Top 10 Git Commands For Code Newbies!

Hey there 👋, welcome to my little world again. Let's see the top git commands every developer should get to learn and why these particular ones.

There are hundreds of Git commands available to use for one reason or another. Today, we shall have a glance at my top commands and these are only a few at disposal.

blog-What-is-github-and-why-you-should-use-it.-removebg-preview.png These commands are super useful for accomplishing common tasks and I believe every developer should learn to be able to learn or know these.

Wait, What is Git ❓

Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.

It is basically a version control system that helps us see changes in our project code over time.

Top 10 Commands :

Note: This is my own opinion and feel free to disagree with this and I am going to have them in no particular order since all of them are useful! I will basically describe what each command does.

🔸 git init

The git init t command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository.

Common usages and options for git init

git init: Transform the current directory into a Git repository

git init <directory>: Transform a directory in the current path into a Git repository

git init --bare: Create a new bare repository (a repository to be used as a remote repository only, that won't contain active development)

🔸 git add

The git add command adds new or changed files in your working directory to the Git staging area.

git add is an important command - without it, no git commit would ever do anything.

Common usages and options for git add

git add <path>: Stage a specific directory or file

git add .: Stage all files (that are not listed in the .gitignore) in the entire repository

git add -p: Interactively stage hunks of changes

🔸 git commit

It is used to record the changes in the repository. It is the next command after the git add. Every commit contains the index data and the commit message.

Common usages and options for git commit

git commit: This starts the commit process, but since it doesn't include a -m flag for the message, your default text editor will be opened for you to create the commit message.

git commit -m "descriptive commit message": This starts the commit process, and allows you to include the commit message at the same time.

git commit -am "descriptive commit message": In addition to including the commit message, this option allows you to skip the staging phase.

The addition of -a will automatically stage any files that are already being tracked by Git (changes to files that you've committed before).

git commit --amend: Replaces the most recent commit with a new commit. (More on this later!)

🔸git push

The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo.

Common usages and options for git push

git push -f: Force a push that would otherwise be blocked, usually because it will delete or overwrite existing commits (Use with caution!)

git push -u origin [branch]: Useful when pushing a new branch, this creates an upstream tracking branch with a lasting relationship to your local branch

git push --all: Push all branches

git push --tags: Publish tags that aren't yet in the remote repository

🔸 git merge

Git merge is a command that allows you to merge branches from Git. Merging is a common practice for developers.

download-removebg-preview.png
Read More Here for more implementation

🔸 git clone

git clone is a Git command-line utility that is used to target an existing repository and create a clone, or copy of the target repository.

1_XHs4aDklfKj0itVxZlI-UQ-removebg-preview.png

Common usages and options for git clone

git clone [url]: Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits.

git clone --mirror: Clone a repository but without the ability to edit any of the files. This includes the refs, or branches. You may want to use this if you are trying to create a secondary copy of a repository on a separate remote and you want to match all of the branches.

🔸 git pull

The Git pull command is used to fetch and merge code changes from the remote repository to the local repository.

Git pull is a combination of two commands, Git fetch followed by Git merge.

main-qimg-54fe6adce878174ef81b037328c3dc0f-removebg-preview.png

Common usages and options for git pull

git pull: Update your local working branch with commits from the remote and update all remote-tracking branches.

git pull --rebase: Update your local working branch with commits from the remote, but rewrite history so any local commits occur after all new commits coming from the remote, avoiding a merge commit.

git pull --force: This option allows you to force a fetch of a specific remote-tracking branch when using the <refspec> option that would otherwise not be fetched due to conflicts.

git pull --all: Fetch all remotes - this is handy if you are working on a fork or in another use case with multiple remotes.

🔸 git status

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.

image.png

Common usages and options for git status

git status: Most often used in its default form, this shows a good base of information

git status -s: Give the output in short format

git status -v: Shows more "verbose" detail including the textual changes of any uncommitted files

🔸 git checkout

In Git terms, a checkout is the act of switching between different versions of a target entity. The git checkout command operates upon three distinct entities: files, commits, and branches.

image.png

In its simplest (and most common) form, only the name of an existing local branch is specified:

$ git checkout other-branch This will make the given branch the new HEAD branch. If, in one go, you also want to create a new local branch, you can use the "-b" parameter:

$ git checkout -b new-branch By using the "--track" parameter, you can use a remote branch as the basis for a new local branch; this will also set up a "tracking relationship" between the two:

🔸 git remote

The git remote command lets you create, view, and delete connections to other repositories. remote-removebg-preview.png

Common git remote commands

git remote -v: List the current remotes associated with the local repository

git remote add [name] [URL]: Add a remote

git remote remove [name]: Remove a remote

Git Vs GitHub

Git is a version control system that lets you manage and keep track of your source code history. GitHub is a cloud-based hosting service that lets you manage Git repositories.

1624956218-103268-removebg-preview.png Git is an open-source that allows users to modify and share the software. It is simply a GUI implementation.

I will further discuss this in a separate article.

Notes & Resources 📑

As I stated earlier, there are more meaningful and useful commands you can read from: 📌 Git-scm Official Docs
📌 Gith Hub Guide
📌 Attlasian Guide

If you enjoyed reading, consider subscribing and reacting to this with love by sharing, commenting and any criticism is much welcome.

🔹Follow me on Twitter :

Ronnie Atuhaire 🤓