Skip to content

Instantly share code, notes, and snippets.

@mephir
Last active August 27, 2017 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mephir/1f734012b0d597b7af6429303c136466 to your computer and use it in GitHub Desktop.
Save mephir/1f734012b0d597b7af6429303c136466 to your computer and use it in GitHub Desktop.
Working with GIT

Basic commands

  • git clone git@github.com:mephir/sf.git - cloning repository (create local repository)- svn equivalent: svn co/svn checkout
  • git fetch remote [[name of a branch 1] [name of a branch 2] ...] - command will download index from remote(fetch all changes) - no svn equicalent
  • git pull remote [[name of a branch 1] [name of a branch 2] ...] - command will download all changes from remote and merge them to our local branch(in our case github, name of the branch(es) is optional argument) branches separated with spaces - no svn equivalent
  • git push origin [[name of a branch 1] [name of a branch 2] ...] - command will send all changes to remote called origin (name of the branch(es) is optional argument, used only when we want send specify branch(es)) - no svn equivalent
  • git commit -a -m "message" - commit changes to local repository(message is mandatory) - svn equivalent: svn ci -m "message"
  • git branch - show all branches which exist in to local repository, the star is showing which branch we are currently using - no svn equivalent
  • git branch foo - creating new branch with name "foo", this command do not switch the branch - no svn equivalent
  • git checkout foo - switching branch for foo - no svn equivalent
  • git merge foo - merging changes from branch foo with currently opened branch - similar to svn merge
  • git reset --hard HEAD - revert all the changes to current revision in our branch
  • git add FILE1 .... - add file(s) to the local repository, files separated with space - svn equivalent: svn add
  • git branch -D foo - remove branch foo from local repository
  • git push origin :foo - remove branch foo from remote(in our case origin), colon is mandatory, in other case we only push the changes - no svn equivalent
  • git status - show changed files - svn equivalent: svn st/svn status
  • git submodule init - initialize git submodules - no svn equivalent
  • git submodule sync - update submodules - no svn equivalent
  • git update-index --assume-unchanged FILE - ignores local changes into tracker file
  • git update-index --no-assume-unchanged FILE - revert effect of above command

More on: http://book.git-scm.com/index.html

Examples(git command line tool)

Cloning repository

If everything went well, we will see something similar

mephir@localhost:~/tmp/git$ git clone git@github.com:mephir/sf.git
Cloning into sf...
remote: Counting objects: 9277, done.
remote: Compressing objects: 100% (3947/3947), done.
remote: Total 9277 (delta 4424), reused 9089 (delta 4247)
Receiving objects: 100% (9277/9277), 25.50 MiB | 8.42 MiB/s, done.
Resolving deltas: 100% (4424/4424), done.
mephir@localhost:~/tmp/git/sf$

in this case we will in subfolder called sf we have 'copy' of repository

Switching between branches

If branch exists on remote:

mephir@localhost:~/tmp/git/sf$ git checkout edit-basic
Branch edit-basic set up to track remote branch edit-basic from origin.
Switched to a new branch 'edit-basic'
mephir@localhost:~/tmp/git/sf$ git branch
  company
* edit-basic
  master
mephir@localhost:~/tmp/git/sf$

Creating and pushing new branch to remote(origin)

mephir@localhost:~/tmp/git/sf$ git branch example-branch
mephir@localhost:~/tmp/git/sf$ git push origin example-branch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:mephir/sf.git
 * [new branch]      example-branch -> example-branch
mephir@localhost:~/tmp/git/sf$

After branch if created we must switch to it, otherwise we will be working in currently opened. git branch new_branch do not switch the branch.

Removing branch

From local repository:

mephir@localhost:~/tmp/git/sf$ git branch -D example-branch
Deleted branch example-branch (was cd6ffb2).
mephir@localhost:~/tmp/git/sf$

From remote(origin):

mephir@localhost:~/tmp/git/sf$ git push origin :example-branch
To git@github.com:mephir/sf.git
 - [deleted]         example-branch
mephir@localhost:~/tmp/git/sf$

Creating branch, adding files, committing and pushing

mephir@localhost:~/tmp/git/sf$ git branch example-branch
mephir@localhost:~/tmp/git/sf$ git checkout example-branch
Switched to branch 'example-branch'
mephir@localhost:~/tmp/git/sf$ touch test1.txt; echo "test file" > test1.txt
mephir@localhost:~/tmp/git/sf$ git status
# On branch example-branch
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test1.txt
nothing added to commit but untracked files present (use "git add" to track)
mephir@localhost:~/tmp/git/sf$ git add test1.txt
mephir@localhost:~/tmp/git/sf$ git commit -a -m "adding test file"
[example-branch 670cb56] adding test file
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test1.txt
mephir@localhost:~/tmp/git/sf$ git push origin example-branch
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 285 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:mephir/sf.git
 * [new branch]      example-branch -> example-branch
mephir@localhost:~/tmp/git/sf$

Merging branches

Extend last example

mephir@localhost:~/tmp/git/sf$ git checkout statistics
Switched to branch 'statistics'
mephir@localhost:~/tmp/git/sf$ git pull origin statistics
From github.com:mephir/sf
 * branch            statistics -> FETCH_HEAD
Already up-to-date.
mephir@localhost:~/tmp/git/sf$ git merge example-branch
Updating cd6ffb2..670cb56
Fast-forward
 test1.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test1.txt
mephir@localhost:~/tmp/git/sf$ git push origin
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:mephir/sf.git
   cd6ffb2..670cb56  statistics -> statistics
mephir@localhost:~/tmp/git/sf$

Removing files

Just remove the file, it will be automatically marked as removed. You need only make a commit.

mephir@localhost:~/tmp/git/sf$ rm -rf test1.txt
mephir@localhost:~/tmp/git/sf$ git status
# On branch statistics
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    test1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
mephir@localhost:~/tmp/git/sf$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment