Git Cheatsheet

Published May 11, 2025, 2:03 a.m. by james

Git Cheatsheet

1. Initialize a Repository

git init

2. Configure .gitignore

Add files/directories to be ignored by Git. For example, to ignore node_modules:

# Add this line to your .gitignore file
echo "node_modules/" >> .gitignore

3. Commit Changes

Stage all changes and commit them with a message.

git add .
git commit -m "Your commit message"

4. Remove Files from Repository (but keep local copy)

To stop tracking a file but keep it in your working directory.

git rm --cached <file.txt>
git commit -m "Stop tracking <file.txt>"

5. Undo Last Commit (keep changes)

Resets the HEAD to the previous commit, keeping the changes from the undone commit in your working directory as unstated changes.

git reset --soft HEAD~1

6. Move Current Uncommitted Changes to a New Branch

If you started working on changes and realize they should be on a new branch:

git checkout -b <new-branch-name>
# Your uncommitted changes will come with you to the new branch.
# Then add and commit them as usual.
git add .
git commit -m "Initial commit on new branch"

7. List All Branches

Shows local and remote-tracking branches.

git branch -a

8. Undo Adding Files to Staging (Unstage)

If you've used git add but want to unstage changes for a specific file:

git reset HEAD <file.txt>

To unstage all files:

git reset HEAD

9. Discard Changes in Working Directory

Revert changes to a file to match what's in the last commit (HEAD).

git checkout -- <file.txt>

10. Remove a Branch Locally

git branch -d <branch-name> # Use -D to force delete if it has unmerged changes

11. Remove a Branch from Remote

git push origin --delete <branch-name>

12. Remove a File from All History

Requires a tool like git-filter-repo (recommended replacement for filter-branch). Caution: Rewrites history. Be careful, especially in shared repositories.

# First, ensure git-filter-repo is installed.
# Then, run from the root of your repository:
git filter-repo --path <path-to-your-file.txt> --invert-paths
# After confirming locally, you may need to force push to all remotes if shared:
git push origin --force --all # Potentially dangerous
git push origin --force --tags # Potentially dangerous

13. List All Files Tracked by Git

git ls-files

14. Ignore All Dot Files (e.g., .env, .DS_Store)

Add a pattern to .gitignore to ignore all files starting with a dot.

# Add this line to your .gitignore file:
echo ".*" >> .gitignore
# Note: This is broad. Be specific if possible (e.g., .DS_Store, .env).
# If you want to ignore hidden directories, use .*/

Git Branching Techniques

1. Create and Switch to New Branch Immediately

git checkout -b <new-branch-name>

# Or, using the 'switch' command (Git 2.23+):
git switch -c <new-branch-name>

2. Create Branch and Switch Later

# Step 1: Create the branch (stay on current branch)
git branch <new-branch-name>

# Step 2: Switch to the new branch when ready
git checkout <new-branch-name>

# Or, using the 'switch' command for Step 2 (Git 2.23+):
git switch <new-branch-name>

3. Create New Branch from an Existing Branch

This creates <new-branch-name> based on the current state of <existing-branch-name> and switches to it.

git checkout -b <new-branch-name> <existing-branch-name>

# Example: Create 'feature/new-widget' based on 'develop'
git checkout -b feature/new-widget develop

4. Create New Branch from a Specific Commit

git checkout -b <new-branch-name> <commit-hash>

5. Move Current Uncommitted Changes to New Branch (Direct Method)

# Step 1: Create the new branch and switch to it
# (current uncommitted changes will come along)
git checkout -b <new-branch-name>

# Step 2: Stage your changes (if not already)
git add . # Or git add <specific-file>

# Step 3: Commit your changes to the new branch
git commit -m "Your commit message"

6. Move Current Uncommitted Changes to New Branch (Stash Method)

# Step 1: Stash your current changes
git stash push -m "Work for new branch"
# (Optional: use -u to include untracked files: git stash push -u -m "...")

# Step 2: Create the new branch and switch to it
git checkout -b <new-branch-name>

# Step 3: Reapply your stashed changes
git stash pop # Or git stash apply stash@{0}

# Step 4: Stage and commit your changes
git add . # Or git add <specific-file>
git commit -m "Your commit message"

Merging 'devel' into Target Branch and Resolving Conflicts

1. Switch to Target Branch and Update

Ensure you are on the branch you want to merge changes into (e.g., v0.2, main), and that it's up-to-date with the remote repository.

# Switch to your target branch (e.g., main)
git checkout <target-branch-name>

# Pull the latest changes for this branch from the remote (optional, but recommended)
git pull origin <target-branch-name>

2. Merge 'devel' into Target Branch

Start the merge process to bring changes from devel into your current target branch.

git merge devel

If there are no conflicts, Git will create a merge commit automatically. If there are conflicts, Git will pause, and you'll need to resolve them.

3. Understanding Merge Conflicts (e.g., add/add)

A conflict occurs when Git cannot automatically combine changes from both branches. An "add/add" conflict, for example, means a file with the same name was created independently in both branches.

# Example conflict message you might see:
CONFLICT (add/add): Merge conflict in <file-path>

4. Resolving Merge Conflicts

You must manually intervene to tell Git how to combine the changes.

Step 4a: Inspect the Conflicted File(s)

Open the conflicted file in your editor. Git usually marks conflicts with <<<<<<< HEAD, =======, and >>>>>>> devel (or the other branch name).

To see specific versions for an "add/add" conflict or other complex cases:

# View version from your current branch (OURS/HEAD)
git show HEAD:<file-path>

# View version from the branch being merged (THEIRS/MERGE_HEAD)
git show MERGE_HEAD:<file-path>
# (Or use the source branch name: git show devel:<file-path>)

Step 4b: Decide and Edit

Choose which version to keep, or manually edit the file to combine the desired parts from both versions. After editing, remove the conflict markers.

# Option 1: Keep the version from your current target branch (OURS)
git checkout --ours <file-path>

# Option 2: Keep the version from the 'devel' branch (THEIRS)
git checkout --theirs <file-path>

# Option 3: Manually edit <file-path> to merge contents.
# (After manual edit, save the file.)

Step 4c: Stage the Resolution

After editing and saving the file with the correct merged content, tell Git the conflict is resolved by adding the file.

git add <file-path>

If you had multiple conflicted files, resolve and git add each one.

5. Commit the Merge

Once all conflicts are resolved and staged, complete the merge by committing. Git usually suggests a default merge commit message.

git commit

If you see an error like "Merging is not possible because you have unmerged files," it means you missed git add for a resolved file.

6. Push Changes (Optional)

After the merge is successfully committed locally, push the changes in your target branch to the remote repository.

git push origin <target-branch-name>

7. Aborting a Merge (If Needed)

If you get stuck or want to start over before committing the merge resolution, you can abort:

git merge --abort

Share this post

Similar posts

Bash cheatsheet

Remove All Signatures from a PDF

comment

There are no comments yet.

Add a new comment