Skip to content

Instantly share code, notes, and snippets.

@blue-ice
Last active April 7, 2017 00:18
Show Gist options
  • Save blue-ice/4a94fc06859dd9f1b586 to your computer and use it in GitHub Desktop.
Save blue-ice/4a94fc06859dd9f1b586 to your computer and use it in GitHub Desktop.
Workflow containing several common actions for reference

Downloading a remote repository for the first time

  1. cd ~/Documents/git-repositories/
  2. We need to grab the entire repository, selecting the branch needed and the local location that should be used (HTTPS):
  3. git clone -b <branch-name> https://github.com/blue-ice/<repo-name>.git ~/Documents/git-repositories/<repo-name>
  4. YOU MUST cd <repo-name>

Modifications, commits and updates

First steps

  1. cd ~/Documents/git-repositories/<repo-name>
  2. Does git remote -v match the fetch and push links that should be in use?
    • If not, then git remote set-url origin git@github.com:blue-ice/<repo-name>.git
  3. Does git rev-parse --abbrev-ref HEAD match the branch name that should be in use?
    • If not, then git checkout <branch-name>
  4. Now match everything up with git pull

Push local edits

  1. git add -A
  2. git commit -m "<commit-message>"
  3. git push

Undo local adding of files

  1. git reset

Undo last commit before pushing

  1. git reset --soft 'HEAD^'
  2. Re-add files
  3. Re-commit files
  4. git push

Remove last pushed commit

  1. Do not change branches from the one that made the bad commit.
  2. git reset HEAD^ --hard
  3. git push origin -f

Rename last commit message

  1. git commit --amend
  2. git push --force

Check to see if local repo is up to date

  1. git fetch --dry-run

Update local repository (with no commits to push)

  1. git pull

Revert/undelete a file from a previous commit

  1. git checkout <commit-hash> <file-path>

Restore branch to remote state

  1. Checkout the branch that needs to be reset.
  2. git fetch origin
  3. git reset --hard origin/<branch-name>

Renaming a repository

  1. Change repo name on GitHub
  2. Change local repo folder name: mv ~/Documents/git-repositories/<old-repo-name> ~/Documents/git-repositories/<new-repo-name>
  3. Change local pull and push destination git remote set-url origin git@github.com:blue-ice/<new-repo-name>.git
  4. Check to see if the output of git remote -v is the correct address.

Authentication

Set up a repo to use SSH-based authentication

NOTE: Does not work behind firewall.

  1. git remote set-url origin git@github.com:blue-ice/repo-name.git

Set up a repo to use HTTPS-based authentication

  1. git remote set-url origin https://github.com/blue-ice/repo-name.git
  2. (git config --global credential.helper cache)

Increase HTTPS-based credential storage time

  1. This will change the time that Git waits to ask you again for your username and password.
  2. git config credential.helper 'cache --timeout=<time-in-seconds-to-keep-credentials>'

Branching

Create a new branch from current staged files (no commit) and checkout new branch

  1. git checkout -b <new-branch-name>
  2. git add ...
  3. git push --set-upstream origin <new-branch-name>

Renaming a branch

  1. Ensure that the branch to be renamed is not the "default" branch (see repo settings on GitHub). If the branch to be renamed is the "default" branch, then just switch the default branch temporarily.
  2. Check out of the branch to be renamed locally.
  3. Locally, run git branch -m <old-branch-name> <new-branch-name>
  4. git push origin :<old-branch-name>
  5. git push origin <new-branch-name>

Merge a branch with another branch

  1. git checkout <into-branch>
  2. git merge <from-branch> --no-ff
  3. git push

Delete a branch

  1. Check out of branch and make sure it is not the default (found in github repo settings)
  2. git branch -D <branch-name> (local deletion)
  3. git push origin --delete <branch-name> (remote deletion)

Viewing data

View latest commits (all branches)

  1. git log --pretty=oneline

View edits before they are committed

  1. (git add ...)
  2. git diff --cached
  3. (git commit ...)

View current branch in a list of all branches

  1. git branch --list
  2. The current branch will be marked with an asterisk and/or marked in green.

