Common Git Operations and Workflows

View commit history from the most recent to oldest commit:

  • git log for full detailed output
  • git log --pretty=oneline for condensed single-line per commit output

Revert to the previous commit version: git reset --hard HEAD^

Git uses HEAD to reference the current latest commit. The previous commit is HEAD^, the commit before that is HEAD^^. To jump back 100 commits, you can use the shorthand HEAD~100 instead of writing 100 individual carets.

Jump to any specific version by its commit hash: git reset --hard <partial-commit-hash>

You do not need to enter the full 40-character commit hash; the first 5-7 characters are enough for Git to uniquely identify the target commit. Avoid using only 1-2 characters, as Git may not be able to distinguish between multiple matching hashes in larger repositories.

View a full history of all HEAD position changes to find lost commit hashes after reversion: git reflog Once you have the target hash, run git reset --hard <hash> to jump directly to that version.

View the content of a text file: cat README.md

Add changes to the Git staging area:

  • git add <file-name> to add a specific file
  • git add . to add all changed files in the current directory

Compare differences between the working directory copy and the version committed in HEAD: git diff HEAD -- <file-name>

Discard all uncommitted changes to a file in the working directory: git checkout -- <file-name>

Undo staging of a changed file and move it back to the working directory: git reset HEAD <file-name> After this step, you can run git checkout -- <file-name> to discard the changes completely.

If changes have already been committed to the local repository, undo the latest commmit and revert to the previous version: git reset --hard HEAD^

If you deleted a file from the working directory after it was committed to the repository, git status will immediately notify you of the mismatch. You have two options:

  1. If you intend to delete the file from the repository as well:
git rm test.txt
git commit -m "remove test.txt"
  1. If the deletion was accidental, restore the file from the repository to your working directory: git checkout -- test.txt

Connecting to a Remote GitHub Repository

Step 1: Generate an SSH key pair. Check your user home directory for a .ssh folder. If you already have id_rsa (private key) and id_rsa.pub (public key) inside, you can skip this step. If not, open your shell (Git Bash on Windows) and run:

ssh-keygen -t rsa -C "youremail@example.com"

Replace the email with your own, press Enter to accept all default settings, no password is required for personal use. The resulting id_rsa is your private key, which must be kept secret, and id_rsa.pub is the public key that can be shared with GitHub.

Step 2: Add the public key to GitHub. Open "Account settings" → "SSH Keys", click "Add SSH Key", add a descriptive title, and paste the full content of id_rsa.pub in to the Key input field.

Link a local repository to your remote GitHub repository: git remote add origin git@github.com:<your-username>/<repo-name>.git

Push all local commits to the remote repository for the first time: git push -u origin master The -u flag is only required for the first push to set up upstream tracking.

To create a local copy of an existing remote repository, clone it directly: git clone git@github.com:<your-username>/<repo-name>.git

Working with Branches

List all local branches, the current active branch is marked with an asterisk: git branch

Create a new branch named dev and switch to it immediately: git checkout -b dev

After making and committing changes on dev, switch back to master and merge the changes:

git add README.md
git commit -m "Add feature changes"
git checkout master
git merge dev

Delete the dev branch after merging is complete: git branch -d dev

Merge conflicts occur when two branches modify the same section of the same file. For example, after modifying and committing the same file on two different branches, merging will produce a conflict:

git switch -c feature-login
git add index.html
git commit -m "Update section content"
git switch master
git add index.html
git commit -m "Update same section content"
git merge feature-login
Auto-merging index.html

Git marks conflicting regions with special markers:

<<<<<<< HEAD
Content from current branch
=======
Content from the branch you are merging
>>>>>>> feature-login

Edit the file to resolve the conflict, remove the conflict markers, save the file, then commit the resolved change:

git add index.html
git commit -m "conflict fixed"

Stash your unfinished working changes to switch to other tasks: git stash

After you finish work on the other task, switch back and restore your stashed changes in one of two ways:

  1. git stash apply: Restores changes but leaves the stash entry in your stash list. You need to delete it manually with git stash drop.
  2. git stash pop: Restores changes and automatically deletes the stash entry after restoration.

List all saved stashes: git stash list

To restore a specific stash from multiple saved entries, use: git stash apply stash@{0}

If you fixed a bug on the master branch and need the same fix on a development branch, use git cherry-pick <commit-hash> to copy the bug fix commit directly to the current branch, avoiding duplicate work.

Always create a new branch when developing a new feature. To delete an unmerged branch, force delete it with: git branch -D feature-vulcan

Collaborative Workflow

  1. Push your local branch changes to the remote: git push origin <branch-name>
  2. If the push fails, it means the remote branch has newer changes than your local copy. Pull the remote changes and merge them locally with git pull.
  3. Resolve any merge conflicts that occur, then commmit the resolved changes locally.
  4. Push the merged changes to the remote after conflicts are resolved.

If git pull returns no tracking information, that means your local branch does not have a tracking link set up to the remote branch. Fix this with: git branch --set-upstream-to <local-branch-name> origin/<remote-branch-name>

Tags: Git Version Control Command Line

Posted on Mon, 08 Jun 2026 18:04:52 +0000 by gudfry