Jump to content

Typical Git workflow: Difference between revisions

no edit summary
mNo edit summary
No edit summary
Line 1: Line 1:
== Introduction  ==
== Introduction  ==


Normal workflow is to develop and check in on a branch, then once everything is happy, merge the branch back into the master. 
Normal workflow is to develop and check in on a branch, then once everything is happy, merge the branch back into the master.


Local repository consists of three '''"trees"''' maintained by git. the first one is your '''Working Directory''' which holds the actual files.The second one is the '''Index''' which acts as a staging area and finally the '''HEAD''' which points to the last commit you've made.  
Local repository consists of three '''"trees"''' maintained by git. the first one is your '''Working Directory''' which holds the actual files.The second one is the '''Index''' which acts as a staging area and finally the '''HEAD''' which points to the last commit you've made. You typically only see the working directory, but as you issue git commands, it is helpful to understand the information used by git.


== Workflow Diagram  ==
== Workflow Diagram  ==
Line 9: Line 9:
[[Image:Git-workflow.png]]
[[Image:Git-workflow.png]]


== References<br> ==
== References  ==


<span style="line-height: 1.5em;">Basic Git Commands&nbsp;:&nbsp;[https://confluence.atlassian.com/display/STASH/Basic+Git+commands https://confluence.atlassian.com/display/STASH/Basic+Git+commands]</span>
* Basic Git Commands : [https://confluence.atlassian.com/display/STASH/Basic+Git+commands https://confluence.atlassian.com/display/STASH/Basic+Git+commands]
 
* Git- The Simple Guide: [http://rogerdudler.github.io/git-guide/ http://rogerdudler.github.io/git-guide]  
Git- The Simple Guide: [http://rogerdudler.github.io/git-guide/ http://rogerdudler.github.io/git-guide/]  
* Basic Git Commands : [http://robert-reiz.com/2011/10/01/git-add-commit-push-pull/ http://robert-reiz.com/2011/10/01/git-add-commit-push-pull]
 
Basic Git Commands&nbsp;:&nbsp;[http://robert-reiz.com/2011/10/01/git-add-commit-push-pull/ http://robert-reiz.com/2011/10/01/git-add-commit-push-pull/]


== Typical Workflow are as follows  ==
== Typical Workflow are as follows  ==


1) get local copy of code<br>2) create a branch<br>3) edit files<br>4) add/commit changes to local machine<br>5) get back in sync with changes commited by others<br>6) push branch to remote git repository<br>7) merge local branch into local master<br>8) push local master to remote git repostiory  
# get local copy of code
# create a branch
# edit files
# add andcommit changes to local machine
# get back in sync with changes commited by others
# push branch to remote git repository
# merge local branch into local master
# push local master to remote git repostiory  


<br>
The cut-and-paste commands assume you have set '''REPO''' and '''BRANCH''' shell variables.  An example setting would be:


'''For the TATAPowersed SDK we can set REPO and BRANCH as below:'''
<pre>
<pre>REPO=git@github.com:RidgeRun/tata-sdk-dm36x
REPO=git@github.com:RidgeRun/eval-sdk-dm36x
BRANCH=fixed-ip-address</pre>
BRANCH=fixed-ip-address
</pre>


== 1) Get&nbsp;a local copy of the code  ==
== Get a local copy of the code  ==
<pre>git clone $REPO.git tatasdk


OR
First create a working directory (what RidgeRun calls a DEVDIR when using a RidgeRun SDK)


git clone https://github.com/RidgeRun/tata-sdk-dm36x.git tatasdk
<pre>
git clone $REPO.git sdk
</pre>


</pre>
== Create a Branch  ==
Creating the Working copy of the local repository by running the command:
<pre>git clone path/to/repository</pre>


== 2) Create a Branch  ==
Branches are used to develop features in isolation from each other. The master branch is the "default" branch when you create a repository.
Use other branches for development and merge them back to the master branch upon completion.


Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. <br>Use other branches for development and merge them back to the master branch upon completion.
Create a new branch and switch to that branch:


