Git
Git#
Intro#
What?#
Why?#
- As a collaboration tool
- As a version control tool
Terminologies#
master#
origin#
remote#
url#
Setup & Config#
config
#
CRLF, LF, CR#
Windows Only#
- Then turn off automatical convertions, and stick to CRLF only
1 |
|
Linux/Mac Only#
- If on
LF
system, then do this to convert any CRLF to LF - but not LF to CRLF
1 |
|
If Both#
- Then set this in Windows system only
- auto-converting CRLF line endings into LF when you add a file to the index
- and vice versa when it checks out code onto your filesystem
- hence Git will be having LF but windows filesystem will always have CRLF
1 |
|
Inspection & Comparision#
diff
#
Compare local with remote branch#
1 2 3 4 5 |
|
diff-tree
#
List all the files in a commit#
1 |
|
show
#
List all the files in a commit#
1 |
|
Commands#
init#
--bare
#
- Source: http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/
clone#
-
Normal Way
git clone url:repo
-
Clone a specific release
git clone url:repo --branch <tag#>
pull#
git pull origin master
git pull origin master -f
git pull --rebase origin master
add#
rm#
commit#
- commit -m "message"
- commit --amend
- to re-phrase the commit message; iff commit has not been pushed
push#
checkout#
status#
log#
To see in descriptive format
1 |
|
where:
--graph: shows flow
--decorate: shows branch names
--oneline: compact description in single line
reflog#
To see in short
reset#
Uses#
Branching#
- Lets say you have 2 branches:
- master
- dev
-
Options:
- create a new branch dev
1
git checkout -b dev
- dev is active
1
git checkout dev
- master is active
1
git checkout master
- dev is active & incorporate master in it
1 2
git checkout dev git merge master
-
master is active & incorporate dev in it
1 2
git checkout master git merge dev
-
delete a local branch if completely merged
1
git branch -d dev
-
delete a local branch if not merged
1
git branch -D dev
-
delete a remote branch
1
git push origin :dev
-
Rename your local branch.
- If you are on the branch you want to rename:
1
git branch -m new-name
- If you are on a different branch:
1
git branch -m old-name new-name
- If you are on the branch you want to rename:
-
Delete the old-name remote branch and push the new-name local branch.
1
git push origin :old-name new-name
-
Reset the upstream branch for the new-name local branch.
- Switch to the branch and then:
1
git push origin -u new-name
- Switch to the branch and then:
- create a new branch dev
-
Golden rules
- first incorporate master into dev
- check compatibility/bugs/conflicts & resolve them
- now incorporate/merge dev in master
Merging#
merge Vs rebase#
- Both are used for same purpose but with different approach
- Integrates changes from one branch to another
merge#
- Lets say, if you're woring on a feature in a dedicated branch. And someone makes a commit in master branch and you want to pull those changes to your feature branch, i.e.
1 2 3 4 5 |
|
then:
1 2 |
|
1 |
|
1 2 3 4 5 |
|
- Very active master branch can pollute the feature branch.
rebase:#
- Alternative to merging
- rebases feature branch onto master branch
1 2 |
|
** shifts the entire feature branch to tip/(latest node) of the master branch.** i.e.
1 2 3 4 5 |
|
- instead of creating "merge commit", it creates brand new commits in feature branch for all the commits created earlier on the feature branch (the changes will same, but with new commit infos)
- i.e. re-writes branch history
- Benifits over merge
- gives cleaner history log by avoiding un-necessary "merge commits"
- results in perfectly linear project history
Golden rules to use rebase: * never use rebase on any public branch: Means, don't rebase the master branch onto feature branch (it is opposite of rebasing feature branch onto master branch). It will create a brand new commit/history in master branch which will affect other developers.
rebase one branch on the top of another#
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 |
|
Reverting back to old commits#
https://stackoverflow.com/questions/4114095/how-to-revert-a-git-repository-to-a-previous-commit
Reseting#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Modify last commit Message#
1 |
|
Modify a particular commit#
If you want to modify files at commit
1 |
|
You will get a editor opened up with list of all the commits prior to that.
Change the pick
to edit
for that particular commit
Make changes to your files.
After that add or rm files.
If you want to change the commit msg also then
1 |
|
Then continue rebasing
1 |
|
Squashing last x commits into one#
If you want to squash last X commits into single commit then
1 |
|
You will get a editor opened up with list of all the X commits.
Change the pick
to s
for all the commits except the oldest one into which you want to merge all the lastest.
Save & close the editor.
Again you'll get an editor with list & order of all the squashed commits. (if you want you can change the commit msgs at this moment)
Then continue rebasing
1 |
|
Done.
Misc#
Travis CI (Continue Integration)#
Continuous Integration is the practice of merging in small code changes frequently - rather than merging in a large change at the end of a development cycle. The goal is to build healthier software by developing and testing in smaller increments.
Prerequisites#
To start using Travis CI, make sure you have all of the following:
- GitHub login
- Project hosted as a repository on GitHub
- Working code in your project
- Working build or test script
Steps to Integrate CI#
- Using github login to
- TravisCI.org for public repositories
- TravisCI.com for private repositories
- Enable the repo in CI portal
-
Add
.travis.yml
file to the repo to tell Travis CI what to do- e.g. ```yaml
language: python python: - "3.6.4"
command to install dependencies#
install: # - pip install -r requirements.pip - pip install pipenv - "pipenv install --dev"
command to run tests#
script: # - pytest src/test.py # or py.test for Python versions 3.5 and below - pipenv run pytest src/test.py # or py.test for Python versions 3.5 and below ```
-
Infrastructure options
```yamlos: linux
dist: trusty
sudo: enabled
```
-
Add the
.travis.yml
file to git, commit and push, to trigger a Travis CI build- Travis only runs builds on the commits you push after you’ve enabled the repository in Travis CI.
- check the build page
- embbed build status on github page
Setup SSH key#
Generate or Get ssh key#
Generate#
1 |
|
Set paraphrase#
Get#
1 |
|
Add the key to the ssh-agent#
Start#
1 |
|
Add#
1 |
|
Provide paraphrase#
Add the public key to your VCS portal settings#
Copy hash of public key#
1 2 3 4 5 |
|
Paste in settings#
Update remotes with ssh url#
1 2 3 |
|