Git Cheat Sheet For Dummies
By: Dharesh Vadalia
Tags: Git, Beginners Guide, Developer Tools, Git Commands, Cheatsheet

Using git isn’t a rocket science anymore…

Basic Git Commands

You can’t proceed with Git without knowing these commands.

git status //Shows status of working branch. Such as

//staged/unstaged changes and new,

//Untracked file. git ls-files //List all files in remote Dir git logs //List details of recent commits git diff //Compare changes of local with remote

Set Up Git Project On Local System

We don’t want to mess up with repository files. So lets make copy of a project on our local machine first.

#Create current working directory on local system as git project folder and fetch files from remote repository. git init

git remote add [remote_name] [your_project_http_address]

git fetch

git checkout [branch_name]



#OR #Load a copy of project to current directory on local system from remote repository. git clone [your_project_http_address]

Git remote_name by default is “origin”. Your project url address should be something like https://github.com/dhareshvadalia/MongoDB_Java.git format. Default branch is named master.

Working With Git Branches

It is a good practice to develop different feature of application on different branch. This will make yours and your project leads life easy.

git checkout -b [branch_name] //Create new branch git checkout [branch_name] //Switch between branch git branch -v //List all local branch git branch -r //List all remote branch git branch --all //List all branch (local+remote) git branch -d [branch_name] //Delete local branch git push origin :[branch_name] //Delete remote branch

Commit & Push Changes To Git Repository

Congrats! over the completion of your development. Its time to push your files changes to the repository.

git pull //Fetch latest files from repo git add * //Stage all files with changes git add [file1] [file2] //Stage specific files git commit -m “your msg” //Commit file to working branch git push -u origin [branch_name] //Push commit files to repository

Git will not allow you to commit changes without git -m commit message. Option -u is equivalent to “ — set-upstream” option. This flag sets default remote-tracking branch for the current local branch, allowing any future pull, fetch or push git commands to apply on this marked upstream branch on the remote repository. Always take a git pull before pushing changes to repository to avoid merge conflicts.

Revert Your Git Changes

Mistakes are forgiven when you are working with Git. But don’t make this a habit, sometimes this can put you in real trouble. Double check your changes before commit.

git reset //Unstage all staged files git reset [file1] [file2] //Unstage specific files git reset —-soft HEAD~1 //Undo last commit on local working

//branch git reset —-hard HEAD~1 //Undo last commit & deletes tracked

//files from local system git revert HEAD~1 //Undo last commit & deletes tracked

//files from local system

Git reset and revert both are used to modify the index of HEAD and delete files or changes created post desired commit. However, revert creates a record in log history, whereas reset does not. HEAD~X, where X is the number of commits you want to rollback (Default is HEAD~1).

Merge Git Branches

When you have finished development on a branch its time to merge your feature branch to master.

git checkout [branch_name] //Name of branch parent branch

git pull //Fetch most updated changes

git merge [branch_name] //Name of branch to be merged

Bonus

Everyone likes to have a litile extra of everything. Cherry on the top.

#Compare files from two different branches

git diff [branch_name1] [branch_name2] ./[file_name] #Shows graphical representation of the commit history

git log —-graph —-online —-all #Move around and review specific commit history

git checkout [commitSHA] #Rename remote from default "origin" to your desired choice

git remote rename [old_name] [new_name]

Many more features of git still remains uncovered in this Blog. However it cover more than enough for a beginner to learn and start using Git professionally.