Git Commands

Quick Start

git clone [URL]

git status

git add --all

git commit -m "commit message"

git push origin master

git pull origin master

git remote set-url origin <repo-name>

git remote -v

When Pulling from New Remote Branch

  1. git branch -f remote_branch_name origin/remote_branch_name
  2. git checkout remote_branch_name

  3. If you get Not a valid object name: 'origin/remote_branch_name', do 'git fetch origin' first

For Coding Challenges

  1. Fork repo
  2. git clone [URL]

  3. git pull upstream master

Pointing Existing Local Repo to New Remote Repo (Accidentally Clone Wrong Fork)

  1. git remote set-url origin URL_TO_GITHUB_REPO

Alternatively:

  1. git remote rename origin upstream
  2. git remote add origin URL_TO_GITHUB_REPO
  3. git push origin master

Now you can work with it just like any other github repo. To pull in patches from upstream, simply run

git pull upstream master && git push origin master

Keeping GitHub Forks in Sync

https://github.com/LambdaSchool/CS-Wiki/wiki/Keeping-GitHub-Forks-in-Sync

error: failed to push some refs to

If the GitHub repo has seen new commits pushed to it, while you were working locally, I would advise using:

git pull --rebase
git push

// The full syntax is:

git pull --rebase origin master
git push origin master

That way, you would replay (the --rebase part) your local commits on top of the newly updated origin/master

E325: ATTENTION Found a swap file by the name “.git/.COMMIT_EDITMSG.swp”

git rm .git/.COMMIT_EDITMSG.swp

Create Repositories

git init [project-name]— Create a new local repository

git clone [url]— Downloads a project and its entire version history

Make Changes

git status— Lists all new or modified files to be committed

git diff— Shows file differences not yet staged

git add .— Add all current changes to the staging area, ready for the next commit

git add -p [filename]— Add some changes in[filename]to the staging area, ready for the next commit

git commit -a— Commit all local changes in tracked files to the staging area, ready for the next commit

git commit– Commit previously staged changes.

git commit [filename] -m 'My commit message'– Commits changes to the[filename]file.

  • The -m flag expects to be followed by a commit message surrounded by quotes.
  • Single quotes if you want to use quotation marks in your message, or double quotes if you want to use apostrophes in your message.
  • If you want to commit everything in a sub directory, git commit sub-dir -m 'message'
  • If you want to commit everything in the repo without staging it first (dangerous), git commit -a -m 'message'

git commit --amend– Change the last commit. Don‘t amend published commits!!

Review History

git log --oneline– Show all commits, starting with newest. The oneline flag cleans the entries up a bit to display each commit on one line.

git log -p <file-name>– Show changes over time for a specific file.

git blame <file-name>– Display who changed what in<file-name>.

git blame -L 10,20 <file-name>– Will display who edited those specific lines of<file-name>.

Branching Out

git checkout <branch-name>– Allows you to switch between git branches, adding the-bflag creates the branch you are switching to if it doesn't already exist. You can also specify specific commit hashes instead of branch names.

If you want to checkout a specific file you cangit checkout <branch-name-or-commit><file-path>which is powerful as it allows you to selectively merge in files or rewind changes.

git branchwithout any parameters will list all current branches. You can also use it to create a new branch withgit branch <new-branch-name>which will create a branch based on your current HEAD but not check it out (which is whygit checkout -b <new-branch-name>is so useful).

git checkout --track <remote-name>/<branch-name>– Create a new tracking branch based on a remote branch.

git branch -d <branch-name>– Delete a local branch.

git push origin --delete <branch-name>– Delete a remote branch.

git tag <tag-name>– Mark the current commit with a tag.

Phoning Home

As evidenced by the popularity of remote repo hosting services (hello GitHub!), One of the most powerful features with git is the ability to track remote repositories.

git remote -v– List all currently configured remotes. The-vflag stands for_verbose_and allows you to see more information about the remote repos, like the url.

git remote show <remote-name>– Show more information about a specific remote.

git remote add <remote><url>– Add new remote repository, named<remote>and with a source url of<url>.

git fetch <remote>– Download all changes from<remote>, but don‘t integrate into HEAD.

git fetch --dry-run -v---dry-runShow what would be done, without making any changes.-vBe verbose.

git pull <remote><branch-name>– Download changes and directly merge/integrate.

git push <remote><branch-name>– Publish local changes to the remote repo.

git branch -dr <remote-name>/<branch-name>– Delete a branch on the remote repo.

git push --tags– Publish any tags you've added.

Making Synergy!

Dealing with differences and changes between repos is the real power behind git.

git merge <branch-name>- Merge<branch-name>into your current HEAD.

git rebase <branch-name>– Rebase your current HEAD onto<branch-name>.Don‘t rebase published commits!!

git rebase --abort– Aborts a rebase in progress.

git rebase --continue– A rebase will pause when it detects a conflict and will wait for you to_resolve_it/them by choosing which side of the merge to keep. When you've resolved the conflicts, this command continues the rebase.

git mergetool– Use your configured merge tool to solve conflicts.

git add <resolved-file>– Use your editor to manually solve conflicts and (after resolving) mark file as resolved.

git rm <resolved-file>

CNTRL/CMD-Z!

We all make mistakes.

git reset --hard HEAD– Discard all local changes in your working directory.

git checkout HEAD <file-name>– Discard local changes in a specific file.

git revert <commit>– Revert a commit (by producing a new commit with contrary changes).

git reset --hard <commit>– Reset your HEAD pointer to a previous commit and discard all changes since then.

git reset <commit>– Reset your HEAD pointer to a previous commit and preserve all changes as unstaged changes.

git reset --keep <commit>– Reset your HEAD pointer to a previous commit and preserve uncommitted local changes.

Tips and Tricks

`git checkout --git ls-files -m```– Reset modified files in a directory

`rm -rfgit ls-files --other --exclude-standard```– Remove untracked files (use caution)

git log -G "<search-string>" --pretty=format:"%C(yellow)%h %Creset%s %Cgreen(%cr) %Cblue[%cn - %ce]" --decorate– Search commits for a string or code

The following snippet is handy if you're dealing with a monolithic repo with many branches and submodules:
git checkout <branch-name>&& rm -rf `git ls-files --other --exclude-standard` && git submodule update --init --recursive Reset-checkout.. checks out branch, removes untracked files, and re-inits submodules

git help <command>— Get help with a specific git command.

results matching ""

    No results matching ""