Tagging

Show tags

  1. git tag

Show tags that match a certain pattern

  1. Note: asterisks can be used as wildcards.
  2. git tag -l '<tag-name>'

Add an annotated tag

  1. see http://git-scm.com/book/en/Git-Basics-Tagging for now

Working with .gitignore files

After .gitignore update, get rid of newly ignored files

  1. Commit and push all untracked changes.
  2. Remove everything: git rm -r --cached .
  3. Now add everything again (.gitignore is in effect): git add -A .
  4. git commit ... (Sample message: ".gitignored files removed from repo.")

Create a repository .gitignore that overrides all global .gitignore requests

  1. Create a file named .gitignore inside the root repository folder.
  2. Put the rule !* inside the file.

Forking and pulling

Forking a repo

see Forking a Repo

  1. Click the "Fork" button online on the project to you want to fork.
  2. cd ~/Documents/git-repositories/
  3. We need to grab the entire repository, selecting the branch needed and the local location that should be used: git clone -b <branch-name> https://github.com/blue-ice/<repo-name>.git ~/Documents/git-repositories/<repo-name>
  4. cd <repo-name>
  5. Add a new upstream: git remote add upstream https://github.com/<forked-user>/<forked-repo-name>.git
  6. git push -u origin master

Push to a forked repo (personal)

  1. git add ...
  2. ```git commit ...````
  3. git push -u origin master

Fetch changes from upstream

  1. Make sure that an upstream is set up.
  2. git fetch upstream
  3. git merge upstream/master
  4. git push

Send a pull request

NOTE: The fork of a repo can be deleted once the pull request has been approved.

see https://help.github.com/articles/using-pull-requests

Change personal branch name, but still be able to pull request to the original repo

  1. Click the "Fork" button online on the project to you want to fork.
  2. Go to https://github.com/blue-ice/<cloned-repo-name>/settings and rename the repo.
  3. We need to grab the entire repository, selecting the branch needed and the local location that should be used: git clone -b <branch-name> git@github.com:blue-ice/<renamed-repo-name>.git ~/Documents/git-repositories/<renamed-repo-name>
  4. cd <repo-name>
  5. Add a new upstream: git remote add upstream https://github.com/<forked-user>/<forked-repo-name>.git
  6. git push -u origin master

Merging

Stop a merge

1. This is for use when there is a merge conflict and you want to restore the repository to the unmerged state. 2. ```git reset --merge```

Modify a file and use it in a merge conflict

  1. Edit the file.
  2. git add <file-name>

Use "their" version of the file in a merge conflict

  1. git checkout --theirs README.md
  2. git add README.md

Use "our" version of the file in a merge conflict

  1. git checkout --ours README.md
  2. git add README.md

Add a repository into another repository (keep the old commits)

  1. git clone <into-repo>

  2. git clone <from-repo>

  3. cd <into-repo>

  4. git checkout <into-repo-branch>

  5. git remote add temp-remote ../<from-repo>

  6. git fetch temp-remote

  7. git branch temp-branch temp-remote/<from-repo-clone-branch>

  8. git checkout temp-branch

  9. mkdir <from-repo>

  10. rsync --remove-source-files -av ./ ./<from-repo> --exclude .git

  11. git add -A

  12. git commit -m "Reorganized files from other repository."

  13. git clean -fd

  14. git checkout <into-repo-branch>

  15. git merge temp-branch

  16. git push

  17. git branch -D temp-branch

  18. git remote rm temp-remote

Troubleshooting

Nonexistent branches

  1. If git branch -a shows nonexistent branches...
  2. It most likely needs a git fetch -p to fetch and prune the repository
  3. If that doesn't work, the bad branch has a local copy and/or a remote copy that both need to be deleted (check out of the branch first):
    • Local deletion: git branch -D <branch-name>
    • Remote deletion:git push origin --delete <branch-name>

Commits are not showing up on user page or in commit graph

  1. The <master> (or "default") branch is the basis for user profile updates. See here for information on merging to correct the issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment