s

Git

By Angela C

April 18, 2021 in GitHub git

Reading time: 10 minutes.

Some notes on using Git and GitHub including some of the more common commands that I have come across. (I will tidy this up.)

Git is a free open-source distributed version control system while GitHub is a platform for hosting and collaborating on Git repositories. It can be downloaded here.
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.

Git is one of the most popular implementations of version control. Using git everything is stored in local repositories on your computer. It is usually used from the command line.

GitHub is a code hosting platform for collaboration and version control which allows you and others work together on projects. Users can keep a backup of their projects on the Github.com server and share their repositories with other users. Users push from local repository to the remote copy of the repository on GitHub.com and can also pull any changes made from the remote repository down to their local repositories.

Some useful commands when using git

  • --git version to see the current version of git installed

  • git config: Some configuration is required the first time you send changes to Github in order to keep track of which user made which change.

    • git config --list to check current username and email in local repository. (Multiple accounts are possible)
    • git config --global user.email "email@email.com"
    • git config -- global.user.name"username"
    • git config user.password "password"

    Exclude --global to apply username and email to a specific local repository

  • git init to initialise a current folder as a git repository - a folder that is tracked by git.

    • Create a blank repo on Github (without adding a README file)

    • Go into folder on local machine and send it to the blank repo on Github

    • git init command to initialise the folder as a git repository (a folder that is tracked by git).

    • git add <filename> to add a file

    • git add . to add all files in the folder

    • git commit to take a snapshot of the work in the folder at that point in time.

    • git remote add origin https://github.com/username/repositoryname.git tells your local git about the remote repo at Github which is currently blank. It points your local repository at the remote repository created on the GitHub.com server. (origin refers to repository original, master or main to the main branch)

    • git -push -u origin master for the first time you sync the local repo with the remote repo at Github

    • git push thereafter

  • mkdir to create a directory on local machine to store the local copy of the repository.

  • git init to initialise an existing local folder as a git repository

  • git clone [url] to clone or download an existing repository from GitHub including all the files, branches and commits to a local machine.

  • cd to change directory into the new directory

  • git remote -v to see the name of the remote that is linked to the local repository.

  • git remote set-url origin <new_url> where ‘new_url’ is the updated name of the repo.

  • git status to see the current status of a local git repository. If there are files in the folder that are not currently tracked by git, it will show these as untracked files. It will also list modified tracked files.

  • git add <file> to add a file to the git repo to be tracked

  • git add . to add the current folder to be tracked or to add changes made in tracked files. Takes a snapshot of the file in preparation for versioning.

  • git add -u will update tracking for files that were deleted or had their names changed.

  • git add -A or git add --all does both git add . and git add -u.

  • git commit to record the snapshot of the changes to the history

  • git commit -m"descriptive commit message". Provide a useful descriptive message of the changes made since the last changes were committed.

  • git commit will only update the local repo and not the remote repo at GitHub.com.

Changes made remotely

  • Changes or edits can be made to the repo at Github or other people could be collaborating on a remote copy of the project.
  • git pull to pull the changes down to your machine

To go back to a previous version of the repository

  • git log to show all commits made, when and by whom.
  • git checkout '40odd-digit-hash-code' to get back to the repo at a specific point in time.
  • note the warning of a detached head state
  • git checkout master to get back to the latest version
  • to set the url of the github repo.
  • git remote set-url origin https://newurl to update to new username if updated on github.

At each commit git takes a snapshot of the local repository and compares it to the previous commit. The changes only are saved in the .git folder. When you go back to a previous version using git checkout command with a hashcode as above, git goes back and undoes the changes since that commit to get back to the version of the repo that existed at that point in time. These changes are put in the .git folder.

  • git checkout master will bring back the repo to the latest commit.

Undo a git add

  • To undo a git add . before git commit run git reset to unstage the changes.
  • git reset <file> to unstage a particular file.

Renaming a git repository

  • A git repository can be renamed on GitHub. The remote repo will continue to work with the local repo name but it is recommended to update the name of the local repository to point to the new repository URL.

A branch is a moveable pointer to a commit while a commit is a snaphot of the entire repository (compressed into a SHA). See Glossary at GitHub Git cheatsheet.

