Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helpful Git Commands

Helpful Git Commands

Syncing Your Forked Repository With Original

When the master you have forked from has been updated and your fork is behind.

The cleanest way to do this is from the command line.

  • Add the (original) remote locally. Call it upstream.

    • git remote add upstream https://github.com/original-account/original-repository
  • Fetch the branches of the (original) remote to your upstream.

    • git fetch upstream
  • Confirm that you are on the master branch if not already.

    • git branch to check your current branch, and...
    • git checkout master to switch to master branch if necessary.
  • Rewrite your master so that any commits that aren't already in upstream/master are replayed on top of the other branch.

    • git rebase upstream/master

If you don't want to rewrite the history of your master branch, replace the last command with git merge upstream/master. This may usually be done if other people have cloned your repository. Although, it is generally better to rebase.

Information pulled from Stack Overflow.

Have Git Ignore Further Changes To A File (Not .gitignore)

This is useful when you have a file that needs to be in the repository but you do not need to keep track of the future changes. For example, a default configuration file where you have to input sensitive information specific to your production environment and don't want to risk accidently committing this sensitive information. This will clear up the git status clutter.

Note: This is not the same as adding files to your .gitignore file, as that is for files you do not at all intend to add to your repository.

  • To have Git ignore further changes to a commited file.

    • git update-index --skip-worktree <file>
  • To revert this change to tracking.

    • git update-index --no-skip-worktree <file>
  • To list the file that are marked with skip-worktree.

    • git ls-files -v | grep ^S | awk '{print $2}'

Information pulled from Stack Overflow

Push A Cloned Repository To Your Own GitHub Account

Do this if you have cloned or forked a repository and want to push changes you have made to your GitHub account. Any clones direct or from forked repositories will have the remote information from the original repository. You may notice this when you try to git push and you are denied because you are trying to push to someone else's GitHub account.

  • Save the original remote in case you want to fetch changes to the original repository.
    • Rename the original remote from origin to upstream: git remote rename origin upstream
  • Give your local repository a remote that points to your GitHub repository.
    • If the cloned repository wasn't a fork on your GitHub account, create a repository with the same name and do not initialize it with a README or anything else.

    • Create the remote and point it to the remote repository on GitHub:

      git remote add origin https://github.com/your-account/repository-name

    • Push the local repository to remote repository on GitHub:

      git push origin master

Removing Sensitive Data From Your Commit History

If you have accidently committed any sensitive data and pushed it to GitHub, read here how to fix it ASAP!

Common Command Reference

Commands you will use often.

  • Create a git repository in your directory or re-initialize current.

    • git init
  • Clone a repository from GitHub.

    • git clone https://github.com/account/repository
  • Get a useful overview of the status of your repository.

    • git status
  • Create and checkout a new branch at the same time.

    • git checkout -b new-branch
  • Get a list of local branches.

    • git branch
  • Checkout an existing local branch.

    • git checkout existing-branch
  • Add a new file to your repository. This will also stage it for commit.

    • git add <file>
  • Stage a file for commit. Works the same as git add, for the most part.

    • git stage <file>
  • Unstage a file from committing.

    • git reset HEAD <file>
  • Removing files from your repository.

    • Remove repository but leave file in your working directory: git rm --cached <file>
    • Remove repository and your working directory: git rm <file>
  • Show changes in files since last commit.

    • All tracked files: git diff HEAD
    • Any tracked file, individually: git diff HEAD <file>
    • All staged files: git diff --staged
    • Any staged file, individiully: git diff HEAD <file>
  • Commit your changes for any staged files with a required description message of your commit.

    • git commit -m "Your descriptive commit message"
  • Show information about previous commits.

    • git log
  • Update your local branch with changes from the remote branch.

    • git pull
  • Update your remote branch with changes from your local branch.

    • git push
  • Merge a branch into your currently checked out branch.

    • git merge branch-to-merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.