Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Thursday, 9 March 2017

Basic Git command List – the simple guide

Harry

GIT CREATE

Clone an existing repository
 $ git clone ssh://user@domain.com/repo.git

Create a new local repository
 $ git init

 LOCAL CHANGES

View Changed files in your working directory
 $ git status

View Changes to tracked files
$ git diff

To Add all current changes to the next commit
$ git add .

To Add some changes in <file> to the next commit
$ git add -p <file>

Commit all local changes in tracked files
$ git commit -a


Commit previously staged changes
$ git commit


Change the last commit

Don‘t amend published commits!
$ git commit --amen

COMMIT HISTORY

Show all commits, starting with newest
$ git log

Show changes over time for a specific file
$ git log -p <file>

Who changed what and when in <file>
$ git blame <file>

BRANCHES & TAGS

List all existing branches
$ git branch -av

Switch HEAD branch
$ git checkout <branch>

Create a new branch based

on your current HEAD
$ git branch <new-branch>

Create a new tracking branch based on a remote branch
$ git checkout --track <remote/branch>

Delete a local branch
$ git branch -d <branch>

Mark the current commit with a tag
$ git tag <tag-name>

UPDATE & PUBLISH

List all currently configured remotes
$ git remote -v

Show information about a remote
$ git remote show <remote>

Add new remote repository, named <remote>
$ git remote add <shortname> <url>

Download all changes from <remote>,
but don‘t integrate into HEAD
$ git fetch <remote>

Download changes and directly merge/integrate into HEAD
$ git pull <remote> <branch>

Publish local changes on a remote
$ git push <remote> <branch>

Delete a branch on the remote
$ git branch -dr <remote/branch>

Publish your tag s
$ git push --tags

MERGE & REBASE

Merge <branch> into your current HEAD
$ git merge <branch>

Rebase your current HEAD onto <branch>
Don‘t rebase published commits!
$ git rebase <branch>

Abort a rebase
$ git rebase --abort

Continue a rebase after resolving conflicts
$ git rebase --continue

Use your configured merge tool to solve conflicts
$ git mergetool

Use your editor to manually solve conflicts and (after resolving) mark file as resolved
$ git add <resolved-file>

$ git rm <resolved-file>

UNDO

Discard all local changes in your working  directory
$ git reset --hard HEAD

Discard local changes in a specific file
$ git checkout HEAD <file>

Revert a commit (by producing a new commit with contrary changes)
$ git revert <commit>

Reset your HEAD pointer to a previous commit and discard all changes since then
$ git reset --hard <commit>

preserve all changes as unstaged changes
$ git reset <commit>

preserve uncommitted local changes
 $ git reset --keep <commit>

Thursday, 9 February 2017

How to Install Gitlab on CentOS/RHEL 5/6/7

Harry
GitLab is a web-based Git repository manager and issue tracking features. GitLab is similar to GitHub, but GitLab has an open source version, unlike GitHub. Git repository management, code reviews, issue tracking, activity feeds and wikis. It comes with GitLab CI for continuous integration and delivery.


This article will help you to install Gitlab on CentOS/RHEL using Omnibus install method. The Omnibus project is a full-stack platform-specific solution.

Step 1: Install and Configure the necessary dependencies

You need to configure mail service on our server. We can use any mail service like postfix, sendmail, exim etc. In this article I am using postfix email service.
# yum install postfix 
# service postfix start
# chkconfig postfix on
# lokkit -s http -s ssh

Step 2: Install other dependencies

Now you need to install other dependencies packages. You following command to install dependencies:
# yum install curl openssh-server cronie

Step 3: Install GitLab package on Server

Use following command to install GitLab packages on server.
# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | # bash
# yum install gitlab-ce
If you are not comfortable installing the repository through a piped script, you can find the entire script here.

Step 4: Change External URl

If you want to change the external url the use follow below steps:
# vim /etc/gitlab/gitlab.rb
external_url 'http://host.domain.com'

Step 5: Configure GitLab on Server

# gitlab-ctl reconfigure

Step 6: Acces and login GitLab

