GIT Cheatsheet

Configuring GitHub

We need to configure both username and password as next step after installation
git config --global "user_name"
git config --global "email_id"

Creating a local repository

git init myCodeFolder

Adds the files in the local repository and stages them for commit

git add .

Committing changes made to the index

git commit -m "some_message"

Connect the remote repository to the local repository. First the remote repository should be created with the same name of the local repository

git remote add origin

Pushing files in local repository to GitHub repository

git push origin master

To unstage a file

git reset HEAD FILE_NAME

Merge development branch with master

These are first work with branch and after finish merge to master
git branch development
git add *
git commit -m "My commit message"
git push -u origin development
Now I will merge all the changes on the development branch into the master
git checkout master
git merge development
git push -u origin master

To remove the commit(before push) and to modify the file

git reset --soft HEAD~1

Setting your branch to exactly match the remote branch can be done in two steps

git fetch origin
git reset --hard origin/master

I you want to save my current branch's state other than master

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

When to use git stash

When I have changes on the working copy, git pull will not work. This is mainly to stash the changes in a dirty working directory away. In other words, when I want to record the current state of the working directory and the index, but want to go back to a clean working directory.git stash

When I want to pull the new changes to my local from the remote.

This command incorporates changes from a remote repository into the current branch. It will pull changes from upstream branch. In its default mode, git pull is shorthand forgit fetchfollowed bygit merge FETCH_HEAD. git pull //this will from the master Pull before before making change in the local. Do not pull after making change, otherwise I will have conflicts

If I want to back to working copy from stashed copy

.git stash popThis will apply stashed changes back to working copy unless there is conflicts. In the case of conflict, they will stay in stash.

To resolve conflict

git staus to show where is the conflicts. Resolve and commit.

There is another better way to merge development

merge master into the development first so that if there are any conflicts, it can be resolved in the development branch itself and master remains clean.
git branch development
git merge master (resolve any merge
conflicts if there are any)
git add *
git commit -m "My commit message"
git push -u origin development
git checkout master
git merge --no-ff development (there won't be any conflicts now)
git push -u origin master

These steps are sometimes to leave master untouched until final stuff. --no-ff development to keep track of who did the merge and when. This is generally useful only when merging development into the master (last step), because I might need to merge master into development (first step) multiple times in my workflow, and creating a commit node for these might not be very useful.

Resetting remote to a certain commit

git reset --hard <commit-hash/commit number>
git push -f origin master

Remove a remote branch

git remote rm origin

Post a Comment