Forking another user’s repository You can create a repository by forking or making a copy of someone else’s work.

  • A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

  • From the repository of interest on GitHub.com click the fork button. This will create a copy of the repository on your own GitHub account.

  • clone or make a local copy of the repository on your local machine as above
    git clone https://github.come/username/repo-name.git

  • If you make changes or add to a repo you forked from someone else you can request the original owner of the repo to merge your changes into their code by issuing a pull request. The original owner of the repo does not have to accept your changes though.

git remote -v to see the current configured remote repository for your fork.

  • git remote add upstream <with the URL of the original repo that you forked and cloned>
  • git remote -v to verify the new upstream repository specified for your fork. This should show the URL for your fork as the origin and the URL for the original repo as upstream.

Once this is setup, your fork of the repo can be kept synced with the upstream repo with a few git commands. See syncing a fork guide

Before you can sync your fork with an upstream repository, you must configure a remote that points to the upstream repository in Git. In the Terminal cd into the local repo.

  • git fetch upstream to fetch the branches and their respective commits from the upstream repository. Commits to master will be stored in a local branch, upstream/master.

  • git checkout master (or git checkout main) to check out your fork’s local master or main branch. This will tell you if your branch is up to date with ‘origin/master’.

  • git merge upstream/master to merge the changes from upstream/main into your local master branch. This brings your fork’s master branch into sync with the upstream repository, without losing your local changes.

  • git push Syncing your fork only updates your local copy of the repository. To update your fork on GitHub, you must push your changes.


Sample GitHub Workflow

  1. Create a new repo on GitHub.com

  2. Copy URL of the repository to the clipboard.

  3. Create a new local directory using mkdir and cd into this folder

  4. Create a new file in directory touch testfile.md

  5. git init to create a git repository by initialising a git repo in the current folder

  6. git status to get a status of the local repo. This will show the new file is not yet tracked.

  7. git add testfile.md to stage the file for committing (ie start tracking the new file) or git add . to add everything to start tracking all untracked files currently in the repo.

  8. Commit the changes using git commit -m"commit message"

  9. git status should show nothing new to commit

    • git log can be used to check the log or history of all commits so far.
  10. Link the remote repo at GitHub to the local repo using git remote add origin <https://github.com/user-name/repo-name.git> using the URL from steps 1 and 2 above.

    • git remote -v can be used at any time to check the remote repo that is setup for the local repo.
  11. Push changes made locally to the remote repo at GitHub.

    • git push -u origin master for the first time only which tells Git to push the changes made to the master/main branch of the origin (original or primary copy of the remote copy of the repo at GitHub).
    • git push for any future pushes.
  12. git status can be used again to check the status. If the previous steps are followed, it should show that Your branch is up to date with ‘origin/master’ and nothing to commit, working tree clean

  13. Can check the remote repo at GitHub.com to see that the changes have been made to the remote repo there.

  14. If you did not initialise the repo with a README file when creating the repo you can do so on the remote repo at GitHub.com by following the instructions to add a README. Click the Add a README button, edit and the click the Commit New File button. This README file will then need to be pulled down to the local repo.

    • git pull in the local repo folder will pull down the changes made.
  15. You could add another new file on the remote repo (although this is not normally done as far as I know) by adding and editing a new file then pressing the green Commit new file button. Commit directly to the master branch. Then git pull locally from the local repo to pull the changes at the remote repo down to the local repo.

    • Click Add file button, edit the file then click the green Commit new file button
    • git pull to pull the changes down to local repo
  • The command git branch -a will show that there is now two branches locally:

        * master
    
    remotes/origin/HEAD -> origin/master
    remotes/origin/master
    

    where master is the local files on the local repo and remotes.origin/master are the files pulled from the remote repo.

  • git fetch vs git pull

    • git fetch command tells your local git to retrieve the latest metadata info from the original without doing any actual file transferring.

    • git pull does the same as git fetch as well as actually transfer the files from the remote repo.

    • git fetch is used to check for changes made remotely rather than makes the changes locally to match.

    • If you use git status after git fetch you will see that your master branch is * behind ‘origin/master’ by 1 commit, and can be fast-forwarded*

    • git pull then to update your local branch.

  • look at git diff, branches, pull requests etc.


Git Submodules

https://git-scm.com/book/en/v2/Git-Tools-Submodules

Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.


References