Thursday, August 22, 2019

GIT Setup


1) sudo yum install "Development Tools"

2) sudo yum install gettext-devel openssl-devel perl-CPAN perl-devel curl-devel zlib-devel

3) sudo yum install wget

4) wget https://mirrors.edge.kernel.org/pub/software/scm/git/

5) extract git-2.7.2.tar.gz

6) cd to git-2.7.2 then enter

7) make configure

8) ./configure --prefix=/usr/local

9) sudo make install

10) Check git version

git --version

11) Enter the below configuration

git config --global user.name "pakkapoti"
git config --global user.email "web.xml@gmail.com"
git config --list

12) ssh-keygen -t rsa -b 4096 -C "web.xml@gmail.com"

13) eval "$(ssh-agent -s)"

ssh-agent bash

14) ssh-add /home/ansadm/.ssh/id_rsa.pub

If you receive the below error, install curl-devel package like below and re-run command again

Error:
Could not open a connection to your authentication agent.

15) yum install curl-devel

16) copy the id_rsa.pub content and paste into github profile SSH keys category

17) ssh -T git@github.com

SSH keys section turn into green

18) mkdir -p /home/ansadm/git-repo

19) cd /home/ansadm/git-repo

git init

20) create a git repository with name ansible_workshop

21) git remote set-url origin git@github.com:pakkapoti/ansible_workshop.git

(or)

get remote add origin https://github.com/pakkapoti/ansible_workshop.git

22) git pull origin master

you should see the below output 

[ansadm@ansible-control git-repo]$ git pull origin master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:pakkapoti/ansible_workshop
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master


Made the changes locally and push to GIT

23) git push origin master


Reference:

https://youtu.be/0yRkRiQUgWo?list=PL9ooVrP1hQOGwtQOt8ZBlVjFlI-9Vr5hx

https://youtu.be/b5oQZdzA37I?list=PL9ooVrP1hQOGwtQOt8ZBlVjFlI-9Vr5hx

https://youtu.be/1MVQYSlgXrI?list=PL9ooVrP1hQOGwtQOt8ZBlVjFlI-9Vr5hx

https://youtu.be/tjVsGOGExh4?list=PL9ooVrP1hQOGwtQOt8ZBlVjFlI-9Vr5hx
Some useful commands for daily use:

To list down the commits by your id

git log --author=Pakkapoti

To see what content changed on files and folders will display using below
git show <SHA_1>
Ever wondered how many commits you’ve contributed to a project? Or perhaps, which coworker has really done nothing (or maybe they have huge changes and fewer commits!) Well, wonder no more, for git shortlog is a nice way to find out.

I’m running this on my clone of jekyll, which is how this blog is generated:

$ git shortlog -s -n
  135  Tom Preston-Werner
  15  Jack Danger Canty
  10  Chris Van Pelt
  7  Mark Reid
  6  remi
  3  Mikael Lind
  3  Toby DiPasquale
  2  Aristotle Pagaltzis
  2  Basil Shkara
  2  John Reilly
  2  PJ Hyett
  1  Marc Chung
  1  Nick Gerakines
  1  Nick Quaranto
  1  Tom Kirchner
The -s option squashes all of the commit messages into the number of commits, and the -n option sorts the list by number of commits.

This command could also be useful for changelogs, since you could easily dump all the changes each person has done. There’s a few other neat options: -e will append emails, and you can control columns widths with -w. Check out the manpage for more information.


Show n number of logs for x user in colour by adding this little snippet in your .bashrc file or you can keep it in a .sh file.


gitlog() {
    if [ "$1" ] && [ "$2" ]; then
       git log --pretty=format:"%h%x09 %C(cyan)%an%x09 %Creset%ad%x09 %Cgreen%s" --date-order -n "$1" --author="$2"
    elif [ "$1" ]; then
       git log --pretty=format:"%h%x09 %C(cyan)%an%x09 %Creset%ad%x09 %Cgreen%s" --date-order -n "$1"
    else
        git log --pretty=format:"%h%x09 %C(cyan)%an%x09 %Creset%ad%x09 %Cgreen%s" --date-order
    fi
}

alias l=gitlog


To show the last 10 commits by Frank:

l 10 frank

To show the last 20 commits by anyone:

l 20

Sunday, August 18, 2019

Remove single file from staging or from commit


COMMIT STAGING GIT
Sometimes we accidentally add a file to staging or commit it to git repo. Lets get to how to we can remove it in this tip.

Before going further with tip, lets revisits states in which file might exists,

Untracked - when you first create the file, it goes in this area
Staged/ index - when you use git add command on the file, it goes in this area
Committed - when you use the git commit on the file, it goes in this area
Modified- the file is committed but has the local changes which are not committed or staged yet.

Remove from staging area
To remove from staging, we can use following command-

git rm --cached <file_name>
Here, we are using the rm command along with switch --cached which indicates the file to be removed from the staging or cached area.

For example, we can use following command-

git rm --cached unwanted_file.txt
Remove single file from committed area
Note: In this, it is assumed, you doing it on local latest commit and not the commit which is pushed to remote repository.

Removing file from committed area requires 3 commands to be run, they are as follows-

git reset --soft HEAD^1
Above will undo the latest commit. if you do git status you will see files in the staging area. Now, we can easily remove it from staging area, as mentioned from previous point.

git rm --cached <file-name>
By running above command, the file will appear in the untracked file section.

Now, we removed the single file, lets commit back those remaining files-

git commit -m "<your-message>"
GIT Commands
#############

$ git reset HEAD~
Personally, I like to keep this aliased to git uncommit for quick reference. To do so, add this to your git configuration:

$ ~/.gitconfig
[alias]
    uncommit = "reset HEAD~"

If you'd like to undo the commit but leave the changes staged, run:

$ git reset --soft HEAD~
If you simply want to redo the commit message, there is no need to rest. You can change the message using:

$ git commit --amend
This can also be used to add changes to the previous commit. For example, to add a file that you forgot to include, run:

$ git add forgotten_file
$ git commit --amend
You'll be given the opportunity to modify the commit message and the newly added file will be included.

To completely remove all traces of the last commit, nuke it from orbit with:

$ git reset --hard HEAD~1
To revert to a specific previous commit, losing all history between, use:

$ git reset --hard a731962

Tuesday, August 13, 2019

GIT Installation

Required packages before installation

Install the prerequisite packages and remove the any current Git installation.


yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel -y
yum install gcc perl-ExtUtils-MakeMaker -y
yum remove git -y

Download the latest Git installation from kernel.org and unpack it.

cd /usr/src
wget https://www.kernel.org/pub/software/scm/git/git-2.x.x.tar.gz
tar xzf git-2.2.2.tar.gz
Install it.

cd git-2.2.2
make prefix=/usr/local/git all
make prefix=/usr/local/git install
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
source /etc/bashrc
Test it, by checking the version.

git --version