Usefull git commands:

Checking out remote repositories:

  • git remote -v (this will tell you your remote repository, usually origin)

Checking out remote/local branches:

  • git branch -r (this will tell you your remote branches)
  • git branch -a (this will tell you your local branches)

Checking out upstream repository:

  • Sometimes you forked a github repository to your own account on github, to make sure your master branch is up to date with the upstream branch you need to add the upstream branch with git.
  • git remote add upstream "url git upstream" (check with remote -v to make sure you've got origin and upstream url's)
  • git pull upstream master (pull the latest changes to your local origin branch)
  • git push origin master (push the latest changes to your remote origin branch)
  • Check with "git status" if your local branch is up to date with the remote branch.
  • Redirecting your remote url to another repository
  • git remote set-url origin "url git"
  • Renaming origin/upstream
  • git remote rename "old" "new"

Checking out a new branch:

  • git checkout master (In case you already have branches installed make sure you switch to master branch)
  • git pull (make sure your master branch is up to date with remote master branch)
  • git status (check if there aren't any changes)
  • git checkout -b "new_branchname" (create local new branch)
  • Make your changes in your local branch and check again after that with "git status" (it will tell you what you've changed)
  • git add "name_of_changed_file"
  • git rm "name_of_deleted_file" (if needed)
  • git commit -m "commit message"
  • git push origin "new_branchname" (provide user name and password when asked for, then you will see git pushing the changes to a new remote branch)

Check out existing branches:

  • git branch (will tell you your local branches)
  • git branch -v (will tell you your remote branches)

Switching branch:

  • git checkout "branchname" (will change your working branch to "branchname")

Only use one commit

  • Sometimes after you committed a change to github and find out you need to make extra changes but don't won't to commit a second commit for a pull-request, this can be done with:
  • git commit --amend (this will open an editor and shows you your initial commit message, just close, save and push your changes to github)
  • git push -f origin "branchname"

Squashing git commits:

  • Sometimes it's handy to commit multiple commit messages (for others to track the changes) but before a merge it's better to squash them all together in one commit:
  • git checkout "branch" (checkout your remote branch)
  • git log (checkout the number of commits)
  • git rebase -i HEAD~n (where n is the number of commits you've made)
  • Then in the editor replace pick with squash to each line you want to squash the commit with the previous one. Close the editor and save.
	pick d02f41c78 lua, bump version
	squash 08dcc99bd change copyright according to -v, add test
  • In the next editor comment the lines out you don't want into the commit with "# ". Close the editor and save.
	# This is a combination of 2 commits.
	# This is the 1st commit message:

	lua, bump version, add test (added "add test" to the commit message)

	# This is the commit message #2:

	# change copyright according to -v, add test (added "# " in the beginning of the line so it will be ignored)
  • git push -f origin "branch" (to push your local changes to your remote branch)

Create a pull-request:

  • After you successfully pushed your local changes to your remote fork head over to github, github will show you your local changes and provide you with a link to create the pull request.
  • Check the actual pull request before filing it to see the changes made by you are what you want them to be.

Delete a upstream merged branch:

  • git push origin :"branchname" (this will delete the remote branch)
  • git branch -D "branchname" (this will delete your locale branch)

Checkout a remote branch

  • Pull another remote branch from Github to a local branch
  • git clone "repository"
  • cd "repository"
  • git fetch origin (make sure the origin is setup to track remote repository with git remote -v)
  • git checkout --track origin/"remote_branch"

Preparing for a new pull-request

  1. Make sure you are in master branch: git checkout master

  2. Make sure master branch is on par (local and remote) with upstream: git pull upstream master git push -f origin master

  3. Checkout new branch: git checkout -b newbranch

  4. Make changes in you newbranch and add/remove the needed files: git rm oldfile git add newfile

  5. Commit changes: git commit -m "newfile, update oldfile/newfile"

  6. Push changes to origin: git push origin newbranch

  7. After merge repeat 1 + 2

  8. Clean new branch (remote/local) git push -f origin :newbranch git branch -D newbranch

This should get you back on track