Skip to content

Instantly share code, notes, and snippets.

@maestrow
Last active March 31, 2018 12:41
Show Gist options
  • Save maestrow/c1a54d65769507b8fca3 to your computer and use it in GitHub Desktop.
Save maestrow/c1a54d65769507b8fca3 to your computer and use it in GitHub Desktop.
git-workflow
git init
git remote add origin https://github.com/try-git/try_git.git
git status
git add --all
git commit -am "initial commit"
git push -u origin master

Merge Two Repositories

http://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories

Q: Consider the following scenario: I have developed small experimental project A in its own git repo. It has now matured, and I'd like A to be part of larger project B, which has its own big repository. I'd now like to add A as a subdirectory of B.

How do I merge A into B, without losing history on any side?

A:

git remote add -f Bproject /path/to/B
git merge -s ours --no-commit Bproject/master
git read-tree --prefix=dir-B/ -u Bproject/master
git commit -m "Merge B project as our subdirectory"
git pull -s subtree Bproject master

Change the current branch to master

http://stackoverflow.com/questions/2763006/change-the-current-branch-to-master-in-git

Scenario: I have a repository in git. I made a branch, then did some changes both to the master and to the branch. Then, tens of commits later, I realized the branch is in much better state than the master, so I want the branch to "become" the master and disregard the changes on master.

Answer:

git checkout better_branch
git merge --strategy=ours master    # keep the content of this branch, but record a merge
git checkout master
git merge better_branch             # fast-forward master up to the merge

If you want your history to be a little clearer, I'd recommend adding some information to the merge commit message to make it clear what you've done. Change the second line to:

git merge --strategy=ours --no-commit master
git commit          # add information to the template merge message

Track remote branch

To view all remote branches: $ git branch -r git branch - list local branches with selected current.

First of all you have to fetch the remote repository:

git fetch [remoteName] Than you can create the new branch and set it up to track the remote branch you want: git checkout -b newLocalBranch remoteName/remoteBranch or git branch --track newLocalBranch remoteName/remoteBranch

Email & Author

git config --global user.name "Chris"
git config --global user.email "[email protected]"
git config user.name "Chris"
git config user.email "[email protected]"
git config --global alias.workprofile 'config user.email "[email protected]"'
git config --global alias.workprofile '!git config user.name "Chris Kelvin";  git config user.email "[email protected]"'
git workprofile

http://www.codeography.com/2011/08/05/project-specific-git-author.html

Try Git in 15 minutes

https://try.github.io/levels/1/challenges/1

Other references:

Init, add, reset, commit, status

$ git init

$ git status

  • Staging Area: A place where we can group files together before we "commit" them to Git.
  • Staged - files are ready to be commited.
  • Unstaged - files with changes that have not been prepared to be commited.
  • Untracked - files aren't tracked by git yet (usualy newly created ones).
  • Deleted - file has been deleted and is waiting to be revomed from git.

add feature allows you to separate commits by edits:

$ git add files_under_one_topic
$ git commit -m "this is about one thing"
$ git add files_under_second_topic
$ git commit -m "this is about other thing"

git reset <file> to remove file from staging area

$ git log --summary

Add remote

Git doesn't care what you name your remotes, but it's typical to name your main one origin.

$ git remote add origin https://github.com/try-git/try_git.git

Push

Push our local changes to our origin repo (on GitHub).

$ git push -u origin master

The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push

Pull, diff & stash

$ git pull origin master

$ git diff HEAD - let's take a look at what is different from our last commit. By default HEAD points to your most recent commit.

$ git diff --staged

$ git reset octofamily/octodog.txt - unstage file(s).

$ git checkout -- octocat.txt - get rid of all the changes since the last commit for octocat.txt.

Sometimes when you go to pull you may have changes you don't want to commit just yet. One option you have, other than commiting, is to stash the changes. Use the command git stash to stash your changes, and git stash apply to re-apply your changes after your pull.

Branching

$ git branch clean_up $ git checkout clean_up

or use one command for that: git checkout -b clean_up.

Removing files

git rm '*.txt' - remove the actual files from disk and also stage the removal of the files.

If you happen to delete a file without using git rm you'll find that you still have to git rm the deleted files from the working tree. You can save this step by using the -a option on git commit, which auto removes deleted files with the commit.

$ git commit -am "Delete stuff"

$ git checkout master - Switch to branch 'master'.

$ git merge clean_up

$ git branch -d clean_up - delete a branch, since you're done with the clean_up you don't need it anymore.

$ git push

Hooks

You can do some really cool things with hooks when you push. For example, you can upload directly to a webserver whenever you push to your master remote instead of having to upload your site with an ftp client.

Customizing Git - Git Hooks

Pull request

Using pull requests

A pull request allows the boss of the project to look through your changes and make comments before deciding to merge in the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment