Exploring Git
Git is an open source DVCS that manages set of files as they change over time.
How is it different from centralised VCM?
A DVCM is secure as multiple users can push the code easily as they get copies of repos. If you lose backups, dealing with CVCM becomes a nightmare. Here lies the power of using Git.
Why Git?
- Git helps organizations share code among teams
- It improves your work and collaborate on projects
- Git helps you keep history through remote hosting services
Commands
To start from scratch, goto the directory where you want to work with your project in the terminal (Mac), and then start using Git commands given below as and when required.
- To initiate Git, the directory must contain
.git
in it. To create it,run commandgit init
- To find the status of your git files… like which files are staged and what are not tracked, run
git status
- Next to keep stage your files, run
git add <filename>
to add a particular file orgit add .
to add all the files in that directory. You can add all the similar kind of files bygit add '.*html'
- Now these added files can be committed by
git commit
, but this command directs you to VIM and give the commit a name in VIM. - For ease in committing, use
-m
ingit commit -m "My commit"
to commit to master. - To find logs run
git log
For remote repos
- To access remote repos, firstly, clone the url from repos and start making changes to those files by running the following:
git clone https://github.com/hasura/git-push-template.git
2. To make changes live, push the code to the repo. -u
in the following command, remembers what to push and where. You need not enter these details again and again
git remote add origin https://github.com/hasura/git-push-templates.gitgit push -u origin master
3. Pull requests let you tell others about changes you’ve pushed to a repository on GitHub. For pull request run git pull origin master
or
git pull origin master --allow-unrelated-histories
4. To see changes in commits, i.e. from previous commit to the latest, run git diff HEAD
.
Branching
If you make some changes and commit again, the next commit stores a pointer to the commit that came immediately before it. A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master.
- Branching can be done by running
git branch MyBranch
git checkout MyBranch
Merging
Merging is used to merge the branches so that all the changes you made comes to live at once.
git merge -a -m 'changes made'
-a
in the above is used to skip the staging step. However, any new files that are untracked must be manually staged using git add <filename>
Merge master into new branch
git merge master
This may arise an error saying CONFLICT
. This can be resolved manually by going to the file and editing it. Fix this by deleting
<<<< HEAD
.....
.....
>>>> master
This can be fixed using tools like ‘tortisemerge’ tool or ‘winmerge’ tools. Firstly install these tools and then try running git mergetool
Learn more on https://try.github.io/ and http://gitreal.codeschool.com/
In Short:
cd repository
git branch new-branch
git checkout new-branch
git checkout master
git add -A
#push to new branch
git push --set-upstream origin new-branch
git remote -v
git remote add upstream https://github.com/TejasReddy9/sec_bichromaticds.git
git remote -v
git fetch upstream
git checkout master
git merge upstream/master