Configure name and email address

[code language=”bash”] git config –global user.name “Vivek Bhadra” –> configuring git with the user name git config –global user.email “vibhadra@xxxx.com” –> configuring git with user e-mail git config –global core.autocrlf input git config –global core.safecrlf true [/code]

Initialize a git repo

[code language=”bash”] git init [/code]

Add file in the repo

[code language=”bash”] git add hello.txt [/code]

Commit your changes

[code language=”bash”] git commit -m “First Commit” [/code]

Checking status of the git repo

[code language=”bash”] git status [/code]

Removing a file

[code language=”bash”] git rm –cached hello.txt –> removes the file if not already [/code]

Discard all changes in a modified file

[code language=”bash”] git checkout — test.c –> discards any changes made [/code]

Unstage any staged file

[code language=”bash”] git reset HEAD test.c –> unstages a git added file [/code]

Checking log

[code language=”bash”] git log –pretty=oneline –> shows git history in one line each commit git log –grep=First –> greps the commit line man git-log –> shows the git log man page git log -n 2 –> shows the last two commit history git log –pretty=format:”Commit Hash: %H, Author: %aN, Date: %aD” [/code]

To revert back to a previous version

[code language=”bash”] git log –> It will show the SHAsum of the previous versions. [[/code] For example below: [code language=”bash”] commit 98a31dfe2c528e4132cd8ca7af5f07e4765958aa Author: unknown Date:   Wed Jun 10 17:26:27 2015 +0100 commit message [/code] [code language=”bash”] git reset  –hard git reset –hard 98a31dfe2c528e4132cd8ca7af5f07e4765958aa [/code] It will set the HEAD to the specified version.

Revert Git repo to a previous commit

Temporarily switch to a different commit

[code language=”bash”] git checkout 0d1d7fc32

If you want to move to a old commit and check it in


git checkout -b old-state 0d1d7fc32

Reverting Working Copy to Most Recent Commit

[code language="bash"] git reset --hard HEAD [/code]

To see the difference between your working copy and commit 4ac0a6733

[code language="bash"] git diff 4ac0a6733 [/code]

To see the previous commits in git

[code language="bash"] git log --pretty=oneline --decorate --all [/code]

Cloning from an existing git repo

[code language="bash"] git clone https://github.com/vivekbhadra/kernel_programming.git [/code] In the above example the git repository is located at https://github.com/vivekbhadra/kernel_programming.git which was created in github.com and can be found here.

Pushing changes up in the master

[code language="bash"] git push [/code]

Check git branch

[code language="bash"] vbhadra@vbhadra-VirtualBox:~/kernel_programming$ git branch -av * master 5a562f2 Sample hellow world module. remotes/origin/master 5a562f2 Sample hellow world module. vbhadra@vbhadra-VirtualBox:~/kernel_programming$ [/code] Create a new git branch [code language="bash"] git checkout -b [/code] In the above the name of the new branch can be anything you like. Ideally a name which can briefly describes what the branch is about would be nice. [code language="bash"] vbhadra@vbhadra-VirtualBox:~/kernel_programming$ git branch -av master 5a562f2 Sample hellow world module. * new_development 5a562f2 Sample hellow world module. remotes/origin/master 5a562f2 Sample hellow world module. vbhadra@vbhadra-VirtualBox:~/kernel_programming$ [/code] Switching between branches [code language="bash"] git checkout [/code] Example [code language="bash"] vbhadra@vbhadra-VirtualBox:~/kernel_programming$ git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'. vbhadra@vbhadra-VirtualBox:~/kernel_programming$ git branch -av * master 5a562f2 Sample hello world module. new_development 5a562f2 Sample hello world module. remotes/origin/master 5a562f2 Sample hello world module. vbhadra@vbhadra-VirtualBox:~/kernel_programming$ git checkout new_development Switched to branch 'new_development' vbhadra@vbhadra-VirtualBox:~/kernel_programming$ git branch -av master 5a562f2 Sample hellow world module. * new_development 5a562f2 Sample hellow world module. remotes/origin/master 5a562f2 Sample hellow world module. vbhadra@vbhadra-VirtualBox:~/kernel_programming$ [/code]

Discover more from Tech For Talk

Subscribe to get the latest posts sent to your email.

Leave a Reply