Things I learn while using git and github

After my git nightmare, I decided to make post like Things I Learn.

To create clone

git clone git@github.com:jakir-hayder/mojolicious-plugin-i18n.git

To create a branch and switch to that branch.

git checkout -b new-issue

Simply switch to another branch

git checkout master

You made some changes to your exiting files, to commit those changes

git commit -am 'changes'

You have added multiple files for example a.php, b.jpg, etc. You have to add them before your commit

git add a.php
git add b.jpg

or you can tell git to add all the new files in one line

git add -A

you can omit the commit message

git commit -a --allow-empty-message -m ''

To delete local branch

 
git branch -d deleted_branch

To remove a remote branch (if you know what you are doing!)

git push origin :deleted_branch

to update local branch with a remote branch, we have to add the remote

git remote add origin git://github.com/my/whatever.git

To fetch a branch, you simply need to:

git fetch origin

This will fetch all of the remote branches for you. With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:

git checkout -b test origin/test

then checkout to the branch and pull the code

git checkout branch1
git pull origin branch1

After you make changes you can push your changes

 
git push origin branch1

Some times you need to force push to your branch

git push -f origin branch

To add your forked repo

git remote add upstream git://github.com/whoever/whatever.git

Now you can pull master branch and create a branch and add you code

git pull upstream master
git checkout -b my-branch

After making some changes you can now push to your repo

 
git push origin my-branch

Suppose you used -am to commit and realise some files you do not want to commit.

 
git reset --soft HEAD^ 
git reset HEAD path/to/unwanted_file

now commit again.

You changed something in a file, now you want to revert back to original content

git checkout -- path/to/unwanted_file

Merging two branch is very easy. Just have to how to find the conflicts. First be on the branch where you want to merge another breach

git checkout master
git merge new-feature

When conflicts occurs you will see these <<<<<<<, ======= and >>>>>>> in your code. It will show like this

<<<<<<< HEAD:master
print 'my awesome line';
=======
print 'my awesome second line';
>>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:master

<<<<<<<: Indicates the start of the lines that had a merge conflict.. =======: Indicates the break point used for comparison. Breaks up changes that user has committed (above) to changes coming from merge (below) to visually see the differences. >>>>>>>: Indicates the end of the lines that had a merge conflict.

Now you have to update the code and remove <<<<<<<, ======= and >>>>>>> from your code.

print 'my awesome line';
print 'my awesome second line';

Now you can commit the changes.

change commit message

git commit --amend -m "New commit message"

To reset/revert a specific file to a specific revision

git checkout 8704b4cc48eb711814ecdb8873e32f1a06d7adad file/to/change.html

Suppose you written some code which you do not want to commit. You can use stash to do that.

git stash

you can view all the stashes using list command

git stash list

You can use index also

git stash pop stash@{2} 

To push all branches when adding an existing repo

git push --all
git push --tags

To make git ignore file mode changes (chmod)

git config core.fileMode false

Update remote using following code

git remote set-url origin https://github.com/zakir-hyder/jambura.git

Show git config

git config --list

If .gitignore not working, do this – First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"

To reset/revert a specific file to a specific revision

git checkout abcde file/to/restore

Clone a specific Git branch

git clone -b <branch> <remote_repo>
git clone -b nx_util-dev https://github.com/nbs-system/naxsi.git

About Zakir Hyder

This entry was written by .

15. June 2013 by Zakir Hyder
Categories: git | Tags: , , , , , , , , , , , , , , , , , , , , | Comments

Comments

  1. […] After my git nightmare, I decided to make post like Things I Learn. To create clone To create a branch and switch to that branch. Simply switch to another branch You made some changes to your exiting files, to commit…  […]