You can browse GitLab from your browser using server IP or hostname. Use below details to login in GitLab:
http://192.168.10.55
or
http://hostname
Username: root 
Password: 5iveL!fe
gitlab
gitlab1
Note: If you do any changes in configuration file than you need to run reconfigure command to make the changes.Use following command to reconfigure:
# gitlab-ctl reconfigure

GitLab Detail:

Main Configuration File: /var/opt/gitlab/gitlab-rails/etc/gitlab.yml
GitLab Document Root: /opt/gitlab
Default Repository Location: /var/opt/gitlab/git-data/repositories
Default Nginx Configuration File: /opt/gitlab/embedded/conf/nginx.conf
GitLab Nginx Configuration file Location: /var/opt/gitlab/nginx/conf
Postgresql data Directory: /var/opt/gitlab/postgresql/data

Thursday, 2 February 2017

Git Cheatsheet

Anonymous


Git is one of the, if the not the, most popular version control systems available. Originally created by Linus Torvalds to help manage the Linux source code, it's now used by millions of projects across all languages.

Trying to remember all those commands to perform common git tasks can be a bit of a nightmare, so we've created this Git cheat sheet of the most common commands so you can print it out as a quick reference to have at your desk.

Creating Repositories


# create new repository in current directory
git init

# clone a remote repository
git clone [url]
# for example cloning the entire jquery repo locally
git clone https://github.com/jquery/jquery

Branches and Tags


# List all existing branches with the latest commit comment 
git branch –av

# Switch your HEAD to branch
git checkout [branch]

# Create a new branch based on your current HEAD
git branch [new-branch]

# Create a new tracking branch based on a remote branch
git checkout --track [remote/branch]
# for example track the remote branch named feature-branch-foo
git checkout --track origin/feature-branch-foo

# Delete a local branch
git branch -d [branch]

# Tag the current commit
git tag [tag-name]

Local Changes


# List all new or modified files - showing which are to staged to be commited and which are not 
git status

# View changes between staged files and unstaged changes in files
git diff

# View changes between staged files and the latest committed version
git diff --cached
# only one file add the file name
git diff --cached [file]

# Add all current changes to the next commit
git add [file]

# Remove a file from the next commit
git rm [file]

# Add some changes in < file> to the next commit
# Watch these video's for a demo of the power of git add -p - http://johnkary.net/blog/git-add-p-the-most-powerful-git-feature-youre-not-using-yet/
git add -p [file]

# Commit all local changes in tracked  files
git commit –a
git commit -am "An inline  commit message"

# Commit previously staged changes
git commit
git commit -m "An inline commit message"

# Unstages the file, but preserve its contents

git reset [file]

Commit History


# Show all commits, starting from the latest 
git log 

# Show changes over time for a specific file 
git log -p [file]

# Show who changed each line in a file, when it was changed and the commit id
git blame -c [file]

Update and Publish


# List all remotes 
git remote -v

# Add a new remote at [url] with the given local name
git remote add [localname] [url]

# Download all changes from a remote, but don‘t integrate into them locally
git fetch [remote]

# Download all remote changes and merge them locally
git pull [remote] [branch]

# Publish local changes to a remote 
git push [remote] [branch]

# Delete a branch on the remote 
git branch -dr [remote/branch]

# Publish your tags to a remote
git push --tags

Merge & Rebase


# Merge [branch] into your current HEAD 
git merge [branch]

# Rebase your current HEAD onto [branch]
git rebase [branch]

# Abort a rebase 
git rebase –abort

# Continue a rebase after resolving conflicts 
git rebase –continue

# Use your configured merge tool to solve conflicts 
git mergetool

# Use your editor to manually solve conflicts and (after resolving) mark as resolved 
git add <resolved- file>
git rm <resolved- file>

Undo


# Discard all local changes and start working on the current branch from the last commit
git reset --hard HEAD

# Discard local changes to a specific file 
git checkout HEAD [file]

# Revert a commit by making a new commit which reverses the given [commit]
git revert [commit]

# Reset your current branch to a previous commit and discard all changes since then 
git reset --hard [commit]

