Git Git

by Shreyansh Jha

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.

Git vs Github

GITGITHUB
It is a version control system that lets you manage and keep track of your source code historyIt is a cloud-based hosting service that lets you manage Git repositories

If you have open-source projects that use Git, then GitHub is designed to help you better manage them.

How to initialize a repository and connecting to Remote Repository:

git init
/*to initialize current directory as git repository*/

How to clone a repository:

git clone <url>
/*to clone an existing repository to your local machine*/

Set Global commit username & Email :

git config --global user.name "YourName"
/* to set an identififable name for credit management.*/

git config --global user.email "YourEmail"
/*to set an email address associated with the user.*/

Set local commit username & Email :

git config --local user.name
/* It will show username in gitcongif */

git config --local user.email
/* It will show userEmail in gitcongif */

git config --local user.name "YourName"
/* It will set username in gitcongif */

git config --local user.email "YourEmail"
/* It will set email in gitcongif */

Staging


git status
/* show modified files in working directory, staged for your next commit. */

git diff <ChangedFileName>
/* to get diffrence of what is changed but not staged */

git diff --staged
/* to get diffrence of what is staged but not yet commited */

git add <YourchangedFile>
/* add a file as it looks now to your next commit */

git reset HEAD <YourchangedFile>
/* Unstage a particular file */

git reset --hard
/* reset all to head */

git reset <YourchangedFile>
/*unstage a file while retaining the changes in working
directory */


git reset --hard <Commit_ID>
/* reset to before that commit */

git add .
/* to all the files in the directory to your next commit */

To Commit, log and Push changes

git commit -m "Your Message"
/* to commit the changes in the working directory */

git commit --amend -o -m "Your New Commit Message"
/* to amend the last commit */

git push origin <YourBranchName>
/* to push the changes to the remote repository */

git push -u origin <YourBranchName>
/* set upstream branch using the "git push" command */

git log
/* list all logs step by step */

git log --oneline
/* Plain list of commits */

Branching

git branch <BranchName>
/* to create a new branch */

git push origin <BranchName>
/* to push the branch to the remote repository */

git push origin --delete <BranchName>
/* to delete a branch from the remote repository */

git branch -r
/* to list all remote branches */

git branch
/* to list all local branches */

git branch -a
/* to list all branches */

git checkout -b <BranchName>
/* to create a new branch and switch to it */

git fetch && git checkout <RemotebranchName>
/* to fetch a remote branch and switch to it */

git branch -d <BranchName>
/* to delete a local branch */

git branch -d <BranchName1> <BranchName2> ..
/* to delete multiple local branches */

git merge <YourLocalBranch>
/* to merge a local branch into the current branch */

git merge origin <YourRemoteBranch>
/* to merge a remote branch into the current branch */

git fetch --prune
/* align git remote branch with local */

Staging

git stash save <myStashName>
/* to save the current state of the working directory */

git stash list
/* to list all stashes */

git stash drop stash@{ID}
/* to drop a stash */

git stash clear
/* to clear all stashes */

git stash apply stash@{ID}
/* to apply a stash */

git merge stash@{ID}
/* to merge a stash */

Revert Commit

git log --onetime
/* to list all commits */

git revert <commit hash>
/* to revert a commit */

Revert last Commit

git reset HEAD~
/* to revert the last commit, edit files as necessary and commit again */

Update last Commit

git commit --amend --no-edit
/* to update the current change in previous commit */

Some Usefull Links

Git for Windows Download

Official Github Cheatsheet Download