Git Commands

Terminology

origin =

main =

master =

remote =

Head =

Headless mode =

Modified =

Staged =

Committed =

Informational Commands

Git can be a little intimidating at first because it has the power to change the files you are working on, so it’s good to start with commands that don’t do anything to get a feel for the situation before you actually do something.

git config --list

Shows useful information about the current repository. Shows the remote.origin.url variable for example.

git config --get remote.origin.url

Shows just the url associated with local git repository.

git config --list --show-origin

Shows ___.

git status

Probably the most common git command.

git diff

To see changes since the last commit. Or

git diff > gitdiff.txt

for a text file of the information.

ls -al ~/.ssh

To check for SSH keys (on Linux only I think).

git log --oneline

Is useful to see all the commits in a row with their commit messages. Or

git log --oneline --decorate --graph --all

for more detailed information.

git log -p filename.py > log.txt

Makes a file with the entire change history of filename. The -p is patch text. This is great for resurrecting “lost portions” of code and replaced the file I used to keep: “unused code.txt”

git branch -v -a

Lists all the branches including remotes, with the current branch highlighted in green plus an asterisk.

git branch --show-current

Shows just the current branch.

git tag -n1

Says to list all the tags and show 1 line of annotation per tag. Default is 0 lines of annotation.

Initial Set-Up Commands and Connecting to GitHub

Notice above command to check for existing keys.

The first step is establishing an SSH key on the machine to be connected to GitHub then copy the public key to GitHub. This is a pretty good description of how to do that. Notice Git bash in Linux is just the command line of a Linux machine with Git installed while on Windows you have to open the Git bash executable for a Git-specific terminal.

git clone [github url].git

Notice the format of the remote url determines SSH versus https transfer.

To initialize, navigate to the directory where your code is stored, and:

git init

to initialize git tracking of the directory.

git add

to add files. You can add one at a time to be selective or

? how to add all the files in the directory, then ignore?

Daily “Save Your Work” is Commit

This is a pretty good list here, but it lacks informational commands.

git commit -a -m 'commit message here'

git push

Are the daily “save your work” commands.

Renaming a File

git mv old-filename.extension new-filename.extension

To rename a file, use git to rename it instead of renaming it manually. This allows git to track the rename instead of appearing to be “delete and add new.”

Deleting a File

git rm filename

To delete a file, use git to allow git to track the deletion. Don’t do it manually or it just shows up missing.

Revert a Single File to an Old Version

git checkout <commit ID> filename

There are various ways to do this (restore and revert maybe?) but I did it once like this and it worked. You have to be OK with losing any work in that file since the last commit. The point is it doesn’t affect the other files.

Branching

git branch <new-branch-name>

To create a new branch.

git switch <new-branch-name>

To switch to a different branch (same as the still-valid git checkout <new-branch-name>)

git checkout -b new-branch-name

To create a new local branch and switch to it in one command.

git push --set-upstream origin new-branch-name

? is that right, not just push?

How to create a branch on GitHub here.

Tagging

git tag -a v1.0.0 ec595fb -m 'message here'

then

git push origin v1.0.0

to tag a past commit. Version naming convention is [Major].[Minor].[Patch]

Merging

To merge, create a pull request on GitHub, then compare and merge. It’s pretty self-explanatory, THEN to update your local repository:

git fetch

because you made changes to the GitHub version, but not your local version, so the changes have to be fetched (opposite of push I think?)

git switch main

to switch to the main branch, and you should see a message saying the local main is behind origin/main, then

git pull

to pull the commits into the local branch. Merge complete.

Remote Repository (GitHub Usually)

git remote -v

lists the remote repositories.

git remote set-url origin git@github.com:rr34/Astro.git

allows you to change the remote url for the origin.

GitHub-Specific Features

The following are features in GitHub only, not classic command line Git:

  • Releases – but tags are command line and also a way of doing releases.
  • Pull requests