# Reset your current branch to a previous commit and preserve all changes as unstaged changes 
git reset [commit]

#  Reset your current branch to a previous commit and preserve staged local changes 
git reset --keep [commit]

Git Tutorial: 10 Common Git Problems and How to Fix Them

Anonymous




Learning Git?  This Git tutorial covers the 10 most common Git tricks you should know about: how to undo commits, revert commits, edit commit messages, discard local files, resolve merge conflicts, and more.

1. Discard local file modifications

Sometimes the best way to get a feel for a problem is diving in and playing around with the code. Unfortunately, the changes made in the process sometimes turn out to be less than optimal, in which case reverting the file to its original state can be the fastest and easiest solution:
  git checkout -- Gemfile  # reset specified path
  git checkout -- lib bin  # also works with multiple arguments  
In case you’re wondering, the double dash (--) is a common way for command line utilities to signify the end of command options.

2. Undo local commits

Alas, sometimes it takes us a bit longer to realize that we are on the wrong track, and by that time one or more changes may already have been committed locally. This is when git reset comes in handy:
  git reset HEAD~2        # undo last two commits, keep changes
  git reset --hard HEAD~2 # undo last two commits, discard changes  
Be careful with the --hard option! It resets your working tree as well as the index, so all your modifications will be lost for good.

3. Remove a file from git without removing it from your file system

If you are not careful during a git add, you may end up adding files that you didn’t want to commit. However, git rm will remove it from both your staging area, as well as your file system, which may not be what you want. In that case make sure you only remove the staged version, and add the file to your .gitignore to avoid making the same mistake a second time:
  git reset filename          # or git remove --cached filename
  echo filename >> .gitingore # add it to .gitignore to avoid re-adding it  

4. Edit a commit message

Typos happen, but luckily in the case of commit messages, it is very easy to fix them:
  git commit --amend                  # start $EDITOR to edit the message
  git commit --amend -m "New message" # set the new message directly
But that’s not all git-amend can do for you. Did you forget to add a file? Just add it and amend the previous commit!
  git add forgotten_file
  git commit --amend
Please keep in mind that --amend actually will create a new commit which replaces the previous one, so don’t use it for modifying commits which already have been pushed to a central repository. An exception to this rule can be made if you are absolutely sure that no other developer has already checked out the previous version and based their own work on it, in which case a forced push (git push --force) may still be ok. The --force option is necessary here since the tree’s history was locally modified which means the push will be rejected by the remote server since no fast-forward merge is possible.

5. Clean up local commits before pushing

While --amend is very useful, it doesn’t help if the commit you want to reword is not the last one. In that case an interactive rebase comes in handy:
  git rebase --interactive
  # if you didn't specify any tracking information for this branch
  # you will have to add upstream and remote branch information:
  git rebase --interactive origin branch  
This will open your configured editor and present you with the following menu:
  pick 8a20121 Upgrade Ruby version to 2.1.3
  pick 22dcc45 Add some fancy library

  # Rebase fcb7d7c..22dcc45 onto fcb7d7c
  #
  # Commands:
  #  p, pick = use commit
  #  r, reword = use commit, but edit the commit message
  #  e, edit = use commit, but stop for amending
  #  s, squash = use commit, but meld into previous commit
  #  f, fixup = like "squash", but discard this commit's log message
  #  x, exec = run command (the rest of the line) using shell
  #
  # These lines can be re-ordered; they are executed from top to bottom.
  #
  # If you remove a line here THAT COMMIT WILL BE LOST.
  #
  # However, if you remove everything, the rebase will be aborted.
  #
  # Note that empty commits are commented out
On top you’ll see a list of local commits, followed by an explanation of the available commands. Just pick the commit(s) you want to update, change pick to reword (or r for short), and you will be taken to a new view where you can edit the message.
However, as can be seen from the above listing, interactive rebases offer a lot more than simple commit message editing: you can completely remove commits by deleting them from the list, as well as edit, reorder, and squash them. Squashing allows you to merge several commits into one, which is something I like to do on feature branches before pushing them to the remote. No more “Add forgotten file” and “Fix typo” commits recorded for eternity!

