Skip to content

Instantly share code, notes, and snippets.

@zmertens
Last active January 7, 2024 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmertens/0f9466c778c212644b7350f0f9cdd64b to your computer and use it in GitHub Desktop.
Save zmertens/0f9466c778c212644b7350f0f9cdd64b to your computer and use it in GitHub Desktop.
A guide to command line git

git-schooled

  1. Create or fork a repo from GitHub.
  2. Clone the repo with git clone <https_url_to_rep> or git clone <ssh_url_to_rep>.
  • This command copies the repo from the url repository to your local system.
  • It will, by default, set the existing branch to be that of the cloned repo (usually master or main).
  • Verify all branche names and the current working branch with git branch -a.
    • Switch to a new branch with git checkout -b <new_branch> assuming no file changes are happening on the current working branch.
      • New branch name can start with something like dev, fixes-2, or link to a ticket or issue name ISSUE-345.
  1. List credentials with git config -l.
  • This will show your git credentials that have been set on your local machine (like email, username, settings, and so forth).
  • These two commands below set credentials on your local machine.
    • Use git config --global user.name "my name" to set your user name
    • Use git config --global user.email "my email" to set your user email
  1. Create an alias to a remote repo using git remote add <remote_short_name> <remote_url>, for example, use git remote add upstream https://www.github.com/org/branch to link to a remote branch called "upstream".
  2. Take a breather and ensure the repo/branch/changes are where they should be using git status.
  • git status shows recent local changes with respect to files that differ to the HEAD branch, for example, a remote branch.

Making a change...

  1. Run the command, echo "Taking care of business\n\t-by Bachman-Turner Overdrive" | tee -i lyrics.txt, to create a basic change on your local system.
  2. Run git add --all to add the local change (a new file). This is called "staging changes for commit".
  3. Commit the change using git commit -m "Taking care of business".
  • Ensure the change is logged with git log -3 (checks the log for the last three commits).
  1. Verify the changes against a specific branch like git diff upstream/main.
  • Add the switch --name-only to the previous command to see just the file name with changes.
  1. Look good? Then use git push origin dev to push to your remote repo on the dev branch. Use the GitHub to create a Pull Request with respect to another branch.
  • Do git push to the remote repo it will ask for your GitHub username and password unless they are stored. See Bonus Commands to see how to use credential helpers.

Next Steps

  1. Checking in changes from the remote to your local machine using git fetch upstream and then git merge upstream/main.
  • See Bonus Commands for steps on how to use the stash to make a change to a separate branch when there are pending changes on the current working branch.
  • Sometimes the current working branch must be reset to a local or remote branch's state. This can be done using git reset --soft <commit_hash> to preserve current changes on the local branch, or git reset --hard <commit_hash> to delete current changes on the local branch and check in changes from the other commit.

Bonus Commands

  • git mergetool -t <text_editor_name> will set the default text editor.
  • git remote -v will show all remote repo's.
  • git remote update to just update the remote changes but not fetch them.
  • git config --local credential.helper store to store GitHub or other repo credentials.
  • git stash is awesome. In a nutshell, it works as a local database where you can save changes staged for commit
    • View the history using git stash list.
    • Pop off the history stack using git stash pop stash@{X} where "X" is a historic id on the stack.
    • Drop changes using git stash drop stash@{X}.
    • Exmple usage is to save changes but not commit to a current working branch:
      1. Start with git add --all and then git stash save "some message".
      2. Follow-up with a git checkout -b <new_brach_name> and then git stash pop to carry-over these changes without making a commit to the prior working branch.
  • git rebase -i "remote/branch"
    • Rebasing is simple, in theory, but in practice it can lead to merge conflicts which are files that have similar changes on different branches (like the current working branch and a branch on a remote repo). It also rewrites commit history.
    • Using git rebase -i HEAD~X one can use their default text editor to resolve change conflicts between commits.
      • Repeat the step above until you can do a git rebase --continue or back-out with git rebase --abort.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment