Hello Everyone,
Please find git Lab and excercise session which has 5 sets to practice the git.
If you want to add any new addition, do let me know in comment section.
Hello Everyone,
Please find git Lab and excercise session which has 5 sets to practice the git.
If you want to add any new addition, do let me know in comment section.
Bitbucket formerly knows as stash belongs to one of the best software solution provider Atlassian who also developed popular tools like JIRA, Bamboo & Confluence. It offers both plans free & Commercial. Bitbucket is written in python.
Visual studio team services formerly known as Visual Studio Online is a Microsoft product which is launched in the year 2012. Visual studio is also a very good option for Git hosting. This platform includes not only code hosting but it has its own CI & agile planning tools for scrum and kanban teams. If you have 5 members team than you can use there most features for free but you need to upgrade your plans if you want to increase that number.
Key Features
Git is a distributed version control system and source code management (SCM) system with focus to handle small and large projects source code versions in the local repository with speed and efficiency. It is free and open source and its one of widly used versioning tools used ever worldwide.
A repository in git, consists of .git directory which contains the each source code commited in form of objects created using SHA1 algorithms. A .git directry where git keeps all of its metadata for the source code in objects forms. It also contains the git configuration file, breanch reference and staging state of the work space.
The command that is used to write a commit message is “git commit –m”this is reason for commit”. The –a on the command line instructs git to commit the new content of all tracked files that have been modified. You can use “git add <file>” before git commit –a if new files need to be committed for the first time.
In nutshell, any new changes has to added from working directory to stageing area and then commit from staging area to reposiory. Please refer the image for the same as below;
The difference between GIT and SVN is
GIT is fast, and ‘C’ language makes this possible by reducing the overhead of runtimes associated with higher languages.
‘GIT PUSH’ updates remote refs along with associated objects.
GIT is an open source version control system; it will allow you to run ‘versions’ of a project, which show the changes that were made to the code overtime also it allows you keep the backtrack if necessary and undo those changes. Multiple developers can checkout, and upload changes and each change can then be attributed to a specific developer.
Before completing the commits, it can be formatted and reviewed in an intermediate area known as ‘Staging Area’ or ‘Index’.
GIT stash takes the current state of the working directory and index and puts in on the stack for later and gives you back a clean working directory. So in case if you are in the middle of something and need to jump over to the other job, and at the same time you don’t want to lose your current edits then you can use GIT stash.
When you are done with the stashed item or want to remove it from the list, run the git ‘stash drop’ command. It will remove the last added stash item by default, and it can also remove a specific item if you include as an argument.
Git branch—merged lists the branches that have been merged into the current branch
Git branch—no merged lists the branches that have not been merged
The git clone command creates a copy of an existing Git repository. To get the copy of a central repository, ‘cloning’ is the most common way used by programmers.
The ‘git config’ command is a convenient way to set configuration options for your Git installation. Behaviour of a repository, user info, preferences etc. can be defined through this command.
In Git, to create a repository, create a directory for the project if it does not exist, and then run command “git init”. By running this command .git directory will be created in the project directory, the directory does not need to be empty.
A ‘head’ is simply a reference to a commit object. In every repository, there is a default head referred as “Master”. A repository can contain any number of heads.
The purpose of branching in GIT is that you can create your own branch and jump between those branches. It will allow you to go to your previous work keeping your recent work intact.
The common way of creating branch in GIT is to maintain one as “Main“
branch and create another branch to implement new features. This pattern is particularly useful when there are multiple developers working on a single project.
To bring a new feature in the main branch, you can use a command “git merge” or “git pull command”.
A ‘conflict’ arises when the commit that has to be merged has some change in one place, and the current commit also has a change at the same place. Git will not be able to predict which change should take precedence.
To resolve the conflict in git, edit the files to fix the conflicting changes and then add the resolved files by running “git add” after that to commit the repaired merge, run “git commit”. Git remembers that you are in the middle of a merger, so it sets the parents of the commit correctly.
Once your development branch is merged into the main branch, you don’t need development branch. To delete a branch use, the command “git branch –d [head]”.
“Rebasing” is an alternative to merging in git.
The syntax used for rebase is “git rebase [new-commit] “
‘git remote add’ just creates an entry in your git config that specifies a name for a particular URL. While, ‘git clone’ creates a new git repository by copying and existing one located at the URI.
With the help of GIT version control, you can track the history of a collection of files and includes the functionality to revert the collection of files to another version. Each version captures a snapshot of the file system at a certain point of time. A collection of files and their complete history are stored in a repository.
Some of the best GIT client for LINUX is
‘Subgit’ is a tool for a smooth, stress-free SVN to Git migration. Subgit is a solution for a company -wide migration from SVN to Git that is:
a) It is much better than git-svn
b) No requirement to change the infrastructure that is already placed
c) Allows to use all git and all sub-version features
d) Provides genuine stress –free migration experience.
‘git diff ’ shows the changes between commits, commit and working tree etc.
As ‘Git Status’ shows you the difference between the working directory and the index, it is helpful in understanding a git more comprehensively.
‘git diff’ is similar to ‘git status’, but it shows the differences between various commits and also between the working directory and index.
A ‘git checkout’ command is used to update directories or specific files in your working tree with those from another branch without merging it in the whole branch.
To remove the file from the staging area and also off your disk ‘git rm’ is used.
When you want to continue working where you have left your work, ‘git stash apply’ command is used to bring back the saved changes onto the working directory.
To find specific commits in your project history- by author, date, content or history ‘git log’ is used.
‘git add’ adds file changes in your existing directory to your index.
The function of ‘Git Reset’ is to reset your index as well as the working directory to the state of your last commit.
‘git Is-tree’ represents a tree object including the mode and the name of each item and the SHA-1 value of the blob or the tree.
‘Git Instaweb’ automatically directs a web browser and runs webserver with an interface into your local repository.
This directory consists of Shell scripts which are activated after running the corresponding Git commands. For example, git will try to execute the post-commit script after you run a commit.
Commit message is a feature of git which appears when you commit a change. Git provides you a text editor where you can enter the modifications made in commits.
To fix any broken commit, you will use the command “git commit—amend”. By running this command, you can fix the broken commit message in the editor.
There are couple of reason
To co-ordinate with the distributed development and developers team, especially when you are working on a project from multiple computers ‘Bare Repository’ is used. A bare repository comprises of a version history of your code.
One or more commits can be reverted through the use of git revert. This command, in essence, creates a new commit with patches that cancel out the changes introduced in specific commits. In case the commit that needs to be reverted has already been published or changing the repository history is not an option, git revert can be used to revert commits. Running the following command will revert the last two commits:
git revert HEAD~2..HEAD
Alternatively, one can always checkout the state of a particular commit from the past, and commit it anew.
Squashing multiple commits into a single commit will overwrite history, and should be done with caution. However, this is useful when working in feature branches. To squash the last N commits of the current branch, run the following command (with {N} replaced with the number of commits that you want to squash):
git rebase -i HEAD~{N}
Upon running this command, an editor will open with a list of these N commit messages, one per line. Each of these lines will begin with the word “pick”. Replacing “pick” with “squash” or “s” will tell Git to combine the commit with the commit before it. To combine all N commits into one, set every commit in the list to be squash except the first one. Upon exiting the editor, and if no conflict arises, git rebase will allow you to create a new commit message for the new combined commit.
git diff-tree -r {hash}
Given the commit hash, this will list all the files that were changed or added in that commit. The -r flag makes the command list individual files, rather than collapsing them into root directory names only.
The output will also include some extra information, which can be easily suppressed by including a couple of flags:
git diff-tree –no-commit-id –name-only -r {hash}
Here –no-commit-id will supress the commit hashes from appearing in the output, and –name-only will only print the file names, instead of their paths.
To configure a script to run every time a repository receives new commits through push, one needs to define either a pre-receive, update, or a post-receive hook depending on when exactly the script needs to be triggered.
Pre-receive hook in the destination repository is invoked when commits are pushed to it. Any script bound to this hook will be executed before any references are updated. This is a useful hook to run scripts that help enforce development policies.
Update hook works in a similar manner to pre-receive hook, and is also triggered before any updates are actually made. However, the update hook is called once for every commit that has been pushed to the destination repository.
Finally, post-receive hook in the repository is invoked after the updates have been accepted into the destination repository. This is an ideal place to configure simple deployment scripts, invoke some continuous integration systems, dispatch notification emails to repository maintainers, etc.
Hooks are local to every Git repository and are not versioned. Scripts can either be created within the hooks directory inside the “.git” directory, or they can be created elsewhere and links to those scripts can be placed within the directory.
Git provides a rather efficient mechanism to find bad commits. Instead of making the user try out every single commit to find out the first one that introduced some particular issue into the code, git bisect allows the user to perform a sort of binary search on the entire history of a repository.
By issuing the command git bisect start, the repository enters bisect mode. After this, all you have to do is identify a bad and a good commit:
git bisect bad # marks the current version as bad
git bisect good {hash or tag} # marks the given hash or tag as good, ideally of some earlier commit
Once this is done, Git will then have a range of commits that it needs to explore. At every step, it will checkout a certain commit from this range, and require you to identify it as good or bad. After which the range will be effectively halved, and the whole search will require a lot less number of steps than the actual number of commits involved in the range. Once the first bad commit has been found, or the bisect mode needs to be ended, the following command can be used to exit the mode and reset the bisection state:
git bisect reset
In Git each commit is given a unique hash. These hashes can be used to identify the corresponding commits in various scenarios (such as while trying to checkout a particular state of the code using the git checkout {hash} command).
Additionally, Git also maintains a number of aliases to certain commits, known as refs. Also, every tag that you create in the repository effectively becomes a ref (and that is exactly why you can use tags instead of commit hashes in various git commands). Git also maintains a number of special aliases that change based on the state of the repository, such as HEAD, FETCH_HEAD, MERGE_HEAD, etc.
Git also allows commits to be referred as relative to one another. For example, HEAD~1 refers to the commit parent to HEAD, HEAD~2 refers to the grandparent of HEAD, and so on. In case of merge commits, where the commit has two parents, ^ can be used to select one of the two parents, e.g. HEAD^2 can be used to follow the second parent.
And finally, refspecs. These are used to map local and remote branches together. However, these can be used to refer to commits that reside on remote branches allowing one to control and manipulate them from a local Git environment.
In simple words, git rebase allows one to move the first commit of a branch to a new starting location. For example, if a feature branch was created from master, and since then the master branch has received new commits, git rebase can be used to move the feature branch to the tip of master. The command effectively will replay the changes made in the feature branch at the tip of master, allowing conflicts to be resolved in the process. When done with care, this will allow the feature branch to be merged into master with relative ease and sometimes as a simple fast-forward operation.
This can be done with a simple script bound to the pre-commit hook of the repository. The pre-commit hook is triggered right before a commit is made, even before you are required to enter a commit message. In this script one can run other tools, such as linters and perform sanity checks on the changes being committed into the repository. For example, the following script:
#!/bin/sh
files=$(git diff –cached –name-only –diff-filter=ACM | grep ‘.go$’)
if [ -z files ]; then
exit 0
fi
unfmtd=$(gofmt -l $files)
if [ -z unfmtd ]; then
exit 0
fi
echo “Some .go files are not fmt’d”
exit 1
… checks to see if any .go file that is about to be commited needs to be passed through the standard Go source code formatting tool gofmt. By exiting with a non-zero status, the script effectively prevents the commit from being applied to the repository.
This saves you time and lets you incorporate your project into the Continuous Integration (CI) process.
API URL – If you server is github.com, your “API URL” would be “https://api.github.com” Otherwise if you use GitHub Enterprise, specify its API endpoint here (e.g., https://ghe.acme.com/api/v3/).
Now you do the changes and commit , you will see jenkins build get trigger automatically. Don’t forget to do the settings in jenkins jobs to start the build when push code in github.
Reference
export GIT_CURL_VERBOSE=1
git push -u origin –all –verbose
git config –list
&
GIT_CURL_VERBOSE=1 git push
&
export GIT_CURL_VERBOSE=1
git push
git config --global http.postBuffer
There are useful to debug, long running Git Commands or Git Commands that seem to be hanged for some reason,
Git has an in-built functionality for us to peek into what is running behind the scenes of a git command, just add GIT_TRACE=1 before ANY git command to get additional info, for example:
Other Flags that we can use are : GIT_CURL_VERBOSE=1, -v or –verbose
[server@user sp-server-branches]$ GIT_TRACE=1 git clone
https://github.com/gitlabhq/gitlab-public-wiki/wiki/Trouble-Shooting-Guide
https://drupal.org/node/1065850
http://mattberther.com/2013/12/29/pushing-large-git-repos-with-ssh
http://ocaoimh.ie/2008/12/10/how-to-fix-ssh-timeout-problems/
BASH aliases for GIT
Following BASH aliases can be used in ~/.BASHRC
alias g = “git status”
alias ga = “git add”
alias gaa = “git add .”
alias gc = “git commit -m”
alias gca = “git commit -am”
alias gb = “git branch”
alias gbd = “git branch -d”
alias gco = “git checkout”
alias gcob = “git checkout -b”
alias gm = “git merge”
alias gr = “git rebase”
alias gl = “git log”
alias gs = “git show”
alias gd = “git diff”
alias gbl = “git blame”
alias gps = “git push”
alias gpl = “git pull”