Changes

Jump to navigation Jump to search
9,653 bytes added ,  21:21, 5 June 2023
Line 1: Line 1:  
<font size="3">GitHub is a web-based platform that provides hosting of coding repositories. It offers a centralized location for people to store, manage, and allow collaboration on their code. It is especially useful for version control and project tracking. The following information is geared towards linking gluey annex repositories with GitHub, but can easily be used to link your home computer to a GitHub repository. This wiki page is being updated as the author learns how to use GitHub, so if you know a better, more efficient way to accomplish a task then by all means please do it that way (... and add an addendum to this page if you have a login).</font>
 
<font size="3">GitHub is a web-based platform that provides hosting of coding repositories. It offers a centralized location for people to store, manage, and allow collaboration on their code. It is especially useful for version control and project tracking. The following information is geared towards linking gluey annex repositories with GitHub, but can easily be used to link your home computer to a GitHub repository. This wiki page is being updated as the author learns how to use GitHub, so if you know a better, more efficient way to accomplish a task then by all means please do it that way (... and add an addendum to this page if you have a login).</font>
   −
== <u>The Setup</u> ==
+
 
 +
== <i>Setup Option #1 (GitHub &#x2192; local computer)</i> ==
 +
Adding a repository in your GitHub account to your local computer (e.g. cloning).
 
<ol>
 
<ol>
 
<!-- Step 1 -->
 
<!-- Step 1 -->
Line 42: Line 44:  
           <li>You can do the same for your home computer, you'll just need to open a terminal or command prompt and navigate to the directory where you want to store your code</li>
 
           <li>You can do the same for your home computer, you'll just need to open a terminal or command prompt and navigate to the directory where you want to store your code</li>
 
         </ul>
 
         </ul>
       <li><u>Type the command:</u><br> <b>git clone https&#58;&#47;&#47;github&#46;com&#47;your-username&#47;your-repository&#46;git</b><br> <i>make sure to replace "your-username" with your GitHub username and "your-repository" with the name of your repository</i></li>
+
       <li>Use the command:<br>
       <li>You will now need to login. Use your <b>GitHub Username</b>, but make sure to use the <b>Personal Access Token</b> you created earlier and <b><u>NOT</u></b> your GitHub password</li>
+
          <b>git clone https&#58;&#47;&#47;github&#46;com&#47;<i>your-username</i>&#47;<i>your-repository</i>&#46;git</b><br>  
 +
          <i>&ensp; - Make sure to replace "your-username" with your GitHub username and "your-repository" with the name of your repository</i></li>
 +
       <li>You will now need to login. Use your <u>GitHub Username</u>, but make sure to use the <u>Personal Access Token</u> you created earlier and <b><u>NOT</u></b> your GitHub password</li>
 
     </ul><br>
 
     </ul><br>
   Line 49: Line 53:  
   <li>At this point you should have a new folder in your annex with the name of your GitHub repository</li>
 
   <li>At this point you should have a new folder in your annex with the name of your GitHub repository</li>
 
     <ul>
 
     <ul>
       <li>Confirm that the remote repository is correctly added, use the command:<br> <b>git remote -v</b><br></li>
+
       <li>Confirm that the remote repository is correctly added, use the command:<br>  
       <li>If needed, you can link the local repository to the remote repository using:<br> <b>git remote add origin &lt;repository-url&gt;</b><br></li>
+
          <b>git remote -v</b><br></li>
 +
       <li>If needed, you can link the local repository to the remote repository using:<br>  
 +
          <b>git remote add origin <i>repository-url</i></b><br></li>
 
     </ul><br>
 
     </ul><br>
   Line 56: Line 62:  
   <li>To switch to or determine a branch in Git, you can use the git checkout command:
 
   <li>To switch to or determine a branch in Git, you can use the git checkout command:
 
     <ul>
 
     <ul>
       <li>Determine the name of the branch you want to switch to via:<br> <b>git branch</b><br> It will show all branches available and indicate your current branch with a * <br></li>
+
       <li>Determine the name of the branch you want to switch to via:<br>  
       <li>Create a new branch using: <br> <b>git branch new-branch-name</b> then use <b>git checkout new-branch-name</b> to switch to that branch and work in it<br> OR <br> <b>git checkout -b new-branch-name</b> will create and switch to the new branch in one step</li><br>
