Git Cheat Sheet

Following are the git challenges that you can practice using this cheat sheet.

Remote Repository

A remote repository is a collection of all the files related to a project, stored on servers elsewhere.

Having a centralized repository makes it easier to share and contribute to a project.

  • https://github.com/ifahimkhan/AndroidStorage.git

Cloning Repository

  1. Creating a clone is exactly what it sounds like. A Git repository is simply a directory that contains all of the code and assets that you need to sync up.
  2. A Git repository is a repository that tracks all changes inside it. It uses a hidden .git directory to keep track of all the changes.

  • git clone https://github.com/ifahimkhan/AndroidStorage.git

Creating Branch

  • git branch my-android
To see Git created a new Branch or not 
  • git branch 
To switch between Branches, by default is main*
  • git checkout my-android
To delete a branch 
  • git branch -d my-android
To view local and Remote Branches
  • git branch --all
To switch to a Remote Branch
  • git checkout --track origin/branch-name 
To see your origin or Remote Repository Path
  • git remote -v

Staging Changes

  1. git add README.md 
  2. git add dir/
  3. git add *
  4. git restore <file>
  5. git diff --staged
Committing
  • git commit -m "Initial commit"
To see status before and after commit
  • git status

Pushing

  • git push --set-upstream origin my-android

Git Log

  • git log
  • git log -p (shows an actual diff of commit)
  • git shortlog (shows concise log)
  • git log --oneline (shows log in one line)
  • git log --all (shows log including remote commit)
  • git log --oneline --graph (show graphical representation of commits)
  • git log --oneline --graph --all (shows commit log including remote)
Git Search
  • git log --author=i_fahimkhan--oneline (search commit done by username)
  • git log --grep=android--oneline (search in commit message)
  • git log -S"android" (search in commit file content)
  • git log -S"android" -p (shows with file content)
Git Merging
  • git checkout branch-name (switch to another branch)
  • git log branch-name --not main (shows commit of that branch)
  • git checkout main (merge the changes done in branch to Main)
  • git merge branch-name (finally this command to merge the changes done in another branch to the Main branch) 
Git Syncing with Remote Repository
  • git pull origin(pull changes from remote to main)
  • git push origin main(sync main branch changes with a remote repo)
Git Syncing with Multiple Remote Repository

First, add the remote repository for which the changes we required in our local copy
  • git remote add user-name https://github.com/user-name/ repository-changes.git  (some user changes you want in your repository)
  • git remote -v (To see  new repository is added)
  • git fetch user-name (To pull down the contents of that user)
  • git checkout branch-name (point to the branch where you want to merge that user change)
  • git merge user-name/branch-name (finally merge the remote changes to your branch)
  • git checkout main (switch to your mainbranch)
  • git merge branch-name (merge your branch change to your Main)
  • git pull origin (just to be on safer side if working with multiple developers)
  • git push origin main (finally push those changes to our remote repository)
Creating Git Repository

  • git init (set up a directory as new git repository) 
  • git branch (to see the branch name change the branch name to main if its not)
  • git branch -M main (Changed branch name to main)
  • create a LICENSE file (https://choosealicense.com) default is MIT License
  • create a README.md file
  • Go to Github and create a New Repository and copy that link
  • git remote add origin https://github.com/user-name/ repository-name.git 
  • git remote -v
  • git push -u origin main

Main Points(Theory)
  • Cloning creates a local copy of a remote Git repository, Use Command: git clone 
  • Forking creates a remote copy of a repository under your personal userspace.
  • A commit is essentially a snapshot of files in the repository at a point in time. 
  • The working tree is the collection of project files that you work with directly.
  • The staging area lets you construct your next commit. 
  • git reset HEAD <filename>  lets you restore your staging environment to the last commit state.
  • Merging combines work done on one branch with work done on another branch. 
  • Git has two mechanisms for synchronization: pushing and pulling. 
  • git push takes your local commits and synchronizes the remote repository with those commits.
  • git pull brings the commits from the remote repository and merges them with your local commits.
  • git pull is actually two commands in disguise: git fetch and git merge


















Comments

Popular posts from this blog

HOW TO GET FREE WEB HOSTING FOR WEBSITE BY FAHIM KHAN

Demystifying Java: Your Journey to Coding

Git Challenge V