Git Command Line

Git Command Line

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

1
2
3
4
5
6
7
8
9
git config --global user.email "your_email@domain_name.com" /* Setup email is used to commit */

git config --global user.name "your user name" /* Setup username is used to commit */

git config --global core.editor "vi" /* Choose editor used by GIT */

git config --global color.ui true /* Setup color ui for command line */

git config --list /* See Git configuration */

To Initialise a Local Repository

1
git init

Add a File to the Repository

1
git init

Commit the Change to Git

1
git commit -m "message"

See the Commits

1
git log

Basic Commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
git status  /*  The command 'git status' tells which files are not added or committed from Working to Staging to Repository */

git commit -m "message" /*  Commits and changes to all files that are in Staging into Repo  */

git diff /*  Show changes between Working and Local Repo, no file supplied shows all files  */

git diff --staged /*  Shows changes between Staged and Local Repo  */

git rm file.txt /*  Will remove file from working then git commit -m "" to also remove from Repo */

git rm --cached file.txt /* Leaves copy of file in Working but removes from Staging and Repo */

git mv /* Rename or move files - then git commit -m "" to move to Repo */

git commit -am "text goes here" /* Adds all files straight to Repo from Staging if they have changes - meaning they skip git add */

git checkout -- file.txt /* Restore Repo file to Working Directory using current branch  */

git reset --soft HEAD^ /* Restore repo file to staging */

git reset HEAD file.txt /*  Move a Stage file out of Stage back to Working */

git commit --amend -m "message" file.txt /* Change last commit to Repo (only last one can change) */

Resetting & Reverting

1
2
3
4
5
6
7
8
9
10
11
/* Reverting --soft --mixed --hard will go back to previous commits* /

git log /* Gets the sha1s so you can see the coomits where you want revert  back to */

git reset --soft sha /* Changes Repo but not Staging or Working */

git reset --mixed sha /* Changes Repo and Staging but not Working */

git reset --hard sha /* Changes all 3 Tiers */

git clean -f /* Remove untracked files from Working  */

Ignore File

1
2
3
4
5
6
7
8
9
10
.gitignore /* Ignores files to track in Working / track the .gitignore file */

Global Ignore /* Create in home folder  */
.gitignore_global
.DS_Store
.Trashes
.Spotlight_V100
/* Add in  */

git config --global core.excludesfile ~/.gitignore_global /* Add to gitconfig */

Stop Tracking Changes

1
git rm --cached file.txt /* Leaves copy in Repo and Working */

Commit Log

1
2
3
4
5
git ls-tree HEAD
git ls-tree master
git log --oneline
git log --author="Bunlong"
git log --grep="temp"

Show Commit

1
git show dc094cb /*  show SHA1 */

Commands on Branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
git branch /* Show local branches * is the one we are on */

git branch -r /* Shows remote branches */

git branch -a /* Shows local and remote */

git branch newbranch /* Creates a new branch */

git checkout newbranch /* Switch to new branch */

git checkout -b oldbranch /* Creates and switches to new branch  */

git push origin newbranch /* Push new branch to remote */

/* Diff in Branches */

git diff master..otherbranch /* Shows diff */

git diff --color-words master..otherbranch /*  Shows diff in color */

git branch --merged /* Shows any merged branches */

/* Rename Branch */

git branch -m oldname newname

/* Delete  Branch */

git branch -d nameofbranch

/* Merge Branch  */

git merge branchname /* 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 */

git merge --abort /*  Abort basically cancels the merge */

Manually Fix Files and Commit - The Stash

1
2
3
4
5
6
7
8
9
10
11
12
13
git stash save "message"

git stash list /* Shows whats in stash */

git stash show -p stash@{0} /* Show the diff in the stash */

git stash pop stash@{0} /* Restores the stash deletes the tash */

git stash apply stash@{0} /* Restores the stash and keeps the stash */

git stash clear /* Removes all stash */

git stash drop stash@{0}

Remotes Commands

1
2
3
4
5
6
7
8
9
git remote add origin https://github.com/bunlong/test.git /* Origin can be named whateve followed by the remote */

git remote /* To show all remotes */

git remote show origin /* To see remote URL*/

git remote remove origin /* To remove remote */

git remote rm origin /* To remove remote */

Clone project. Push from local to Remote

1
2
3
4
5
6
7
8
9
10
11
/* 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' */

git clone https://github.com/bunlong/test.git nameoffolder

git push -u origin master /* 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... */

git push

git push origin newbranch /* Push a branch to a remote */

Fetch Changes from a Cloned Repository

1
git fetch origin /* Pulls down latest committs from remote origin/master not origin, also pull down any branches pushed to Repo Fetch before you work Fetch before you pull Fetch often */

Merge with origin/master

1
git merge origin/master

Fetch + Merge data ==> Pull

1
git merge origin/master

Get Remote Branch

1
git branch branch_name origin/branch_name /* This will bring the remote branch to local and track with the remote */

Delete Branch

1
git branch -d branch_name

Checkout and Switch Branch and Track to Remote

1
git checkout -b nontracking origin/nontracking

Remove Remote Branch

1
git push origin --delete branch

Undoing Changes

1
2
3
4
5
6
7
git checkout path-to-file /* Restores a file before it is staged */

git reset HEAD path-to-file /* If it is staged - restores a file from last commit and then git checkout path-to-file */

git checkout HEAD^ path-to-file /* If is staged and committed - restores from last commit */

git reset --hard HEAD^ /* Restore prior commit */

Tag

1
2
3
4
5
6
7
git tag -a v1.0.0 -m "message" /* Tagging a commit with a version number*/

git push --tags /* Pushes tag info to master remote */

/* You can checkout a commit and add a tag to that commit by checking out its SHA */

git checkout f1f4a3d /* Checking out a commit - see the commit SHAS by git log */