Thursday, June 5, 2014

GIT: common commands

Synch Commands

Once I have my REPO established this is a set of commands I run several times a day

# make sure you are in a correct directory (.git files should be in the that folder)
# check that you are working on the correct remote repo
git config --get remote.origin.url
# get update of all remote changes
git fetch
# add all files to be managed by git
git add --all
# commit all local changes to LOCAL git repo with a message (change the message as needed)
git commit -m "auto-commit"
# take remote changes at merge them with you local code, your new changes will not be erased
git rebase
# move your local add+commit to remove server
git push
# make sure that you get this message "Your branch is up-to-date with 'origin/XYZ'."
git status

Show differences

$ git diff HEAD FETCH_HEAD

Branch Operations

Switch to an existing branch

$ git checkout develop

Create a new branch named thrift

$ git checkout -b thrift 

Delete an existing branch

$ git branch -D thrift

Resetting - removing local changes

reset hard to previous commit

$ git reset --hard 9a5xxx317

Get local code to the state that is on remote server

Sometimes you may do local changes that you may not like, or your coworker does changes to the same code that are better than yours. In that case you need to fetch remote change, reset your state to what is on remote server and then clean up.

$ git status
$ git fetch

Reset to REMOTE SERVER state

$ git reset --hard FETCH_HEAD
$ git clean -f -d 
$ git status
On branch master
Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working directory clean

$ git rebase
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Working with Submodules

see this topic: