Ticker

6/recent/ticker-posts

List of basics git commands

As being software developer, At some point of time you will have to know about this SCM tool i.e GIT. 

I have compiled few git commands that i used while working in Software Development Life Cycle.

See the git version installed:

[satish@host]$ git --version


On UNIX based Operating System:

[satish@host]$ which git


For Initialization of git:

[satish@host]$ git init

  • This will create .git folder which contains the files/folders needed to work git seamlessly 


See the status of your git at any point of time:

[satish@host]$ git status


It will stage all the files/folders to be added to version control:

[satish@host]$ git add .
OR
[satish@host]$ git add -a
OR
[satish@host]$ git add --all


commit your code with a meaningful commit message and it will create a snapshot of your project :

[satish@host]$ git commit -m "Initial commit"

git add & git commit altogether with the following command

[satish@host]$ git commit -am "Initial commit" 


Good & meaningful commit message:

  • It is very much important when you are working in a Team to write good & meaningful commit message so that people can understand what did you changed in this line of code.
  • If you are using issues tracker tool(most popularly JIRA) then it is always recommended to put JIRA id in that commit message. See the following example:

DEV-121: Implemented login function 
DEV-127: Added admin module  
DEV-120: fix typo and incorrect example  


Clone a repository:

[satish@host]$ git clone https://github.com/username/projectname.git
OR
[satish@host]$ git clone https://yourusername@bitbucket.org/username/projectname.git

To specify a different name of the directory, e.g. MyCode:
[satish@host]$ git clone https://github.com/username/projectname.git MyCode 

To clone in the current directory:
[satish@host]$ git clone https://github.com/username/projectname.git .


Configure git i.e Setting your username and email:

See the global configuration of git:

[satish@host]$ git config --list

And you can check where settings are defined:

[satish@host]$ git config --list --show-origin


Define user name and email for all repositories:

[satish@host]$ git config --global user.name "Your Name"
[satish@host]$ git config --global user.email mail@example.com


Define user name and email for single repository:

[satish@host]$ cd /home/pankaj/repo
[satish@host]$ git config user.name "Your Login At office"
[satish@host]$ git config user.email mail_at_office@example.com


To remove global configured user name and email:

[satish@host]$ git config --global --remove-section user.name
[satish@host]$ 
git config --global --remove-section user.email


Add git remote commands:

Before proceeding for this command you should have required repository in your git service.

[satish@host]$ git remote add origin https://<your-git-service-address>/owner/repository.git
With SSH
[satish@host]$ git remote add origin ssh://user-name@server:/path/to/repo.git 

##Origin is nothing but alias

[satish@host]$git remote -v
## list all your remote repository

[satish@host]$git push origin master