6. Reverting pushed commits

Despite the fixes demonstrated in the previous tips, faulty commits do occasionally make it into the central repository. Still this is no reason to despair, since git offers an easy way to revert single or multiple commits:
  git revert c761f5c              # reverts the commit with the specified id
  git revert HEAD^                # reverts the second to last commit
  git revert develop~4..develop~2 # reverts a whole range of commits
In case you don’t want to create additional revert commits but only apply the necessary changes to your working tree, you can use the --no-commit/-n option.
  # undo the last commit, but don't create a revert commit
  git revert -n HEAD
The manual page at man 1 git-revert list further options and provides some additional examples.

7. Avoid repeated merge conflicts

As every developer knows, fixing merge conflicts can be tedious, but solving the exact same conflict repeatedly (e.g. in long running feature branches) is outright annoying. If you’ve suffered from this in the past, you’ll be happy to learn about the underused reuse recorded resolution feature. Add it to your global config to enable it for all projects:
  git config --global rerere.enabled true
Alternatively you can enable it on a per-project basis by manually creating the directory .git/rr-cache.
This sure isn’t a feature for everyone, but for people who need it, it can be real time saver. Imagine your team is working on various feature branches at the same time. Now you want to merge all of them together into one testable pre-release branch. As expected, there are several merge conflicts, which you resolve. Unfortunately it turns out that one of the branches isn’t quite there yet, so you decide to un-merge it again. Several days (or weeks) later when the branch is finally ready you merge it again, but thanks to the recorded resolutions, you won’t have to resolve the same merge conflicts again.
The man page (man git-rerere) has more information on further use cases and commands (git rerere status, git rerere diff, etc).

8. Find the commit that broke something after a merge

Tracking down the commit that introduced a bug after a big merge can be quite time consuming. Luckily git offers a great binary search facility in the form of git-bisect. First you have to perform the initial setup:
  git bisect start         # starts the bisecting session
  git bisect bad           # marks the current revision as bad
  git bisect good revision # marks the last known good revision
After this git will automatically checkout a revision halfway between the known “good” and “bad” versions. You can now run your specs again and mark the commit as “good” or “bad” accordingly.
  git bisect good # or git bisec bad
This process continues until you get to the commit that introduced the bug.

9. Avoid common mistakes with git hooks

Some mistakes happen repeatedly, but would be easy to avoid by running certain checks or cleanup tasks at a defined stage of the git workflow. This is exactly the scenario that hooks were designed for. To create a new hook, add an executable file to .git/hooks. The name of the script has to correspond to one of the available hooks, a full list of which is available in the manual page (man githooks). You can also define global hooks to use in all your projects by creating a template directory that git will use when initializing a new repository (see man git-init for further information). Here’s how the relevant entry in ~/.gitconfig and an example template directory look like:
  [init]
    templatedir = ~/.git_template
  

  
  → tree .git_template
  .git_template
  └── hooks
      └── pre-commit  
When you initialize a new repository, files in the template directory will be copied to the corresponding location in your project’s .git directory.
What follows is a slightly contrived example commit-msg hook, which will ensure that every commit message references a ticket number like “#123“.
  ruby
  #!/usr/bin/env ruby
  message = File.read(ARGV[0])

  unless message =~ /\s*#\d+/
    puts "[POLICY] Your message did not reference a ticket."
    exit 1
  end

10. When all else fails

So far we covered quite a lot of ground on how to fix common errors when working with git. Most of them have easy enough solutions, however there are times when one has to get out the big guns and rewrite the history of an entire branch. One common use case for this is removing sensitive data (e.g. login credentials for production systems) that were committed to a public repository:
  git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch secrets.txt' \
  --prune-empty --tag-name-filter cat -- --all
This will remove the file secrets.txt from every branch and tag. It will also remove any commits that would be empty as a result of the above operation. Keep in mind that this will rewrite your project’s entire history, which can be very disruptive in a distributed workflow. Also while the file in question has now been removed, the credentials it contained should still be considered compromised!

Monday, 30 January 2017

How to Fixed .gitignore file if not working

Anonymous


If you are an user of the GIT version control then you must be aware of the .gitignore file. For the ones how are not aware of this file, .gitignore helps you ignore or avoid certain files from being committed into the main repository using GIT.
Now it happens at times that the .gitignore file behaves weirdly and GIT fails to ignore all the file names listed in the .gitignore file and as a result all these files which you do not want to be committed into the repository start getting committed. And when your .gitignore file is not working, its a big mess!
A very very quick fix to this problem would be to get rid of any trailing whitespace in the .gitignore file, if you find one. Also, you should not put any comments next to the listed file in the .gitignore
If all this still does not solve your problem, follow these steps:
Step 1: Commit all your pending changes in the repo which you want to fix.
Step 2: Now you need to remove everything from the git index in order to refresh your git repository. This is safe. Use this command:
git rm -rf --cached .
Step 3: Now you need to add everything back into the repo, which can be done using this command:
git add .
Step 4: Finally you need to commit these changes, using this command:
git commit -m ".gitignore Fixed"
Please let us know if you were able to fix .gitignore using these steps.


Ignore changes to committed files

Temporarily ignore changes

During development it's convenient to stop tracking file changes to a file committed into your git repo. This is very convenient when customizing settings or configuration files that are part of your project source for your own work environment.
> git update-index --assume-unchanged <file>
Resume tracking files with:
> git update-index --no-assume-unchanged <file>

Permanently ignore changes to a file

If a file is already tracked by Git, adding that file to your .gitignore is not enough to ignore changes to the file. You also need to remove the information about the file from Git's index:
These steps will not delete the file from your system. They just tell Git to ignore future updates to the file.
  1. Add the file in your .gitignore.
  2. Run the following:
    > git rm --cached <file>
    
  3. Commit the removal of the file and the updated .gitignore to your repo.

How to add gitignore file in our Git Project

Anonymous

Here is Step by Step Tutorial to Create git ignore file for our git project.

For Ubuntu / unix os 

$ touch .gitignore 

Now Open  .gitignore using your Text Editor and  copy below  Code ( pattern) and paste in .gitignore file..
Copy and paste into .gitignore files.
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# PHP IGNORE
#/inc/log.txt
/logs/log.txt
/logs/*.php
/*.txt
*.txt
test.php




# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
 

  How to Fixed Gitignore if not Working properly.


 
 
 

Sunday, 22 January 2017

How to Use Git Step by Step using Command line

Anonymous
For Example :  I am a developer  and my git Details is below.
User name : Harry
User Email : Harry@India.com ( Git Email)
Lets suppose I want to  work on TicketTool Branch. So TicketTool branch is live for me. I should not Modified TicketTool Files Directly. So We have to Create local Branch from TicketTool. Here we have created local branch issue-2 from TicketTool. Now we can modify any files in my local Branch( issue-2).
 Live Branch :-  TicketTool.
Local Branch:-  issue-2
If you want to initialize new clone/ Project Follow bellow Command.
 #Git global setup ( one Time only)
 git config --global user.name "Hari"
 git config --global user.email "Harry@india.com"
 git clone  http://ubuntu/harry/itsupport.git   // project url
  cd  MyProject    // project folder
   
# This command will use many times.
    git checkout TicketTool            // live Branch
    git pull            // Pull for latest update.   
    git checkout issue-2     // Working branch / local Branch.
    git merge TicketTool      // if your local live branch is already uptodated. Then Not Required.



//After Completing above 4 Command now we can start our work. When our work will done. Then we have to Run below commands.

    git add *   // mark all changes for commit
    git commit  // commit / save  project changes on your local machine.



// if you run above Two commands it means your Work is Committed ( Saved)  on your local system. but we have to push our work on git server. So we have to follow below commands once more.

    git checkout TicketTool                           
    git pull
    git checkout issue-2     // Working branch
    git merge TicketTool








// above 4 commands are used for taking latest update and merge into our local branch ( issue2).   Finaly here  is command to Push our work on git Server.

    git push issue-2

if you have pushed  your work successfully  on git server . now create merge request from web login. ( if required)