Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

Git basics: The simple commands to begin with

Home  »  Git • Tips   »   Git basics: The simple commands to begin with
Posted on August 17, 2022September 18, 2022 No Comments on Git basics: The simple commands to begin with
635

Git config command

This command is used to configure settings accompanying your use of git on your local machine/computer. Here are the major settings and configurations executed with this command. You can configure these settings on a global or local level.

NB:
Your username and email should be the same as those on your GitHub or any service provider you use to store your code for example Bitbucket.

  • Name (username)
    git config –global user.name “username”
    This command is used to create a username for the computer on a global level.
  • Email (email address)
    git config –global user.email “email address”
    This command is used to create an email address on the computer for the global user.
  • Default Editor (Code Editor)
    git config –global core.editor “code”
    This command sets the specified code editor as your default editor.

Initializing a repository

git init
This command is used to initialize a new repository on your machine/computer in the current folder or any other folder you may want

Git add command

This command is used to do quite a several tasks with flags attached to it at the end. Let’s look at the major 3 tasks this command executes in detail.

Adding a new branch to the repo.
This task itself has 3 simple major commands that can help reach your goal depending on the direction you want to take. Here they are.

  • git branch < new name >
    This command creates a new branch in the repository but keeps you at the current branch.
  • git checkout -b < new name >
    This command creates a new branch in the repository and takes you to the newly created branch.
  • git checkout -b < new name > < another name >
    This command creates a new branch in the repository from another branch.

Adding new changes (staging the changes)

  • git add < file.ext >
    This command adds changes to the specified file to the staging area in the repository.
  • git add .
    This command adds changes to all files in the repository to the staging area. You can also add a directory to a repo by using the command “git add < directory name >”

Adding a remote repo

  • git remote add < alias name > < URL >
    This command is primarily used to add a remote repo.

Git commit command

The commit command enables you to commit (or submit, if you will) all changes to the repo. It has two main implementations, which are:

  • git commit -m < message >
    This command commits all staged changes to the repository. Remember that the message should be a short description of what the snapshot represents.
  • git commit -a
    This command commits all the local changes in tracked files.

Ignoring files

You can also ignore tracking specific files in certain directories like log files.

  • < dir name >/*
    This command enables you to ignore all files in a specified directory. Where there is, you replace it with the name of the directory whose files you want to ignore.
  • *.< file extension >
    This command enables the .gitignore file to ignore all files with a specified extension. This must be added to the .gitignore file.
  • For example, * .css tells git to ignore all CSS files.
  • .jxt tells git to ignore all files in the JavaScript XML format.

Cherry-pick command

git cherry pick < commit-hash > < commit-hash >
This command enables you to apply one or commits from other branches to the current working branch. This can come in handy when time is not on your side while working on a project.

Git rename command

This command enables you to rename branches, files, and URLs with flags attached at the end.

  • git branch -m < new name of branch >
    This command allows you to rename the current working branch you are in.
  • git branch -m < old name of branch > < new name to give branch >
    This command allows renaming any branch that exits in the repo even though you may not at the moment, be working in that branch.
  • git mv file_from file_to
    This command allows renaming a file from its old name to the one you want to give it.
  • git remote rename < old name > < new name >
    This command allows you to rename the remote repo from its old name to the new one you want.

Git merge command

The merge command allows merging two or more branches or files to keep track of the changes in either branch or files.

  • git merge < branch name >
    This command allows you to merge another branch into the current branch.
  • git checkout < branch name > < path to file > –patch
    This command allows you to merge a single file from one branch to another.

Git modify command

The modify command allows you to change your latest commit message, change your latest commit and leave the message the same or change the repo’s remote URL.

Let’s take a look at its implementation in detail.

  git commit --amend

This command allows you to change your latest commit message.

  git add .
  git commit --amend --no-edit

This allows you to change your latest commit but leave the commit message the same.

  git remote set-url <alias> <URL>

This command allows you to modify your remote repo’s URL.

Viewing commands/status

This command allows you to do quite a number of tasks. Here are the subcommands that fall under it:

  git status

This command allows you to view the status of your project i.e unstaged, staged, and untracked files.

  git log

This command allows you to view all commits logs or history.

  git diff

This command allows you to view uncommitted changes in your repo.

  git diff --staged

This command shows you staged changes in your repo.

   git remote -v

This command shows you your remote repo’s remote URL.

    git branch

This command allows you to view the branches on your repo.

     git tag

This command allows you to view the tags in your repo.

Git clone command

This command allows replicating ( making the same exact copy ) of a specific repo into a specified directory.

     git clone <repo-url> <directory>

This command allows you to clone an existing repo into a specified directory.

     git clone <repo-url> .

This command allows you to clone an existing repo into the current working directory.

     git clone --recurse-submodules <repo-url>

This command allows you to replicate the existing repo along with its submodules into the current working directory.

     git submodule update --init --recursive

This command allows you to replicate submodules after cloning the existing repo.

Git delete command

This command allows you to delete files, branches, tags, etc. Let’s look at what it can do in detail.

    git branch -D <branch name>

This command allows you to delete a specified branch.

    git branch -D <branch name> <branch name>

This command allows you to delete multiple branches by going on listing their names. There is also an option of deleting them with a regex pattern but that is out of the scope of this article.

    git tag -d  v<tag version>

This command allows you to delete a tag.

    git remote rm <remote>

This command allows you to delete a remote repo.

    git clean -<flag>

This command allows you to delete untracked files.

    git remote prune <remote-name>

This command allows you to delete local branches that don’t exist at the remote repo.

Git revert command

This command allows you to undo a specific command or action. Here is how it works:

    git revert <commit-hash>

This command allows you to reset a specific commit by using their hashes alongside them.

    git checkout <repo>/<branch>  <filename>

This command allows you to reset a specific file.

    git reset --hard

This command allows you to revert or go back to the latest commit.

    git reset --hard <repo>/<branch>

This command allows you to go back to the last commit on the remote branch.

Got any questions? Any addition? Feel free to leave a comment.

Git, Tips

Post navigation

Previous Post: Best JavaScript Minifying Tools
Next Post: How to increase browser zoom level on page load with CSS?

Related Posts

  • How to Reverse A String with JavaScript
  • What is SSH in Linux?
  • How to Use useLocation Hook in React Router DOM V6
  • ES6 classes
  • Here are 10 platforms where you can find your next remote job as a developer.
  • Difference Between Git and GitHub

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

How to Deploy or Host your ReactJS App in cPanel
How to Send Email from Localhost in PHP
How to Delete Unused Database Tables in WordPress
How to check if a customer is logged in to Magento 2 or not?
How To Install Nginx, PHP on Ubuntu 22.04

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved