Coder Social home page Coder Social logo

git-commands's Introduction

Git-Commands

Table of Contents

Git Repository

Git initialization:

git init

Delet the Initialized git repository: (This will remove the version control system)

rm -rf .git

Display the status of git repository:

git status

Display commits history:

git log

Basics

Add all files to staging area:

git add .

Remove all files from staging area:

git rm --cached -r .

Commiting the staging area files:

git commit -m "Intial Commit"

Remote Repository

Pushing the local git repository to remote git repository:

git push origin master

Pull from remote to local repository:

git pull

Cloning a Remote Repository:

git clone <url>

Merging a Remote Repository:

git pull origin <branch_name>
  • This Merges the remote repository with local repository.

Branches

Creating a branch:

git branch <branch-name>

Displaying all the branches:

git branch

Merging the branch with main branch:

git merge <branch-name>

Git Checkout

Switching Branches

git checkout <branch-name>

Create a new branch and switch to it:

git checkout -b feature/new-feature

Go to the specific commit of the file:

git checkout <commit-hash>

Go to the latest commit of the working branch:

git checkout <branch-name>

This command will update your working directory to the latest commit on the specified branch.

Reversing Changes

git reset [options] <commit-hash>

This command in Git is a versatile command that allows you to reset the state of your repository to a specific commit.


git reset --soft <commit-hash>

The --soft option

  • This command will undo the last commit, but keep the changes from that commit in your working directory and staging area.
    • This means that your changes are not lost and are ready to be committed again.

You can then manually unstage or delete the files you want to remove.

A -- B -- C -- D (HEAD)

If you run git reset --soft B, you'll get:

A -- B (HEAD) -- C -- D

The commits C and D are effectively "uncommitted," and their changes are in the staging area, ready for a new commit.


git reset --hard <commit-hash>

The --hard option

  • moves the branch pointer to the specified commit
  • resets the staging area (index) to that commit
  • discards all changes in your working directory

Use this option with caution, as it permanently deletes your uncommitted changes.

A -- B -- C -- D (HEAD)

If you run git reset --hard B, you'll get:

A -- B (HEAD)

Commits C and D are effectively "discarded," and their changes are lost.

Note : We might want to update the remote repository with the changes we made in the local repository.

To do so we can use the following command: git push --force origin <branch-name>


Git Clean

If you have new files in your working directory that you want to remove in addition to resetting your branch to the latest commit, you can use the git clean command.

The git clean command is used to remove untracked files and directories from your working directory. Here's how you can do it:

Check for Untracked Files:

git clean -n

Remove Untracked Files:

git clean -f

Be very cautious when using git clean -f because it permanently deletes untracked files and directories, and there's no way to recover them.

Git Revert

git revert <commit-hash>

This command is used to create a new commit that undoes the changes made by a previous commit.

A -- B -- C -- D (HEAD)

You want to undo the changes introduced by commit C. You can use git revert as follows:

git revert C

After running this command, Git will create a new commit (let's call it E) that undoes the changes from commit C. The commit history will look like this:

A -- B -- C -- D -- E (HEAD)

Git Subtree

Git subtree is a feature in Git that allows you to embed one Git repository within another as a subdirectory.

This is useful when you want to include another repository's content into your own project, while still keeping them separate, and track changes to the embedded repository as part of your project's history.

git subtree add --prefix=subdir-name remote-repo.git branch-name

Summary

Summary

git-commands's People

Contributors

raajesh3108 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.