Coder Social home page Coder Social logo

git-commands's Introduction

Git-Commands

A. General

  • Create Git

    git init
  • Add All Files

    git add .
  • Commit

    git commit -m "Your Message"
  • Add & Commit together

    git commit -am "Your Message"
    or,
    git commit -a -m "Your Message"
  • Push

    git push -u origin main
    or,
    git push
  • Clone

    git clone [url-link.git]
  • Fetch

    git fetch
  • Pull

    git pull
  • Check file status

    git status
  • Check all log

    git log
  • Merge file

    git merge <branch-name>

    This command merges the specified branch’s history into the current branch.

  • Undo merge before commit & push

    git merge --abort
  • Steps to remove directory from Repository

    git rm -r --cached YourFolderName
    git commit -m "Removed YourFolderName from repository"
    git push origin main
  • Check remote origin

    git remote -v
  • Change remote origin

    git remote set-url origin https://github.com/......
  • Add remote url

    git remote add origin [url-link]
  • To push with new branch

    git push --set-upstream origin <New-Branch>
  • To Check Remote Origin

    git remote -v 
  • To change remote origin

    git remote set-url origin https://github.com/......
  • If gitignore not working, then run this command

    git rm -r --cached directory/sub-directory

    then use git add . , git commit -m message & git push


B. Branch

  • Check all branch List

    git branch
  • Create New Branch

    git branch <Your-Branch-Name>
  • Push the new branch in github

    git push --set-upstream origin <Your-New-Branch-Name>
  • Switch Branch

    git checkout <Branch-Name>
  • Delete branch locally

    git branch -d <Branch-Name>
    or,
    git branch -D <Branch-Name>

    The -d option will delete the branch only if it has already been pushed and merged with the remote branch

    -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

  • Delete branch remotely

    git push origin --delete <Branch-Name>

Merging branch via command line:

If you do not want to use the merge button or an automatic merge cannot be performed, you can perform a manual merge on the command line. However, the following steps are not applicable if the base branch is protected.

  • Step 1: Clone the repository or update your local repository with the latest changes.
git pull origin develop
  • Step 2: Switch to the base branch of the pull request.
git checkout develop
  • Step 3: Merge the head branch into the base branch.
git merge tenant-authorization
  • Step 4: Push the changes.
git push -u origin develop

Fetch new branch from remote to local machine:

If a team member has created a new branch on the GitHub server and you want to fetch and work with that branch locally, you can follow these steps:

  • Step 1: Fetch Branches from the Remote
git fetch origin
  • Step 2: List Remote Branches
git branch -r
  • Step 3: Check Out the New Branch
git checkout -b new-branch-name origin/new-branch-name

This command will create a new local branch named branch-name that tracks the remote branch of the same name. It also checks out the new branch so you can start working on it.

  • Step 4: Push Changes to the Remote (Optional)
git push origin new-branch-name

C. Publish your private assignments in your github public repository

  • Check Current URL

    git remote -v

Then create a new repository. After get new repository link update something in your locale machine which is in private mode. Copy the new url and jus change set-url instead of add

git remote set-url origin https://github.com/Irfan-Chowdhury/your_new_repository.git
git push

D. Undo last commit from local and remote

  • Locally

git reset --soft HEAD~1
  • Remotely

git push origin +HEAD

E. Tag

  • For Releasing New Version

git tag -a v0.1.2 -m "Release v0.1.2"
git push --follow-tags

F. Control your specific files/directory to visible/hide in GitHub

  • For remove/hide

git rm -r --cached app/Http/Controllers
  • For add/visible

git add -f app/Http/Controllers/Landlord
  • After doing these, please run these commands given below

git commit -m "Control my controller files"
git push origin main

G. Pushing Issues

If you face authenticate issue during try to push from terminal, first check which remote url exists in your repository.

  • check remote url
git remote -v
  • if url start with https://github.com/ then replace with [email protected]. For example, suppose your url is https://github.com/Irfan-Chowdhury/Git-Commands.git. Now set with [email protected]:Irfan-Chowdhury/Git-Commands.git.
git remote set-url origin [email protected]:Irfan-Chowdhury/Git-Commands.git

Now you can push easily from your terminal.

