Configure name and email address


git config --global user.name "Vivek Bhadra" --&gt; <em>configuring git with the user name</em>

git config --global user.email "vibhadra@xxxx.com" --&gt; <em>configuring git with user e-mail</em>

git config --global core.autocrlf input

git config --global core.safecrlf true

Initialize a git repo


git init

Add file in the repo


git add hello.txt

Commit your changes


git commit -m "First Commit"

Checking status of the git repo


git status

Removing a file


git rm --cached hello.txt --&gt; <em>removes the file if not already</em>

Discard all changes in a modified file


git checkout -- test.c --&gt; discards any changes made

Unstage any staged file


git reset HEAD test.c --&gt;<em> unstages a git added file</em>

Checking log


git log --pretty=oneline --&gt; <em>shows git history in one line each commit</em>

git log --grep=First --&gt; <em>greps the commit line</em>

man git-log --&gt; <em>shows the git log man page</em>

git log -n 2 --&gt; <em>shows the last two commit history</em>

git log --pretty=format:"Commit Hash: %H, Author: %aN, Date: %aD"

To revert back to a previous version


git log --&gt; It will show the SHAsum of the previous versions.
[

For example below:

commit 98a31dfe2c528e4132cd8ca7af5f07e4765958aa Author: unknown  Date:   Wed Jun 10 17:26:27 2015 +0100 commit message
git reset  --hard
git reset --hard 98a31dfe2c528e4132cd8ca7af5f07e4765958aa

It will set the HEAD to the specified version.

Revert Git repo to a previous commit

Temporarily switch to a different commit


<code>git checkout 0d1d7fc32</code></pre>
<h4>If you want to move to a old commit and check it in</h4>
<pre><code>
git checkout -b old-state 0d1d7fc32
</code></pre>
<h4>Reverting Working Copy to Most Recent Commit</h4>
<pre>[code language="bash"] <code>git reset --hard HEAD</code> 

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


<code>git diff 4ac0a6733</code> 

To see the previous commits in git


<code>git log --pretty=oneline --decorate --all</code> 

Cloning from an existing git repo


git clone https://github.com/vivekbhadra/kernel_programming.git

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


git push

Check git branch


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$

Create a new git branch


git checkout -b 

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.


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$

Switching between branches


git checkout 

Example


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$

Leave a Reply