GitHub

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).

Setup Option #1 (GitHub &#x2192; local computer)
Adding a repository in your GitHub account to your local computer (e.g. cloning). 

Sign up for a GitHub account by going to the GitHub website and create a new account - If you do not make your email address public you will have problems pushing your code later

Once you're logged in, click on the "+" sign at the top-right corner of the GitHub homepage and select "New repository" to create a new repository. Give your repository a name and provide a description.  The repository name will be your folder name on gluey (see below) Repository description is optional Repository Setting:  Private vs. Public License (optional) - MIT License is well known and least restrictive</li> You can add a README file</li> You can also rename the main branch (this must be done before the repository is created, changing the name will change all subsequent repositories that are made in the future)</li> And more ...</li> </ul> </ul>

Once the repository is setup online, add a Personal Access Token</li> <ul> At the top-right of the GitHub website click on the pull-down and select "Settings"</li> On the left select "Developer settings" located at the bottom </li> Then on the left select "Personal access tokens"</li> Select "Fine-grained tokens" or "Tokens (classic)", I did the prior one</li> <ul> For the Fine-grained token you will need to select which permissions you'd like to allow (not sure about the classic token)</li> Select a period for the token to be valid, upto 90 days maximum (I believe)</li> Once complete it will provide you with the token you can copy, this will be used in place of your password later</li> </ul> </ul>

Clone your repository</li> <ul> Log in to gluey using a VNC and navigate to the location in your annex where you'll be working</li> <ul> 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> Use the command: git clone https&#58;&#47;&#47;github&#46;com&#47;your-username&#47;your-repository&#46;git &ensp; - Make sure to replace "your-username" with your GitHub username and "your-repository" with the name of your repository</li> You will now need to login. Use your GitHub Username, but make sure to use the Personal Access Token you created earlier and  NOT  your GitHub password</li> </ul>

<li>At this point you should have a new folder in your annex with the name of your GitHub repository</li> <ul> <li>Confirm that the remote repository is correctly added, use the command: git remote -v </li> <li>If needed, you can link the local repository to the remote repository using: git remote add origin repository-url </li> </ul>

<li>To switch to or determine a branch in Git, you can use the git checkout command: <ul> <li>Determine the name of the branch you want to switch to via: git branch &ensp; - It will show all branches available and indicate your current branch with a *  </li> <li>Create a new branch using: git branch new-branch-name </li> <li>Switch to that branch and work in it via: git checkout new-branch-name </li> <li>The two steps above can be combined: git checkout -b new-branch-name &ensp; - This will create and switch to the new branch in one step</li> </ul>

<li>Add files/folders to your directory</li>

<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: git add folder-name &ensp;- Adds the specified file or folder &emsp; OR   git add . &ensp;- Adds all new files and folders in the current directory recursively, does not add higher directory information &emsp; OR   git add -A ''&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'' &emsp; OR   git add -u ''&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'' For a more detailed description of git add go to: https://github.com/git-guides/git-add</li>

<li>Once the new folder/file is added to the staging area, you can commit the changes using the git commit command: git commit -m "Commit message" ''&ensp; - Replace Commit message with a descriptive message summarizing the changes you made. Keep " " around your message''</li>

<li>After committing the changes (on your local machine), you can push them to the remote repository on GitHub using the git push command: git push origin branch-name &ensp; - Replace branch-name with the name of the branch you want to push to (e.g., main) &ensp; - You will need to login with your Username and Personal Access Token</li> </ol>

Setup Option #2 (Local computer &#x2192; GitHub)
Adding a repository from your local computer to GitHub <ol>

<li>On your local computer, initialize the folder you want to adds as a repository: &ensp; - Use cd to navigate to the folder that contains the work you'd like to track/backup &ensp; - Type git init to initialize the folder for Git tracking &ensp; - (optional step) Type touch .gitignore 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>

<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>

<li>On your local computer, link your working folder to the GitHub repsoitory: &ensp; - Type git remote add origin github-repository-url &ensp; &ensp; github-repository-url can be found online: &ensp; &ensp; &ensp; &ensp; - In your repository folder click on the green [<> Code] button then under the Local tab you'll see Clone HTTPS &ensp; &ensp; &ensp; &ensp; - Click the copy button to paste the address into the line of code above</li>

