FlexSEA Wiki

A WEARABLE ROBOTICS TOOLKIT

User Tools

Site Tools


git

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
git [2018/04/02 19:55]
jfduval
git [2020/07/28 21:05] (current)
bbaig [Real life examples - Dephy specific]
Line 1: Line 1:
-====== Git Tips & Commands ​======+====== ​Using Git at Dephy ======
  
-===== General ​=====+===== Getting started ​=====
   * Documentation:​   * Documentation:​
     * git manual: https://​git-scm.com/​doc     * git manual: https://​git-scm.com/​doc
     * Tutorial by github: https://​guides.github.com/​activities/​hello-world/​     * Tutorial by github: https://​guides.github.com/​activities/​hello-world/​
     * Got 15 minutes and want to learn Git? Interactive tutorial: https://​try.github.io/​levels/​1/​challenges/​1     * Got 15 minutes and want to learn Git? Interactive tutorial: https://​try.github.io/​levels/​1/​challenges/​1
-  * Essential commands+  * Relevant Wiki pages: 
-    * To get code from server: git clone +    * [[sourcecode|Source Code]] has graphical examples and short stories to help you understand the flow of working with repos and submodules. 
-    * To update ​your code to the latest ​version: ​git pull +    * [[internal:​git|Git Workflow Best Practices]] (Dephy only, no public access) 
-    To stage the files you changes ​(ie to include them in the next commit): git add +    * [[internal:​jira|Using Jira at Dephy]] (Dephy only, no public access) 
-    * To commit your changes: git commit +    * Before pushing code, make sure it follows the [[styleguide|Dephy C/C++ Programming Style Guide]] 
-    * To change branch: ​git checkout +  * Tools: 
-    * To push your commits to the server: git push +    * When you install git you'll get git bash. All the examples below use it. Make sure to read the [[#​Configuration]] section. 
-    To see what changes in a file: git diff +    * If you prefer a graphical user interface you can request a GitKraken license. 
-  * git commit: +    * To compare branches: [[https://​winmerge.org/​|WinMerge]] 
-    -aAutomatically stages files that have been modified and deletedbut new files you have not told Git about are not affected+  * [[#​Essential commands|Essential commands]] 
-    * -mAdds a commit ​message +  * [[#​Additional commands & tricks]] 
-    * -amAll of the aboveEx.: <​code>​git commit -am "Added new files."</​code>​ +  * [[#​GitHub.com]] tips and tricks 
-  * git add: using -u will add all of the tracked files to the staging area +  * [[#Real life examples - Dephy specific]] 
-  * After resolving ​a conflict, ​git add fileName where fileName is the file that was conflicted+    * Cloning a repository and its submodule(s) 
-  * From the command line you can add a submodule ​to a project by doing: <​code>​git ​submodule add -b branchName repoURL</​code>​ +    * Switching Branch (Existing Branch) 
-  * To create ​new branch:<​code>​git ​checkout -b yourNewBranch</​code>​+    * Switching Branch (New Branch) 
 +    * Committing work done on a detached head 
 +  * [[#​Configuration]] 
 + 
 +===== Essential commands ===== 
 + 
 +  * [[#git clone|clone]] 
 +  * [[#git checkout|checkout]] 
 +  * [[#git pull|pull]] 
 +  * [[#git push|push]] 
 +  * [[#git add and git commit|add and commit]] 
 +  * [[#gitk]] 
 + 
 +==== git clone ==== 
 +//Use this to get code from the server.// 
 + 
 +Simplest caseclone a repository (no submodules, master branch) 
 +<​code>​git clone https://​github.com/​DephyInc/​mySimpleRepo.git</​code>​ 
 + 
 +Clone a repository that has submodules (master branch) 
 +<​code>​git clone --recursive https://​github.com/​DephyInc/​myTypicalRepo.git</​code>​ 
 + 
 +Clone a repository that has submodules, specific branch 
 +<​code>​git clone -b mySpecificBranch --recursive https://​github.com/​DephyInc/​myTypicalRepo.git</​code>​ 
 + 
 +For more examples, including real repositories:​ [[git#​Cloning a repository and its submodule(s)|Cloning a repository and its submodule(s)]]. 
 + 
 +==== git checkout ==== 
 +//Use this to change/​create branches//​ 
 + 
 +Change branch (when that branch already exists) 
 +<​code>​git checkout thatOtherBranch</​code>​ 
 + 
 +Create a new branch and start using it (locally) 
 +<​code>​git checkout -b myNewBranch</​code>​ 
 +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: 
 +<code>git push --set-upstream origin myNewBranch</​code>​ 
 +Tip: if you try ''​git push''​ without doing this first it will give you the exact command you need to use. 
 + 
 +==== git pull ==== 
 +//Use this to get a local copy of what's on the server// 
 + 
 +Want to get your colleague'​s ​latest ​work? 
 +<​code>​git pull</​code>​ 
 +  Note 1: always pull before pushing 
 +  * Note 2: if you made changes ​and the server copy is different a merge will be triggered 
 + 
 +==== git push ==== 
 +//Use this to upload your local copy to the server.// 
 + 
 +After you committed code, use this to send it up 
 +<​code>​git push</​code>​ 
 +Note 1always pull before pushing 
 + 
 +==== git add and git commit ==== 
 +//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 
 +<​code>​git add thisOneFile.c</​code>​ 
 + 
 +Stage all the modified files 
 +<​code>​git add -u</​code>​ 
 + 
 +Commit your work after it was manually staged: 
 +<​code>​git commit ​-m "This is your commit message"</​code>​ 
 + 
 +Stage all your modified files (equivalent to ''​git add -u''​),​ then commit them with a message: 
 +<​code>​git commit -am "This is your commit message"</​code>​ 
 + 
 +==== gitk==== 
 +//Use this to visualize and search local branches and commits.// 
 + 
 +This is how to open the built in visualization tool 
 +<​code>​gitk</​code>​ 
 +{{ :gitkexample.jpg?​400 |}} 
 + 
 + 
 +===== Additional commands & tricks ===== 
 + 
 +  * [[#git merge]] 
 +  [[#git stash]] 
 +  * [[#git log]] 
 +  [[#​Everything else]] 
 + 
 +==== git merge ==== 
 + 
 +//Use this to merge two branches together. In this example we are on branch myBranch and we want to merge it with branch theirBranch.//​ 
 +<​code>​git merge theirBranch</​code>​ 
 +Tipdepending 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 modethe two branches will easily 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: 
 +  ​Tabs (good) vs spaces (bad) 
 +  * Two people modified the same function 
 +  * Variable or function name changes 
 + 
 +Once you are done fixing the conflicts (aka file doesn'​t have ''<<<''​ and ''>>>''​ anymore, and code makes sense) you need to tell git. 
 +<​code>​git add happyConflictFreeFile.c</​code>​ 
 + 
 +Is that merge more complicated than what you can handle? There is a way to get out of it. 
 +<​code>​git merge --abort</​code>​ 
 + 
 +==== git stash ==== 
 +//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. 
 +<​code>​git stash</​code>​ 
 +Tipyou can stash and pop multiple levels. 99% of the time I only stash as way of "​unlocking"​ my copy so I can pull new stuff. 
 + 
 +==== git log ==== 
 +//Use this to get a list of the commits done in that branch// 
 + 
 +This will give you hash codes, ​commit ​messages, dates, etc. 
 +<​code>​git log</​code>​ 
 + 
 +{{ ::​gitlog.png?​600 |}} 
 + 
 +==== Understanding merging ==== 
 + 
 +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 fileThis 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: 
 +<​code>​ 
 +#​include ​"source.h" 
 +int myVariable = 100; 
 +</​code>​ 
 + 
 +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 thinks that myVariable shouldn'​t be 100. He changes it to 250, commits his code and pushes 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. 
 +  * Open the file in your favorite editor 
 +  * Look for the ''>>>''​ symbols indicating ​conflict.  
 +  * Fix the conflict, ​remove ​the indicators
 +  * Call ''​git ​add source.c''​ to let git know that you have fixed the conflict 
 +  * ''​git commit -am "Fixed merged conflict, correct value is 200."''​ 
 + 
 +The easiest way to prevent ​merge conflict is to push and and pull frequently. Merging ​few hours of work is usually far easier than merging days worth of work. 
 + 
 +==== Everything else ==== 
 + 
 +  * To see what changed in a file: <​code>​git ​diff</​code>​ 
 +  * After resolving ​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 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 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>​   * To populate a submodule that exists on the server, but that's empty on your computer:<​code>​git submodule update --init</​code>​
  
-===== Cloning a repository and its submodule(s) ​=====+===== GitHub.com ===== 
 + 
 +[[https://​github.com/​DephyInc|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. 
 + 
 +{{ :​gitnetwork.png?​600 |}} 
 + 
 +==== Specific line of code ==== 
 + 
 +We often need to refer to a vary specific line or section of the code. On github.com, if you click on the line number, it 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 
 + 
 +{{ :​gitline.png?​600 |}} 
 + 
 +===== Real life examples - Dephy specific ===== 
 + 
 +Some/most of this is already covered in [[sourcecode|Source Code]], but I'm including here for your convenience. 
 + 
 +==== Cloning a repository and its submodule(s) ====
 **Latest version of a branch:** **Latest version of a branch:**
 <​code>​git clone --recursive -b branchName https://​github.com/​DephyInc/​RepositoryName.git</​code>​ <​code>​git clone --recursive -b branchName https://​github.com/​DephyInc/​RepositoryName.git</​code>​
Line 47: Line 214:
 </​code>​ </​code>​
  
-[[sourcecode|Source Code]] has detailed examples.+[[sourcecode|Source Code]] has detailed examples, including a very detailed description of the process of [[sourcecode#​Cloning a repository and its submodule(s) - latest version of a branch|cloning a repository and its submodule(s)]]
  
-===== Committing work done on a detached head =====+==== 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>​ <​code>​
Line 58: Line 260:
 </​code>​ </​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)
git.1522698921.txt.gz · Last modified: 2018/04/02 19:55 by jfduval