Difference between revisions of "Git and github stuff"

From Ed's Mediawiki
(Git Functions I Use All The Time)
Line 25: Line 25:
 
This sends the repository to a remote repository (GitHub for me.) What it is really doing is sending the changes, so we can keep track of them. If the GitHub repository is now cloned, the whole history will come with it.
 
This sends the repository to a remote repository (GitHub for me.) What it is really doing is sending the changes, so we can keep track of them. If the GitHub repository is now cloned, the whole history will come with it.
  
'''Git pull'''
+
'''Git fetch'''
  
This updates a local repository with new data from a remote repository. I use this to keep my machines tracking. E.G. I make some changes and pushes from my laptop and I want to work on the stuff on the desktop. I use the pull command on the desktop to pull these changes into the local repository on that machine. It will also update the files based on changes made to the pulled repository.
+
This fetches the necessary information (metadata) to update it to the remote version. It does not change the files in the local repository. That is done with the "merge" command, which merges the changes into the local copy. It is actually more complicated than that when there are multiple branches in the repository. more formally, git fetch pulls in the latest remote commits, which may come from multiple developers, and then git merge combines all of the changes from those commits.
 +
 
 +
'''Git merge'''
 +
 
 +
After a fetch pulls in all of the latest commits from the remote repository, the git merge command executes the changes in those commits to a destination branch. This can get complicated, and I don't understand it beyond the basics, but since I am not doing complicated stuff, and usually only work on one branch, git merge for me just applies the commits I have made in the remote repository since the last time I committed from the local machine, effectively just pulling in the most recent version. The git pull command, included in some git interfaces, seems to effectively do a fetch followed by a merge.
  
 
==GitHub Authorization==
 
==GitHub Authorization==

Revision as of 18:07, 3 June 2021

I use Git and GitHub to version manage all software I write and maintain. I started using it with IDEs, like PyCharm and Android Studio, with support for Git built in, so I didn't really have to learn what was under the hood. Now using it with the Arduino IDE, I need to know enough to more directly use it. In addition, GitHub deprecating username/password authentication kind of forced me to dig into it some.

Git Basics

First of all, I use Git on Windows, so I am running Git for Windows. This includes the base Git software, which is available from the command line, Git Bash, which is an alternative Unix command line, Git Command, which is just an alternative Windows CMD window, and Git GUI, which is a graphical interface for Git. After trying GitHub Desktop and Tortoise Git, which are pretty decent GUIs, but have problems with authentication, I have settled on stuff directly from Git, which is a little clunky, but seems to always work.

To create and work on a local Git repository, you need to set the working directly to the directory that contains the code. E.G. for my current Arduino code, this is in C:\development\arduino\ejbfleet-v1\ The Git directory, .git is in there along with the source files.

Git Functions I Use All The Time

Git clone

Clones a repository, either local or remote (generally on GitHub.) I use this to copy up to date code to a machine where I want to work on it. For me, I generally start a coding project on ejbdesk or ejblap, and eventually have occasion to work on it on the other machine, and possible on my shop machine, ejbshop.

Git add .

In the gui, this is Stage Changed. Not sure why they use a different name, add vs stage, but so be it. This sets up all changed files to be committed to the repository. Git add <filename> or Git add <directory name> will stage specific things to commit.

Git commit

This commits staged files to the repository, i.e. marks them as changed and takes a snapshot of the changes. It is also customary for the person who does the commit to add commentary about what has changed, the state of the project, etc.

Git push

This sends the repository to a remote repository (GitHub for me.) What it is really doing is sending the changes, so we can keep track of them. If the GitHub repository is now cloned, the whole history will come with it.

Git fetch

This fetches the necessary information (metadata) to update it to the remote version. It does not change the files in the local repository. That is done with the "merge" command, which merges the changes into the local copy. It is actually more complicated than that when there are multiple branches in the repository. more formally, git fetch pulls in the latest remote commits, which may come from multiple developers, and then git merge combines all of the changes from those commits.

Git merge

After a fetch pulls in all of the latest commits from the remote repository, the git merge command executes the changes in those commits to a destination branch. This can get complicated, and I don't understand it beyond the basics, but since I am not doing complicated stuff, and usually only work on one branch, git merge for me just applies the commits I have made in the remote repository since the last time I committed from the local machine, effectively just pulling in the most recent version. The git pull command, included in some git interfaces, seems to effectively do a fetch followed by a merge.

GitHub Authorization

GitHub has deprecated the username/password method of authenticating a login as described here. In its place, the thing that I found made the most sense for me was to add a SSH key to each of my computers and put a matching public key on my GitHub account. Here is how I did it on my Windows 10 computers:

Create the keys