High-Frequency Git Operations for Everyday Development

Whether you're joining a new project, dealing with ignored files, or wanting to clean up your commit history, you'll often need to execute a set of Git commands. Here are some of the most frequent Git operations you may need in day-to-day development.

Connect Your Local Folder to a GitHub Repository

Scenario

You have an existing local folder with files and want to connect it to an already existing GitHub repository.

Steps

Initialize Git in your folder:

1
git init

Add your GitHub repository (replace URL with your repo):

1
git remote add origin https://github.com/username/repo.git

Fetch remote history:

1
git fetch origin

Merge remote and local files, allowing unrelated histories (change main to your repo's default branch if necessary):

1
git pull origin main --allow-unrelated-histories

If you see an error like:

The following untracked working tree files would be overwritten by merge...

We recommend:

1
2
3
4
5
6
7
8
9
# 1. Move local conflicting files to a temporary directory
# For example, back them up to a non-conflicting `backup` folder in the current directory
mkdir backup
mv conflicting-file-1 conflicting-file-2 backup/

# 2. Try pulling the remote repository content again
git pull origin main --allow-unrelated-histories

# 3. Manually merge the backed up files into the pulled files

Commit after resolving all conflicts:

1
2
git add .
git commit -m "Merge local files with remote repository"

Push to the remote repository (if needed):

1
git push --set-upstream origin main

The --set-upstream origin main option is only needed for the first push; you don't need it afterward.

List What Files git add . Would Stage

1
git status --short

Example Output:

1
2
M file1.txt       # modified file
?? newfile.txt # untracked file

Breakdown:

  • M = Modified (but not staged)
  • ?? = Untracked (new files)

Remove Previously Committed Files Now in .gitignore

Scenario

You added rules to .gitignore but some files were already committed.

Steps

Stage removal of all currently tracked files (but not delete locally):

1
git rm -r --cached .

Add everything back to the repo ("add" skips gitignored files):

1
git add .

Commit the change:

1
git commit -m "Remove ignored files from the repository"

At this point, the files are only removed from the repository from this commit onward (they'll still exist in older commits/history). If you want them removed from previous commits as well, consider squashing multiple commits into a single commit, as explained below.

Squash Multiple Commits into a Single Commit

Scenario

You have made several small commits (some of which may be faulty or embarassing) and want to clean up history by squashing them into one.

Steps

Review commit history:

1
git log

Decide how many previous commits you want to squash.

Start an interactive rebase, e.g. with the last 3 commits:

1
git rebase -i HEAD~3

In the opened editor:

  • Leave pick for the first commit.
  • For the others, change pick to squash (or just s).
  • Save and close.

You will then enter a second editor session. Edit the combined commit message. Save and close.

Force-push the branch to rewrite history on GitHub:

1
git push --force

Note

  • Use squashing carefully if collaborating, as force push overwrites history.

High-Frequency Git Operations for Everyday Development
https://jifengwu2k.github.io/2025/08/21/High-Frequency-Git-Operations-for-Everyday-Development/
Author
Jifeng Wu
Posted on
August 21, 2025
Licensed under