<li>On your local computer, merge the two repositories (local and online): &ensp; - Type git pull origin main &ensp; &ensp; This assumes the GitHub branch is named main in the repository you created online </li>

<li>On your local computer, add files to track: git add folder-name &ensp;  - Adds the specified file or folder &emsp; OR      git add . &ensp;  - Adds all new files and folders in the current directory recursively, does not add higher directory information &emsp; OR      git add -A &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'' &emsp; OR      git add -u &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'' For a more detailed description of git add go to: https://github.com/git-guides/git-add</li>

<li>On your local computer, commit that add: &ensp; - Type git commit -m "Message here" &ensp; &ensp; Change Message here to a brief description of the reason for the commit (ex: Initial commit) &ensp; &ensp; Make sure to keep "" </li>

<li>On your local computer, change (if required) the branch name to the name listed in the online repository: &ensp; - Typing git branch tells you the name(s) of the branch(es) with the current one having a * next to it      &ensp; - If you need to modify your local computer's branch name use git branch -M main where main is the name to change the branch to       &ensp; - Use git branch to verify the change </li>

<li>On your local computer, push your commit: &ensp; - Type git push -u origin main &ensp; &ensp; You'll need to enter your GitHub profile name and your Personal Access Token to complete the push &ensp; - The purpose of the -u flag is to establish an upstream relationship between the local branch and the specified remote branch 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>

<li>At this point your online GitHub repository should be the same as the working folder on your local computer &emsp;  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> </ol>

Ignoring Files (.gitignore)
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> Find those problem files : &ensp; - Ensure you're in the directory of interest (use cd  command as needed) &ensp; - Type '''find. -type f -size +50M ''' &ensp; - This will list files that are above GitHub's 50 MB limit </li>

<li> Create a .gitignore file : &ensp; - To create one use the command touch .gitnore  &ensp; &ensp; Make sure you are in the root folder of your repository on your local computer when you use this command &ensp; &ensp; Hint - The root folder is named the same as your repository on GitHub </li>

<li> Use your favorite text editor to open the file (i.e. Vim, Notepad++, etc.) : &ensp; - Enter the oversized files from above into the girignore file &ensp; &ensp; Make sure to remove the . if you decide to copy/paste  Example :</li>


 * 1) Remember: A line that follows # is  a comment.
 * 2) To ignore a file update this file (.gitignore) then add, commit, and push this file.
 * 3) Do this BEFORE any other pushes so the rule will be in effect.
 * 1) Do this BEFORE any other pushes so the rule will be in effect.

MonteCarlo/fermi.txt Examples/mctuple.root Examples/mctuple.hbook Examples/ntuple.root Examples/log Examples/omega2p_sim_1.itape Examples/ntuple.hbook
 * 1) These files are above GitHub's recommended file size (50 MB). SUBFOLDER/FILE


 * 1) These are object files. Code that is compiled to machine code, no need fo me to saved/tracked in GitHub.
 * 2) * 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.*

<li> Ignore already checked in files : &ensp; - Since you already tried and failed to fully push those file, you'll need to untrack them &ensp; - From your terminal type git rm --cached FILENAME  &ensp; &ensp; Hint - You can copy/paste from the find results git rm --cached /SUBFOLDER/FILENAME  &ensp; - Do this for all files that require it before proceeding </li>

<li> Add, commit, and push the .gitignore file : &ensp; - Add the gitignore file using git add .gitignore  &ensp; - Commit the file using git commit -m "Files to ignore"  &ensp; - Push the commit using git push origin main  &ensp; &ensp; Now subsequent pushes will know to ignore those files or types of files </li>

Extras
<ul>

<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) &ensp; - This site is a good starting point for common questions</li>

<li>git status &ensp; - This command shows the branch you're on, what files are in the working or staging directory, and other important information &emsp; Example : 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) </li>

<li>git reset &ensp; - This command unstages all changes (e.g. all git add performed before a git commit) </li> git reset file-name &ensp; - This command undoes git add for uncommited changes to file file-name

</li>