Skip to content

Instantly share code, notes, and snippets.

@KL13NT
Last active March 7, 2023 22:02
Show Gist options
  • Save KL13NT/2c9a6cfca216f7bfa3d47e5c2c987ab1 to your computer and use it in GitHub Desktop.
Save KL13NT/2c9a6cfca216f7bfa3d47e5c2c987ab1 to your computer and use it in GitHub Desktop.
A collection of git commands used frequently. Formatted as a set of steps for each task.

Table of Contents

Maintaining a branch

# clone target repo
git clone <repo_https_link|repo_ssh_path>

# checkout target branch
git checkout <branch_name>

# create, edit, and add files to staging
git add <file_names|.>

# create a new commit 
git commit -a -s -m "<commit_message>"

# Passed flags are:
# - a: --all
# - s: --signoff
# - m: --message

# merge target branch to another
# checkout the branch you want to merge to first then merge
git checkout <branch_to_merge_to>
git merge --no-commit <other_branch>

# push changes to remote
git push <remote> <branch_name>

Reset specific file

git reset HEAD -- filename

Named Stash

To save a stash with a specific name

$ git stash push -m "name"

To retrieve

$ git stash list
stash@{0}: On better-reminder: #11
stash@{1}: On master: thanks

$ git stash pop/apply stash@{0}

Move change between commits

Let's say you have 3 commits to unfuck:

$ git log -3 --oneline
97e9d62 Commit C, editing file2.txt
34b066b Commit B, creating file2.txt + editing file1.txt
73ff1bb Commit A, creating file1.txt

Now, if you want the Commit B to only contain the file2.txt creation but not the file1.txt edition which should be in Commit C, git rebase -i will display this:

pick 73ff1bb Commit A, creating file1.txt
pick 34b066b Commit B, creating file2.txt + editing file1.txt
pick 97e9d62 Commit C, editing file2.txt

# ...

If I replace "pick" with "edit" or "e" for Commit B and Commit C and close my editor, git will stop on the second commit and let me amend my changes:

pick 73ff1bb Commit A, creating file1.txt
edit 34b066b Commit B, creating file2.txt + editing file1.txt
edit 97e9d62 Commit C, editing file2.txt

Stopped at 34b066b... Commit B, creating file2.txt + editing file1.txt
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

$ vi file1.txt    # edit of file1.txt to remove the updated line
$ git commit --amend file1.txt
$ git rebase --continue

# -- git continues rebasing and stops on the Commit C to edit it

$ vi file1.txt   # edit of file1.txt to put the removed line
$ git commit --amend file1.txt
$ git rebase --continue

Working with remote changes

Some time passed and others made changes to your remote branch?

# merge others changes without creating a merge commit
git pull <remote> <branch_name> --no-commit

# solve conflicts
# do your work and make changes
# push changes to remote

Maintaining a fork

Do everything as above but clone the fork instead of the original repo. Sometimes you may want to squash commits before pushing to your fork. To do this:

# squashing commits 
git rebase --interactive HEAD~<number_of_commits_backwards>

# change 'pick' to 'squash' adjacent to commits you want to squash
git push -d <remote> <branch_name> # remote
git branch -d <branch_name> #local

Rebasing fucks up the history, if changes are already pushed to remote avoid it

Keeping a fork up to date

These commands are specific to keeping your fork up to date

# clone your fork
git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git

# add remote from original repository in your forked repository:
cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream

# update your fork with original repo to keep up with remote changes
git pull upstream master

Disclaimer: Keeping your fork up to date is taken from the great Keeping your fork up to date

How to unfuck stuff according to remote

Fucked up your local copy of a branch so bad that you need to reset to remote?

# reset your local copy to *current* upstream
git reset --hard @{u}

# delete untracked files 
git clean -df

# You may also pass it `-x` to remove ignored files as well.
# pull latest branch updates
git pull

The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don't have to be explicitly specified.

You can also alias it

alias resetthisbranch="git reset --hard @{upstream}"

Or you can do it using the reflog.

git reflog
# you will see a list of every thing you've
# done in git, across all branches!
# each one has an index HEAD@{index}
# find the one before you broke everything

git reset HEAD@{index}
# magic time machine

Reset

Reflog

How to unfuck stuff back to the previous commit

Sometimes you may just want to remove changes that occured after the last successful local commit. In that case you can just reset your local copy to your history's head.

git reset --hard HEAD

Want to modify last commit? Amend it!

# make your change
git add . # or add individual files
git commit --amend --no-edit

# now your last commit contains that change!
# WARNING: never amend public commits

Source

Want to edit last commit message? Also amend it!

git commit --amend -m "new message here"

Accidentally commited to the wrong branch?

# undo the last commit, but leave the changes available
git reset HEAD~ --soft
git stash

# move to the correct branch
git checkout name-of-the-correct-branch
git stash pop
git add . # or add individual files
git commit -m "your message here";

# now your changes are on the correct branch

A lot of people have suggested using cherry-pick for this situation too, so take your pick on whatever one makes the most sense to you!

git checkout name-of-the-correct-branch

# grab the last commit to master
git cherry-pick master

# delete it from master
git checkout master
git reset HEAD~ --hard

Source

Nothing shows up in diff even though you added changed files?

If you know that you made changes to files, but diff is empty, you probably add-ed your files to staging and you need to use a special flag.

git diff --staged

Source

Need to undo something from a past fuck-up commit?

# find the commit you need to undo
git log

# use the arrow keys to scroll up and down in history
# once you've found your commit, save the hash
git revert [saved hash]

# git will create a new commit that undoes that commit
# follow prompts to edit the commit message
# or just save and commit

Source

Wanna bring back a dead soldier (aka working version of a file before you fucked it up)?

# find a hash for a commit before the file was changed
git log

# use the arrow keys to scroll up and down in history
# once you've found your commit with the file version you want, save the hash
git checkout [saved hash] -- path/to/file

# the old version of the file will be in your index
git commit -m "Wow, you don't have to copy-paste to undo"

Source

Fucked up so hard you can't even reset like a normal human being?

You can do this. Or just, ya know... blow the fuck out of that whole repo folder and clone it all over again:

cd ..
sudo rm -r fucking-git-repo-dir
git clone https://some.github.url/fucking-git-repo-dir.git
cd fucking-git-repo-dir

Rename case sensitive

# Only rename a file
git mv OldFileNameCase newfilenamecase

# If it fails, try passing -f to it 
git mv -f OldFileNameCase newfilenamecase

Or your can set git to be case-sensitive. There are some issues, however, with this method.

git config core.ignorecase false

Explore stashes

# this allows you to see a diff of latest stash
git stash show -p

# you can make it more specific to a single stash
# to get all stashes and their indexes
git stash list 

# diff only a specific stash, replacing `n` with the index
git stash show -p stash@{n}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment