Introduction
Integrated Solutions
- Custom Development
This is an old revision of the document!
Use this to get code from the server.
Simplest case: clone a repository (no submodules, master branch)
git clone https://github.com/DephyInc/mySimpleRepo.git
Clone a repository that has submodules (master branch)
git clone --recursive https://github.com/DephyInc/myTypicalRepo.git
Clone a repository that has submodules, specific branch
git clone -b mySpecificBranch --recursive https://github.com/DephyInc/myTypicalRepo.git
For more examples, including real repositories: Cloning a repository and its submodule(s).
Use this to change/create branches
Change branch (when that branch already exists)
git checkout thatOtherBranch
Create a new branch and start using it (locally)
git checkout -b myNewBranch
Important: name your branches according to Dephy's git/Jira convention!
To send that new branch info to the server follow the previous command with:
git push --set-upstream origin myNewBranch
Tip: if you try git push
without doing this first it will give you the exact command you need to use.
Use this to get a local copy of what's on the server
Want to get your colleague's latest work?
git pull
Use this to upload your local copy to the server.
After you committed code, use this to send it up
git push
Note 1: always pull before pushing
Use add
to stage (git's term for “prepare for a commit”) a file, and commit
to commit all your staged files. Add can be called explicitly, or as part of commit.
Stage one specific file
git add thisOneFile.c
Stage all the modified files
git add -u
Commit your work after it was manually staged:
git commit -m "This is your commit message"
Stage all your modified files (equivalent to git add -u
), then commit them with a message:
git commit -am "This is your commit message"
Use this to visualize and search local branches and commits.
This is how to open the built in visualization tool
gitk
Use this to merge two branches together. In this example we are on branch myBranch and we want to merge it with branch theirBranch.
git merge theirBranch
Tip: depending on the tree structure between the branch you may have to call git merge origin/theirBranch
for it to be recognized.
If you are playing in Easy mode, the two branches will beatly merge and you won't have anything to do. In Real Life mode conflicts are frequent. git will list the files that it wasn't able to merge automatically. You need to open them one after the other and look for the «««
and »»»>
symbols, and manually fix what's wrong.
Typical reasons why it won't automatically merge:
Once you are done fixing the conflicts (aka file doesn't have «<
and »>
anymore, and code makes sense) you need to tell git.
git add happyConflictFreeFile.c
Is that merge more complicated than what you can handle? There is a way to get out of it.
git merge --abort
Use this to discard (technically, save for later…) local changes.
Often you'll try something, then realize you can simply pull a new version that already includes those changes.
git stash
Tip: you can stash and pop multiple levels. 99% of the time I only stash as a way of “unlocking” my copy so I can pull new stuff.
Use this to get a list of the commits done in that branch
This will give you hash codes, commit messages, dates, etc.
git log
New git users can sometimes be surprised by a merge even though they did not type the git merge
command. What triggers a merge? If two developers are working on the same branch git needs to unify the two versions of the source file. This can happen when you pull from the server.
You are developer A and you work with developer B. Monday morning you both pull the latest copy of the dev
branch. Your local copy of source.c is exactly the same as the server's copy. This is what you both have:
#include "source.h" int myVariable = 100;
Later that day you change myVariable to be 200. You do not commit or push this code, it only exists on your local machine. Your colleague B also things that myVariable shouldn't be 100. He changes it to be 250, commits his code and pushed it. Tuesday you remember that you forgot to commit this important change. You do:
git commit -am “Bug fix, variable had wrong value”
git pull
… and boom, you initiated a merge!
The reason is simple. On the server int myVariable = 250;
and on your local machine int myVariable = 200;
. Git doesn't know what's right, you have to decide.
»>
symbols indicating a conflict. git add source.c
git commit -am “Fixed merged conflict, correct value is 200.”
==== Everything else ====
* To see what changed in a file: <code>git diff</code>
* After resolving a conflict,
git add fileName where fileName is the file that was conflicted.<code>git add fileName</code>
* To list all of your branches, both on the server and locally:<code>git branch -a</code>
* To delete a remote branch: <code>git push origin –delete branchName</code>
* To populate a submodule that exists on the server, but that's empty on your computer:<code>git submodule update –init</code>
===== GitHub.com =====
Dephy's GitHub page
==== Network ====
If you click on Insights then on Network you can visualize the repo tree. This is useful if you are using the command line yet want to get a better understanding of the different branches.
==== Specific line of code ====
We often need to refer to a vary specific l;ine or section of the code. On github.com, of you click on the line lumber if will highlight that line, and modify the URL for you. Here's an example where I'm pointing at main(), in main.cpp of the DSEP-64_PyVersion branch of the Actuator-Package repository: https://github.com/DephyInc/Actuator-Package/blob/DSEP-64_PyVersion/C/main.cpp#L46
===== Real life examples - Dephy specific =====
Some/most of that is already covered in Source Code, but I'm including here for your convenience.
==== Cloning a repository and its submodule(s) ====
Latest version of a branch:
<code>git clone –recursive -b branchName https://github.com/DephyInc/RepositoryName.git</code>
Example:
<code>git clone –recursive -b dev https://github.com/DephyInc/fx-rigid-mn.git</code>
Specific commit:
<code>
git clone –recursive repoAddress/repoName.git
cd repoName
git checkout hash
git submodule update –recursive
</code>
Example:
<code>
git clone –recursive https://github.com/DephyInc/fx-rigid-mn.git
cd fx-rigid-mn
git checkout e922c5dd0464679cb43cfef4f56fff87e33e3b75
git submodule update –recursive
</code>
Source Code has detailed examples, including a very detailed description of the process of cloning a repository and its submodule(s)
==== Switching Branch (Existing Branch) ====
In this example you have already cloned a repository to your computer. Let's say you are on the “develop” branch. You now need to run a test on a branch named “newFeatureBranch”.
First, if you have been modifying the sources you have two options:
* If you do not want to keep those changes discard them with the
git stash command.
* If you want to save this work:
- Commit it
git commit -am “This is my commit message”
-
git pull to get the last version from the server. This may trigger a merge, but for now we will assume that git returned
Already up to date.
-
git push
You are now ready to change branch.
-
git pull will fetch the latest information from the server
-
git checkout newFeatureBranch will select that branch
You are now ready to run your test(s).
==== Switching Branch (New Branch) ====
In this example you have already cloned a repository to your computer. Let's say you are on the “develop” branch. You now need to program a new feature. The work will go to a new branch named “newFeatureBranch”.
First, if you have been modifying the sources you have two options:
* If you do not want to keep those changes discard them with the
git stash command.
* If you want to save this work:
- Commit it
git commit -am “This is my commit message”
-
git pull to get the last version from the server. This may trigger a merge, but for now we will assume that git returned
Already up to date.
-
git push
You are now ready to change branch.
-
git pull will fetch the latest information from the server
-
git checkout -b newFeatureBranch will create that new branch. The only difference between creating a new branch and switching to an existing branch is the
-b command.
-
git push –set-upstream origin newFeatureBranch will send the information about this new branch to the server.
You are now ready to do your work. Once you are done it's a simple matter of committing, pulling and pushing.
==== Committing work done on a detached head ====
<code>
git commit -m “Your commit message”
git branch temporaryBranch
git checkout branchYouWantToCommitTo
git merge temporaryBranch
</code>
===== Configuration =====
Download git from https://git-scm.com/download/ and follow the instructions.
After installing git, all you need to do is:
- Set your git username/email to make sure that your commits are associated with your name
- Set your github.com password
-
git config –global core.autocrlf input'' so git use LF as end of line (that makes Bash scripts work equally well on Win and Unix)