Local Repository has 3 Areas:

  • Working Area
  • Staging Area
  • Commited Files
  • Stash
  • The file that should be include in commit first get added to staging area and a commit get created when developer commits the file.



    Git log commands:

    [satish@host]$ git log --max-count 1
    OR
    [satish@host]$ git log --oneline

    [satish@host]$ git log --name-only 

    #######  Reflog
    ###### if you accedently used git reset --hard HEAD~1
    [satish@host]$git reflog
    [satish@host]$git reset --hard commit_hash


    Git branch:

    A branch is basically a pointer to last commit.

    [satish@host]$ git checkout -b new_branch
    ##It will create new_branch for you from your current branch and it will switch you to that branch in a single command

    [satish@host]$git branch new_branch
    ##it will simply switch to branch new_branch

    [satish@host]$git branch
    ##see on which branch you are

    [satish@host]$git branch -d branch_name
    ## To delete the branch branch_name    

    see previous commit history along with the branch they were committed on.:

    [satish@host]$ git log --graph --decorate


    Git Merge:

    There are basically two types of merge.

    1. fast-forward : There will be no any new commits.
    2. no-fast-forward : If current branch has commits then git will perform no-fast-forward merge and it will create a new merging commit

    There is need of emergency-patch in the application, stable branch is master
    so Let's create a new branch emergency-patch
    current branch is master

    [satish@host]$ git checkout -b emergency-patch

    Made the needed changes in emergency-patch branch now need to merge this into master
    We have an emergency-patch branch so let's merge the emergency branch into master

    [satish@host]$ git checkout master
                   switched to branch 'master'

    Now i will merge the current branch (master) with emergency-fix
    OR 
    merge the emergency-patch branch into master branch

    [satish@host]$ git merge 
    emergency-patch
                   
    Updating 09f4acd..dfa79db
                   
    Fast-forward
                     
    index.html | 2 +-
                     
    1 file changed, 1 insertion(+), 1 deletion(-)

    Since the emergency-patch branch came directly from master, and no other changes had been made to master while we were working, Git sees this as a continuation of master. So it can "Fast-forward", just pointing both master and emergency-patch to the same commit.


    Merge Conflict

    If your master branch is continuosly updating and your feature is also updating and then if you try to merge feature branch into master, you may get Merge conflict

    [satish@host]$ git checkout master
    [satish@host]$ git merge feature-branch
                   Auto-merging index.html
                   CONFLICT (content): Merge conflict in index.html
                   Automatic merge failed; fix conflicts and then commit the result.



    git pull | git fetch:

    [satish@host]$ git pull = git fetch + git merge


    git rebase:

    [satish@host](sarah)$git rebase master
    ## rebase sarah branch on top of master branch
    ## it shall strealines the complex history of commits


    cherry-pick:

    aaba5 is commit of feature branch that we want to cherry pick in master branch.

    [satish@host]( master) $ git cherry-pick aaba5 


    git stash:

    [satish@host]$ git stash
    [satish@host]$ git stash pop
    [satish@host]$ git stash list
    [satish@host]$ git stash pop statsh_id


    Reseting & Reverting:

    [satish@host]$ git revert commit_hash
    ### It will undo the commit with new commit, In history it will show everything

    [satish@host]$git reset --soft HEAD~1
    ### still it will show in git status commnad

    [satish@host]$git reset --hard HEAD~1
    ### it will completely remove commit



    git revert:

    If you want to undo a changes safely

    [satish@host]$ git log --oneline

    Revert just the previous Commit

    [satish@host]$ git revert HEAD --no-edit

    To revert to earlier commits, use git revert HEAD~x (x being a number. 1 going back one more, 2 going back two more, etc.)

    [satish@host]$ git revert HEAD~x

    Git reset vs Git revert



    git amend:

    One of the simplest thing you can do with git amend command is to change the recent commit message
    [satish@host]$ git commit -m "added lines for HYML code"

    [satish@host]$ git commit --amend -m "added lines for HTML code"


    Some Important points:

    • The master branch is the final version of the story. It should always only have content that has been reviewed and approved. We cannot just allow everyone to directly push to the master branch. So let's do it the right way this time.
    • Even though you are in master branch already since the file is not tracked you can switch to a new branch and commit the file to the new branch.
    • Use a non-bare repository to work locally and a bare repository as a central server/hub to share your changes with other people. For example, when you create a repository on github.com, it is created as a bare repository.
    • It is very much important when you are working in a Team to write good & meaningful commit message so that people can understand what did you changed in this line of code.
    • If you are using issues tracker tool(most popularly JIRA) then it is always recommended to put JIRA id in that commit message. See the following example:
    • DEV-121: Implemented login function 
      DEV-127: Added admin module  
      DEV-120: fix typo and incorrect example  


    Message : Your branch is 56 commits behind, update your branch

    1st method:
    Initially stash your work or commit it to your branch.Then follow the following commands
    $ git checkout master
    $ git pull origin master
    $ git checkout BranchNameBehindCommit(your branch)
    $ git merge master // Now your branch is in sync with local Master branch
    $ git push origin BranchNameBehindCommit

    2nd method:
    $ git pull
    $ git checkout ci
    $ git pull origin development (Solve conflicts if any)
    $ git push origin ci



    How to keep a feature branch synchronized/updated with master?

    git checkout master
    git pull
    git checkout feature
    git merge master
    to keep feature in sync with master.

    Then, when you're ready to put feature into master, first, merge in master like above, then:
    git checkout master
    git merge feature
    git push origin master

    git fork

    to contribute to open source project we first fork the main project and create a feature branch in the forked copy and after developing the feature you can send the pull request to original repository so that reviewer of original repository now can review the PR and can merge the request.

    Get the name of file in which you have made changes with shell command.

    $ echo "------------------------------------"
    $ commit1=$(git log -n 1 --pretty=format:"%H")
    $ commit2=$(git log -n 2 --pretty=format:"%H" | tail -1)
    $ filename_with_folder=$(git diff --name-only $commit1 $commit2)
    $ folder=$(echo $filename_with_folder | cut -d '.' -f1)
    $ filename=$(echo $folder | cut -d '/' -f2)
    $ echo $filename


    Potential addon Topics:

    • git tag
    • ls-remote
    • git ignore

    That's all for this blog!!


    Post a Comment

    0 Comments