Use Git

Initialisation

Clone a repository

I would like to make a local copy of code from a public repository I found online.

# for http-based
git clone https://[email protected]/zephinzer/blog.joeir.net

# for ssh-based
git clone ssh://[email protected]/zephinzer/blog.joeir.net

Create a new repository

I would like to initialise a new Git repository on my computer.

git init

Remote management

Add a Git remote

I would like to add a new remote named origin to my repository.

Update the Git remote

I would like to update the URL for my remote named origin in my repository.

Retrieving changes

Fetch changes

Fetching retrieves the changes but does not merge the changes with your local copy.

I would like to get updates from the remote but I don't want to update my code yet.

Merge changes

Merging takes the remote changes that have been fetched from the remote and merges them with your local copy.

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.

Pull changes

Pulling basically does a fetch and merge.

I would like to update my code so that it is the same as the remote's.

Pull with Rebase

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.

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.

Rebase

I would like to pull in changes from another branch that's available locally and place those changes before the changes I've committed.

Saving changes

Stashing

I would like to temporarily store my unstaged changes so that I can pull in the latest updates from the remote.

Staging

I would like to add file(s) that will be 'saved' during a commit.

Commiting

I would like to save my changes to my local Git repository.

Commiting without any changes

  • 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

Modifying changes

Adding a file to a previous commit

I forgot to run git add on a file that should be in the previous commit.

Squash commit

I have made 5 commits and I would like to compress them into a single commit so my Git history is cleaner.

Squashing till origin/master/HEAD

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

Uncommit last commit

I would like to reverse the last commit but leave changes I made intact

Reverting a commit

I would like to create a commit that reverses the changes in a certain commit with hash ${COMMIT_HASH}

Submitting changes

Pushing

I would like push all committed changes from my computer to the remote

Force Pushing

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)

Assessing changes

View all current changes

I would like to see what files have been staged

View commit history

  • 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

View difference between commits

I would like to check out what changes have been made between two commits

Viewing repository information

View the Git configuration

I would like to see who am I committing code as

Check which branch you're on

I would like to confirm which branch I am on

See all remotes

Why

  • I would like to see which remotes I am pushing to

Checking which .gitignore is ignoring a file

Why

  • I would like to know which .gitignore is causing a file to be ignored without any obvious reason

Repository adminstration

Creating a new branch from an existing one

I would like to create a new branch based on the current one I'm on

Deleting a local branch

I would like to delete a local branch so that git branch -a is less messy

Deleting a remote branch

I have deleted a local branch and want to delete the pushed branch too

Deleting local remote branches that have been deleted remotely

Someone else deleted a remote branch and I don't want it locally

Last updated

Was this helpful?