{% hint style="info" %} I would like to make a local copy of code from a public repository I found online. {% endhint %}
# for http-based
git clone https://[email protected]/zephinzer/blog.joeir.net
# for ssh-based
git clone ssh://[email protected]/zephinzer/blog.joeir.net
{% hint style="info" %} I would like to initialise a new Git repository on my computer. {% endhint %}
git init
{% hint style="info" %}
I would like to add a new remote named origin
to my repository.
{% endhint %}
git remote add origin ssh://[email protected]/zephinzer/blog.joeir.net
{% hint style="info" %}
I would like to update the URL for my remote named origin
in my repository.
{% endhint %}
git remote set-url origin ssh://[email protected]/zephinzer/blog.joeir.net
Fetching retrieves the changes but does not merge the changes with your local copy.
{% hint style="info" %} I would like to get updates from the remote but I don't want to update my code yet. {% endhint %}
git fetch
Merging takes the remote changes that have been fetched from the remote and merges them with your local copy.
{% hint style="info" %} I have reviewed the changes I retrieved from the remote and I want to update my local code to match the remote's copy now. {% endhint %}
git merge HEAD
Pulling basically does a fetch and merge.
{% hint style="info" %} I would like to update my code so that it is the same as the remote's. {% endhint %}
git pull
Pulling with rebase does a fetch, but before merging in the remote changes, it rolls back to a state before all remote changes were made, applies the remote changes, and then applies your local changes.
{% hint style="info" %} I would like to update my code by placing whatever's from the remote before my current changes so that it is the same as the remote's and I don't have a merge commit. {% endhint %}
git pull -r
{% hint style="info" %} I would like to pull in changes from another branch that's available locally and place those changes before the changes I've committed. {% endhint %}
# assuming we are on branch feature_x pulling in updates from master
git rebase master
{% hint style="info" %} I would like to temporarily store my unstaged changes so that I can pull in the latest updates from the remote. {% endhint %}
# put all unstaged changes into a stash
git stash
# checking stashed changes
git stash list
# popping the last stashed change
git stash pop
{% hint style="info" %} I would like to add file(s) that will be 'saved' during a commit. {% endhint %}
# to stage all changes, run this from project root
git add .
# to stage only one file
git add ./path/to/changed_file
{% hint style="info" %} I would like to save my changes to my local Git repository. {% endhint %}
git commit -m 'some message'
{% hint style="info" %}
- I would like to add a commit to my local repository without adding any files
- I would like to have a commit that can trigger a pipeline in the remote source control {% endhint %}
git commit --allow-empty 'some message'
{% hint style="info" %}
I forgot to run git add
on a file that should be in the previous commit.
{% endhint %}
# stage the missing file first
git add ./path/to/missed/file;
# this will add the staged file to the previous commit
git commit --amend
{% hint style="info" %} I have made 5 commits and I would like to compress them into a single commit so my Git history is cleaner. {% endhint %}
# indicate `p` or `pick` for the head commit, and `s` or `squash` for the rest
git rebase -i HEAD~5
{% hint style="info" %} I have made X number of commits to my branch and want to squash/rebase my commits within my branch so that a rebase with master will be cleaner {% endhint %}
git rebase -i HEAD~$(git log --oneline master..HEAD | wc -l);
{% hint style="info" %} I would like to reverse the last commit but leave changes I made intact {% endhint %}
# this will leave the committed files as staged
git reset --soft HEAD^
# this will also unstage all changes
git reset HEAD^
{% hint style="info" %}
I would like to create a commit that reverses the changes in a certain commit with hash ${COMMIT_HASH}
{% endhint %}
# use `git log` to find the commit hash of the commit you wish to undo the effects of
git revert ${COMMIT_HASH}
{% hint style="info" %} I would like push all committed changes from my computer to the remote {% endhint %}
git push
{% hint style="info" %} I have modified a commit locally and am unable to push normally to the remote since I rewrote history (WARNING: this will erase any changes others may have made between when the original commit was made, and your current commits) {% endhint %}
git push -f
{% hint style="info" %} I would like to see what files have been staged {% endhint %}
git status
{% hint style="info" %}
- I want to do an interactive rebase (squashing) and I would like to see which commit I should rebase up till
- I want to see what changes have been made by other team members/developers {% endhint %}
# interactive browsing of git commits
git log
# output only the last 5 logs
git log -n 5
{% hint style="info" %} I would like to check out what changes have been made between two commits {% endhint %}
# view file changes from HEAD to ${COMMIT_HASH}
git diff HEAD ${COMMIT_HASH}
{% hint style="info" %} I would like to see who am I committing code as {% endhint %}
git config -l
{% hint style="info" %} I would like to confirm which branch I am on {% endhint %}
git branch
Why
- I would like to see which remotes I am pushing to
git remote -v
Why
- I would like to know which
.gitignore
is causing a file to be ignored without any obvious reason
cd `./path/to`;
git check-ignore -v *;
{% hint style="info" %} I would like to create a new branch based on the current one I'm on {% endhint %}
git checkout $SOURCE_BRANCH_SLUG;
git checkout -b $NEW_BRANCH_SLUG;
{% hint style="info" %}
I would like to delete a local branch so that git branch -a
is less messy
{% endhint %}
git branch -D $BRANCH_SLUG;
{% hint style="info" %} I have deleted a local branch and want to delete the pushed branch too {% endhint %}
git push origin --delete $BRANCH_SLUG;
{% hint style="info" %} Someone else deleted a remote branch and I don't want it locally {% endhint %}
git remote update origin --prune;