H. Explore Git

  • Git Restore

    git restore . 

    git restore কমান্ডটি মূলত আপনােক কোন ফাইল বা ডিরেক্টরি আগের অবস্থায় (শেষ কমিটের অবস্থায়) ফিরিয়ে আনতে সাহায্য কের। ধরেন আপনার অলরেডি কমিটেড একটা প্রজেক্টের নতুন একটা ফিচার ডেভ্লপ করার চেষ্টা করেছিলেন কিন্তু কিছু কোড করার ফলে মনে হল পূর্বের অবস্থায় চলে যাওয়া বেটার তখন এই কমান্ডা ব্যবহার করলে লাস্ট Commit অবস্থায় ফিরিয়ে নিয়ে যাবে ।

  • Git Stash

    যদি কোন কাজের মাঝামাঝি থেকে কোন প্রকার কমিট ছাড়া অন্য কোন টাস্ক বা ব্রাঞ্চে যাওয়া লাগে, ঐ কাজ গুলো টেম্পোরারি কোথাও স্টোর করার জন্য নিচের কমান্ডি ব্যবহার করা যেতে পারে ।

    git stash . 

    কমান্ডটি দেয়ার সাথে করা কাজগুলো ভ্যানিশ হয়ে যাবে । চিন্তার কোন কারণ নেই ।

    স্ট্যাশ কমান্ডটি এভাবে করলে আপনার অলরেডি গিটে ট্র্যাক করা (কমিটেড) ফাইল বা ডিরেক্টরির চেঞ্জসগুলাও স্ট্যাশে রাখবে । যদি শেষ কমিটের পর একেবারে নতুন কোন ফাইল অথবা ডিরেক্টরি এড করে থাকেন তাহলে সেগুলা এভাবে স্ট্যাশে যাবেনা সেক্ষেত্রে আপনাকে ফ্ল্যাগ ব্যবহার করে গিটকে বলে দিতে হবে যে আপনি নতুন ফাইল, ডিরেক্ট্রিগুলাও স্ট্যেষে নিতে চান ।

    git stash -u . 

    Listing stashes: You can view a list of your stashes using: ```bash git stash list ```

    Restoring the stash and removing it: for getting latest stash data, you've to command this

    git stash pop

    Applying a stash: সুবিধা হল আপনি চেঞ্জেস এপ্লাই করার পরও স্ট্যাশ থেকে এগুলার এক্সেস পাবেন ।

    git stash apply

    To apply a specific stash : যদি খেয়াল করেন তাহলে দেখবেন প্রত্যেকটা আইটেমের আগে এখানে stash@{n}, এখানে n মানে নাম্বার দিয়ে মার্ক করা আছে । আপনি এটা ব্যবহার করেও pop অথবা apply করতে পারবেন ।

    git stash apply stash@{3}
    git stash pop stash@{1}

    Clear Stash : To clear all stashes, you can use this.

    git stash clear

    Specific Clear Stash : To clear specific stash, you can use this.

    git stash drop stash@{3}
  • Git Reset

    ভুলে কোন কমিট করে ফেললে সেটি পূর্বের অবস্থায় ফিরিয়ে আনতে নিমোক্ত কমান্ডটি করতে হবে -

    git reset <commit_id>

    যদি উক্ত কমিটের সবগুলাই যদি চেঞ্জ হয়ে যাক চান তাহলে নিমোক্ত কমান্ড

    git reset <commit_id> --hard
  • Git Revert

    Reset - এর মতই তবে এখানে Revert করার এর পর আরেকটি কমিট দেয়া লাগে ।

    git revert <commit_id>

    Reset এবং Revert এর মধ্যে মূলত পার্থ্য হল -

    Reset কমান্ডে একটি কমিট পর্যন্ত থাকা চেঞ্জেসগুলা পর্যন্ত ফিরে পাওয়া যায় পরবর্তী সব কমিট বাদ দিয়ে । এক্ষেত্রে নতুন কোন কমিট তৈরি হয়না ।

    Revert এর ক্ষেত্রে একটা কমিটের চেঞ্জেসগুলা সব বাদ দিয়ে নতুন আরেকটা কমিট এর মাধ্যম । সেক্ষেত্রে আপনার প্রজেক্টটি গিটহাবে বা রিমোট কোন রিপোতে থাকলে অন্যান্য কন্ট্রিবিউটাররা রিভার্টের কমিটটা দেখতে পাবে এবং কনফিউজড হবেনা । এছাড়াও আপনি গিট লগ দেখলে দেখবেন যেখানে Reset করা হলে কোন প্রমাণ বা লগ পাবেন না সেখানে Revert করা হলে সেটার জন্য আরেকটা কমিট মানে লগ দেখতে পাবেন ।

  • Git Rebase

    git rebase main    

    নরমাল মার্জ করার মতই তবে এক্ষেত্রে লগ ক্লিন থাকে । Details in -
    Book : "এক পলকে গিটহাব"
    Author : জুনায়েদ আহমেদ
    Page No: 133

  • Git Squash

    If you create another new branch, currently you now in main branch and want to merge with the new branch -

    git merge new-feature --squash      

    নতুন ব্রাঞ্চে থাকা অবস্থায় যত গুলা কমিট করা হয়েছে সেগুলা চাইল সব এক কমিটেই মেইন ব্রাঞ্চের সাথে এড করে দিতে পারবো । এতে লগ ক্লিন থাকে । প্রাথমিকভাবে স্টেজড অবস্থায় পাবেন । উপরক্ত কমান্ডি চালানোর পর আরেকটি কমিট দিতে হবে যাতে সবগুলা চেঞ্জেস যাস্ট একটা কমিটের মাধ্যমে আপনার ব্রাঞ্চে যুক্ত হয়ে যায় -

    git commit -m “new feature introduced”      

git-commands's People

Contributors

irfan-chowdhury avatar

Watchers

James Cloos avatar  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.