+
          <b>git branch</b><br>  
 +
          <i>&ensp; - It will show all branches available and indicate your current branch with a * </i><br></li>
 +
       <li>Create a new branch using: <br>  
 +
          <b>git branch <i>new-branch-name</i></b> </li>
 +
      <li>Switch to that branch and work in it via:<br>
 +
          <b>git checkout <i>new-branch-name</i></b> <br></li><br>
 +
      <li>The two steps above can be combined:<br>  
 +
          <b>git checkout -b <i>new-branch-name</i></b> <br>
 +
          &ensp; <i>- This will <u>create and switch</u> to the new branch in one step</i></li><br>
 
     </ul>
 
     </ul>
   Line 64: Line 78:     
<!-- Step 8 -->
 
<!-- Step 8 -->
   <li>When you're ready to save, make sure you are in the root directory of your repository or the directory where the new folder you want to save is located. Then use the git add command to add the new folder and its contents to the staging area. Run the following command:<br> <b>git add folder-name</b><br> OR <br> <b>git add .</b> <br> If you want to add all new files and folders in the current directory</li><br>
+
   <li>When you're ready to save, make sure you are in the root directory of your repository or the directory where the new folder you want to save is located. Then use the git add command to add the new folder and its contents to  
 +
      the staging area. Run the following command:<br>
 +
  <b>git add <i>folder-name</i></b><br>
 +
      <i>&ensp;- Adds the specified file or folder</i><br>
 +
  &emsp; OR <br>
 +
  <b>git add .</b> <br>
 +
      <i>&ensp;- Adds all new files and folders in the current directory recursively, does not add higher directory information</i><br>
 +
  &emsp; OR <br>
 +
  <b>git add -A</b> <br>
 +
      <i>&ensp;- Adds all files: new, modified, and deleted files. It will include files in the current directory and in higher directories that belong to the same git repository</i><br>
 +
  &emsp; OR <br>
 +
  <b>git add -u</b> <br>
 +
      <i>&ensp;- Adds modified and deleted files only. Excludes new files. It will include files in the current directory and in higher directories that belong to the same git repository</i><br><br>
 +
  For a more detailed description of <i>git add</i> go to: https://github.com/git-guides/git-add</li><br>
    
<!-- Step 9 -->
 
<!-- Step 9 -->
   <li>Once the new folder/file is added to the staging area, you can commit the changes using the git commit command:<br> <b>git commit -m "Commit message"</b> <br> Replace <i>Commit message</i> with a descriptive message summarizing the changes you made.</li><br>
+
   <li>Once the new folder/file is added to the staging area, you can commit the changes using the git commit command:<br> <b>git commit -m "<i>Commit message</i>"</b> <br>
 +
      <i>&ensp; - Replace Commit message with a descriptive message summarizing the changes you made. Keep " " around your message</i></li><br>
    
<!-- Step 10 -->
 
<!-- Step 10 -->
   <li>After committing the changes (on your local machine), you can push them to the remote repository on GitHub using the git push command:<br> <b>git push origin branch-name</b><br> Replace <i>branch-name</i> with the name of the branch you want to push to (e.g., main)<br> You will need to login with your Username and Personal Access Token</li><br>
+
   <li>After committing the changes (on your local machine), you can push them to the remote repository on GitHub using the git push command:<br>  
 +
      <b>git push origin <i>branch-name</i></b><br>
 +
      <i>&ensp; - Replace branch-name with the name of the branch you want to push to (e.g., main)</i><br>
 +
      <i>&ensp; - You will need to login with your Username and Personal Access Token</i></li><br>
 
</ol>
 
</ol>
 +
 +
== <i>Setup Option #2 (Local computer &#x2192; GitHub)</i> ==
 +
Adding a repository from your local computer to GitHub
 +
<ol>
 +
<!-- Step 1 -->
 +
      <li>On your local computer, initialize the folder you want to adds as a repository:<br>
 +
      &ensp; - Use <b>cd</b> to navigate to the folder that contains the work you'd like to track/backup<br>
 +
      &ensp; - Type <b>git init</b> to initialize the folder for Git tracking<br>
 +
      &ensp; - (<i>optional step</i>) Type <b>touch .gitignore</b> to create a blank file in that folder, which can later be modify to specify files or types of files you don't want to track </li><br>
 +
 +
<!-- Step 2 -->
 +
      <li>Go to https://github.com/ , login, and create a repository as described above. Name the repository the same as your working folder you did the git init command in</li><br>
 +
 +
<!-- Step 3 -->
 +
      <li>On your local computer, link your working folder to the GitHub repsoitory:<br>
 +
      &ensp; - Type <b>git remote add origin <i>github-repository-url</i></b><br>
 +
      &ensp; &ensp; <i>github-repository-url</i> can be found online: <br>
 +
      &ensp; &ensp; &ensp; &ensp; - In your repository folder click on the green [<> Code] button then under the Local tab you'll see Clone HTTPS <br>
 +
      &ensp; &ensp; &ensp; &ensp; - Click the copy button to paste the address into the line of code above</li><br>
 +
 +
<!-- Step 4 -->
 +
      <li>On your local computer, merge the two repositories (local and online):<br>
 +
      &ensp; - Type <b>git pull origin main</b><br>
 +
      &ensp; &ensp; This assumes the GitHub branch is named <i>main</i> in the repository you created online </li><br>
 +
 +
<!-- Step 5 -->
 +
      <li>On your local computer, add files to track:<br>
 +
      <b>git add <i>folder-name</i></b><br>
 +
      &ensp; <i> - Adds the specified file or folder</i><br>
 +
      &emsp; OR <br>
 +
      <b>git add .</b> <br>
 +
      &ensp; <i> - Adds all new files and folders in the current directory recursively, does not add higher directory information</i><br>
 +
      &emsp; OR <br>
 +
      <b>git add -A</b> <br>
 +
      &ensp; <i> - Adds all files: new, modified, and deleted files. It will include files in the current directory and in higher directories that belong to the same git repository</i><br>
 +
      &emsp; OR <br>
 +
      <b>git add -u</b> <br>
 +
      &ensp; <i> - Adds modified and deleted files only. Excludes new files. It will include files in the current directory and in higher directories that belong to the same git repository</i><br><br>
 +
      For a more detailed description of <i>git add</i> go to: https://github.com/git-guides/git-add</li><br>
 +
 +
<!-- Step 6 -->
 +
      <li>On your local computer, commit that add:<br>
 +
      &ensp; - Type <b>git commit -m "Message here"</b><br>
 +
      &ensp; &ensp; Change <i>Message here</i> to a brief description of the reason for the commit (ex: Initial commit) <br>
 +
      &ensp; &ensp; Make sure to keep "" </li><br>
 +
 +
<!-- Step 7 -->
 +
      <li>On your local computer, change (if required) the branch name to the name listed in the online repository:<br>
 +
      &ensp; - Typing <b>git branch</b> tells you the name(s) of the branch(es) with the current one having a * next to it<br>
 +
      &ensp; - If you need to modify your local computer's branch name use <b>git branch -M main</b> where <i>main</i> is the name to change the branch to<br>
 +
      &ensp; - Use <b>git branch</b> to verify the change </li><br>
 +
 +
<!-- Step 8 -->
 +
      <li>On your local computer, push your commit:<br>
 +
      &ensp; - Type <b>git push -u origin main</b><br>
 +
      &ensp; &ensp; You'll need to enter your GitHub profile name and your Personal Access Token to complete the push<br><br>
 +
      &ensp; - The purpose of the <i>-u</i> flag is to establish an upstream relationship between the local branch and the specified remote branch<br>
 +
      &nbsp; &nbsp; This is typically done on the initial push to a remote repository. After that, you don't need to use the -u flag again for subsequent pushes to the same branch </li><br>
 +
 +
<!-- Step 9 -->
 +
      <li>At this point your online GitHub repository should be the same as the working folder on your local computer <br>
 +
      &emsp; &nbsp; You can now continue to code and push/pull to GitHub as needed to track/update changes. Good luck, if it doesn't work don't blame me ... I only work here :) </li><br>
 +
</ol>
 +
 +
== <i>Ignoring Files (.gitignore)</i> ==
 +
There are various reasons to not want to track/push files. The following situation helps when you're having trouble pushing files due to them being too big (>50 MB, GitHub max) .
 +
