How to install GIT on Linux/Ubuntu – Today, Source Version Control has gained popularity in the management of source code. Therefore, the software engineer needs to know how to use and manipulate GIT. The following common GIT command line will help you manipulate GIT:
Set up Git Configuration
123456789
gitconfig--globaluser.email"your_email@domain_name.com"/* Setup email is used to commit */gitconfig--globaluser.name"your user name"/* Setup username is used to commit */gitconfig--globalcore.editor"vi"/* Choose editor used by GIT */gitconfig--globalcolor.uitrue/* Setup color ui for command line */gitconfig--list/* See Git configuration */
To Initialise a Local Repository
1
gitinit
Add a File to the Repository
1
gitinit
Commit the Change to Git
1
gitcommit-m"message"
See the Commits
1
gitlog
Basic Commands
1234567891011121314151617181920212223
gitstatus/* The command 'git status' tells which files are not added or committed from Working to Staging to Repository */gitcommit-m"message"/* Commits and changes to all files that are in Staging into Repo */gitdiff/* Show changes between Working and Local Repo, no file supplied shows all files */gitdiff--staged/* Shows changes between Staged and Local Repo */gitrmfile.txt/* Will remove file from working then git commit -m "" to also remove from Repo */gitrm--cachedfile.txt/* Leaves copy of file in Working but removes from Staging and Repo */gitmv/* Rename or move files - then git commit -m "" to move to Repo */gitcommit-am"text goes here"/* Adds all files straight to Repo from Staging if they have changes - meaning they skip git add */gitcheckout--file.txt/* Restore Repo file to Working Directory using current branch */gitreset--softHEAD^/* Restore repo file to staging */gitresetHEADfile.txt/* Move a Stage file out of Stage back to Working */gitcommit--amend-m"message"file.txt/* Change last commit to Repo (only last one can change) */
Resetting & Reverting
1234567891011
/* Reverting --soft --mixed --hard will go back to previous commits* /gitlog/* Gets the sha1s so you can see the coomits where you want revert back to */gitreset--softsha/* Changes Repo but not Staging or Working */gitreset--mixedsha/* Changes Repo and Staging but not Working */gitreset--hardsha/* Changes all 3 Tiers */gitclean-f/* Remove untracked files from Working */
Ignore File
12345678910
.gitignore/* Ignores files to track in Working /trackthe.gitignorefile*/GlobalIgnore/* Create in home folder */.gitignore_global.DS_Store.Trashes.Spotlight_V100/* Add in */gitconfig--globalcore.excludesfile~/.gitignore_global /*Addtogitconfig*/
Stop Tracking Changes
1
gitrm--cachedfile.txt/* Leaves copy in Repo and Working */
gitbranch/* Show local branches * is the one we are on */gitbranch-r/* Shows remote branches */gitbranch-a/* Shows local and remote */gitbranchnewbranch/* Creates a new branch */gitcheckoutnewbranch/* Switch to new branch */gitcheckout-boldbranch/* Creates and switches to new branch */gitpushoriginnewbranch/* Push new branch to remote *//* Diff in Branches */gitdiffmaster..otherbranch/* Shows diff */gitdiff--color-wordsmaster..otherbranch/* Shows diff in color */gitbranch--merged/* Shows any merged branches *//* Rename Branch */gitbranch-moldnamenewname/* Delete Branch */gitbranch-dnameofbranch/* Merge Branch */gitmergebranchname/* Be on the receiver branch to merge the other branch *//* Merge Conflicts between the same file on 2 branches are marked in HEAD and other branch */gitmerge--abort/* Abort basically cancels the merge */
Manually Fix Files and Commit - The Stash
12345678910111213
gitstashsave"message"gitstashlist/* Shows whats in stash */gitstashshow-pstash@{0}/* Show the diff in the stash */gitstashpopstash@{0}/* Restores the stash deletes the tash */gitstashapplystash@{0}/* Restores the stash and keeps the stash */gitstashclear/* Removes all stash */gitstashdropstash@{0}
Remotes Commands
123456789
gitremoteaddoriginhttps://github.com/bunlong/test.git/* Origin can be named whateve followed by the remote */gitremote/* To show all remotes */gitremoteshoworigin/* To see remote URL*/gitremoteremoveorigin/* To remove remote */gitremotermorigin/* To remove remote */
Clone project. Push from local to Remote
1234567891011
/* Cloning a GitHub Repo - create and get the URL of a new repository from GitHub, then clone that to your local repo, example below uses local repo named 'nameoffolder' */gitclonehttps://github.com/bunlong/test.gitnameoffoldergitpush-uoriginmaster/* Push to remote(origin) and branch(master) *//* Push to Remote from Local - more - since when we pushed the local to remote we used -u parameter then the remote branch is tracked to the local branch and we just need to use... */gitpushgitpushoriginnewbranch/* Push a branch to a remote */
Fetch Changes from a Cloned Repository
1
gitfetchorigin/* Pulls down latest committs from remote origin/masternotorigin,alsopulldownanybranchespushedtoRepoFetchbeforeyouworkFetchbeforeyoupullFetchoften*/
Merge with origin/master
1
gitmergeorigin/master
Fetch + Merge data ==> Pull
1
gitmergeorigin/master
Get Remote Branch
1
gitbranchbranch_nameorigin/branch_name/* This will bring the remote branch to local and track with the remote */
Delete Branch
1
gitbranch-dbranch_name
Checkout and Switch Branch and Track to Remote
1
gitcheckout-bnontrackingorigin/nontracking
Remove Remote Branch
1
gitpushorigin--deletebranch
Undoing Changes
1234567
gitcheckoutpath-to-file/* Restores a file before it is staged */gitresetHEADpath-to-file/* If it is staged - restores a file from last commit and then git checkout path-to-file */gitcheckoutHEAD^path-to-file/* If is staged and committed - restores from last commit */gitreset--hardHEAD^/* Restore prior commit */
Tag
1234567
gittag-av1.0.0-m"message"/* Tagging a commit with a version number*/gitpush--tags/* Pushes tag info to master remote *//* You can checkout a commit and add a tag to that commit by checking out its SHA */gitcheckoutf1f4a3d/* Checking out a commit - see the commit SHAS by git log */