Create a new branch named BRANCH=fixed-ip-address and switch to it using below command:
<pre>
<pre>git checkout -b $BRANCH
git checkout -b $BRANCH
</pre>  
</pre>  
Switch back to master by running the below command:  
Switch back to master by running the below command:  
<pre>git checkout master
 
<pre>
git checkout master
</pre>  
</pre>  
And delete the branch again:  
 
<pre>git branch -d $BRANCH
A branch is not available to the others unless you push the branch to your remote repository
 
<pre>
git push origin $BRANCH
</pre>
 
== Edit Files ==
 
Use your favorite editor to modify existing source files and create new source files.
 
== Add and commit changes to a local machine  ==
 
git creates a commit (very much like a patch file) based on the files you want added to commit.
 
First you tell git which files you want part of the commit.  Any changes you have made to those files will be included in the commit.
 
<pre>
git add $FILE1 $FILE2
</pre>
 
You can see what files will be part of the next commit, which other files you have changed, and what files have been added that are not part of the git index:
 
<pre>
git status
</pre>
 
Now you can create the actual commit:
<pre>
git commit -m "$COMMIT_MESSAGE"
</pre>  
</pre>  
A Branch is not available to the others unless you push the branch to your remote repository  
 
<pre>git push origin $BRANCH
Now the file is committed to the HEAD of your local repository, but the changes are not part of the repository you cloned.  However, git will not let you push your commits to the remote repository unless changes commited by others to the remote repository are part of your local repository.
Another helpful command is to see all the commits on your currently checked out branch
 
<pre>
gitk &
</pre>
</pre>


== 3) Edit Files<br> ==
You may have to run:


== 4) Add / Commit changes to a Local Machine  ==
<pre>
sudo apt-get gitk
</pre>


You can propose changes (add it to the Index) using <br>
to install the gitk tool.
<pre>git add &lt;filename&gt;
</pre><pre>git add * </pre>
This is the first step in the basic git workflow. To actually commit these changes use:<br>
<pre>git commit -m "Commit message"
</pre>
Now the file is committed to the HEAD, but not in your remote repository yet.<br>


'''Pushing Changes:'''<br>
== Get back in sync with changes commited by others ==


Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute
You first need to get your local master in sync with the remote repository, then merge those changes into your branch.
<pre>git push origin master
</pre>
Change master to whatever branch you want to push your changes to.<br>


== 5) Get back in sync with changes commited by others<br> ==
<pre>
<pre>git pull
git checkout master
git pull
git checkout $BRANCH
git merge master
</pre>
</pre>


== 6) Push branch to remote git repository<br>  ==
If someone changed a file and pushed that change to the remote repository and you changed the file too in your local repository, then you might need to resolve which changes have priority.  There is lots of information on the Internet on how to do this sometimes complex task.
<pre>git push origin $BRANCH
 
== Push branch to remote git repository ==
 
You can share your branch with other by pushing it to the remote repository
 
<pre>
git push origin $BRANCH
</pre>
</pre>


== 7) Merge local branch into local master  ==
== Merge local branch into local master  ==
 
To merge your branch into your local master:
 
<pre>
git checkout master
git merge $BRANCH
</pre>


To update your local repository to the newest commit, execute<br>
git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with
<pre>git pull
<pre>
git add $FILE1 $FILE2
</pre>  
</pre>  
This will in fetch and merge remote changes in your working directory.<br>


To merge another branch into your active branch (e.g. master), use
<pre>git merge $BRANCH</pre>
in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with<br>
<pre>git add &lt;filename&gt; </pre>
before merging changes, you can also preview them by using  
before merging changes, you can also preview them by using  
<pre>git diff &lt;source_branch&gt; &lt;target_branch&gt;
 
<pre>
git diff $BRANCH master
</pre>
</pre>


== 8) Push local master to remote git repostiory<br> ==
== Push local master to remote git repostiory  ==
 
To send the changes (from the HEAD of your local master branch) to your remote repository:


To send the changes (from the HEAD of your local working copy) to your remote repository, execute
<pre>
<pre>git push origin master
git push origin master
</pre>  
</pre>  
Change master to whatever branch you want to push your changes to.
Change master to whatever branch you want to push your changes to.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.