<ol>
 +
  <li><u>Find those problem files</u>: <br>
 +
      &ensp; - Ensure you're in the directory of interest (use <b>cd </b> command as needed) <br>
 +
      &ensp; - Type <b>find . -type f -size +50M </b><br>
 +
      &ensp; - This will list files that are above GitHub's 50 MB limit </li><br>
 +
 +
  <li><u>Create a .gitignore file</u>: <br>
 +
      &ensp; - To create one use the command <b>touch .gitnore </b><br>
 +
      &ensp; &ensp; Make sure you are in the root folder of your repository on your local computer when you use this command <br>
 +
      &ensp; &ensp; <u>Hint</u> - The root folder is named the same as your repository on GitHub </li><br>
 +
 +
  <li><u>Use your favorite text editor to open the file (i.e. Vim, Notepad++, etc.)</u>: <br>
 +
      &ensp; - Enter the oversized files from above into the girignore file<br>
 +
      &ensp; &ensp; Make sure to remove the <b>.</b> if you decide to copy/paste <br>
 +
      <i><u>Example</u>:</i></li>
 +
 +
<!-- Example -->
 +
<pre># Remember: A line that follows # is  a comment.
 +
#
 +
# To ignore a file update this file (.gitignore) then add, commit, and push this file.
 +
# Do this BEFORE any other pushes so the rule will be in effect.
 +
 +
# These files are above GitHub's recommended file size (50 MB). SUBFOLDER/FILE
 +
MonteCarlo/fermi.txt
 +
Examples/mctuple.root
 +
Examples/mctuple.hbook
 +
Examples/ntuple.root
 +
Examples/log
 +
Examples/omega2p_sim_1.itape
 +
Examples/ntuple.hbook
 +
 +
# These are object files. Code that is compiled to machine code, no need fo me to saved/tracked in GitHub.
 +
# * is a wild card, so anything that has .o at the end is ignored using *.o, while *.o.* says ignore anything that has .o. within the name.
 +
*.o
 +
*.o.*
 +
</pre><br>
 +
 +
  <li><u>Ignore already checked in files</u>: <br>
 +
      &ensp; - Since you already tried and failed to fully push those file, you'll need to untrack them <br>
 +
      &ensp; - From your terminal type <b>git rm --cached FILENAME </b><br>
 +
      &ensp; &ensp; Hint - You can copy/paste from the find results <b>git rm --cached /SUBFOLDER/FILENAME </b><br>
 +
      &ensp; - Do this for all files that require it before proceeding </li><br>
 +
 +
  <li><u>Add, commit, and push the .gitignore file</u>: <br>
 +
      &ensp; - Add the gitignore file using <b>git add .gitignore </b> <br>
 +
      &ensp; - Commit the file using <b>git commit -m "Files to ignore" </b> <br>
 +
      &ensp; - Push the commit using <b>git push origin main </b> <br>
 +
      &ensp; &ensp; Now subsequent pushes will know to ignore those files or types of files </li> <br>
 +
 +
== <u><i>Extras</i></u> ==
 +
<ul>
 +
<!-- 0 -->
 +
  <li>For more information on common git commands, check out the links in the header at the top of the Git Guides webpage (https://github.com/git-guides/git-add)<br>
 +
      &ensp; - This site is a good starting point for common questions</li><br>
 +
 +
<!-- 1 -->
 +
  <li><b>git status</b><br>
 +
      &ensp; - This command shows the branch you're on, what files are in the working or staging directory, and other important information<br>
 +
      &emsp; <u>Example</u>:<br>
 +
      <pre>        mcintyre@gluey/nfs/direct/annex/mcintyre/GitRepo/Radphi_pi02gp/RUN0
 +
        $ git status
 +
        # On branch PI0
 +
        # Untracked files:
 +
        #  (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)
 +
        #
 +
        # STEP6/
 +
        # STEP7/
 +
        nothing added to commit but untracked files present (use &quot;git add&quot; to track)
 +
      </pre>
 +
  </li><br>
 +
 +
<!-- 2 -->
 +
  <li><b>git reset</b><br>
 +
      &ensp; - This command unstages all changes (e.g. all <i>git add</i> performed before a <i>git commit</i>)<br></li>
 +
  <b>git reset <i>file-name</i></b><br>
 +
      &ensp; - This command undoes <i>git add</i> for uncommited changes to file <i>file-name</i><br><br>
 +
 +
<!-- 3 -->
 +
 +
 +
</li>
 +
 +
 +
 +
 +
<!-- For spacing:      &nbsp; OR <br>    &ensp; OR <br>    &emsp; OR <br> -->
 +
<!--                      Smaller indent        to          Larger indent  -->
581

edits

Navigation menu