Skip to content

Instantly share code, notes, and snippets.

@acbharat
Forked from prashanth-sams/Git_commands.md
Last active May 7, 2019 14:17
Show Gist options
  • Save acbharat/ed650650dc0e5d98641b36827d259e72 to your computer and use it in GitHub Desktop.
Save acbharat/ed650650dc0e5d98641b36827d259e72 to your computer and use it in GitHub Desktop.
Git work-around for newbies
Step-1: Whenever you start new changes, keep your master up to date and cut the branch from master
-------------
```
git checkout master
git pull --rebase origin master
git checkout -b Name_to_your_branch
```
Step-2: Do your changes and create the commits before leaving for the day
-------------
```
git status
```
See all files only you intend to add/edit are there
If you find there is something you don't intend to modify get its master copy ( git checkout -- 'file_name' )
you can use gitk command to review your changes and the diff with master
```
git add .
git add ..
git add ../..
git commit -m "Commit message"
```
Step-3: Repeat Step 2 until you finish the task.
-------------
Step-4: Squash all your commits
-------------
```
git rebase -i HEAD~<Number of commits you want to squash>
```
This will open your default editor with all commit list with pick prefix
keep the pick prefix for first commit and replace pick with s for rest commits s is a short handle for squash
save your changes (Esc+Shift+: if VIM is the default editor)
Now it will open all commit messages again in default editor
You can remove unwanted messages and give a meaningful complete commit message for the commit, thats upto you
save your changes (Esc+Shift+: if VIM is the default editor)
Squash will be successful
Step-5: Now you are ready to push, but lets make sure we are again updated with master
-------------
```
git pull --rebase origin master
```
If there are any conflicts it will interrupt the rebase operation and will ask to resolve the conflicts
See the files which has conflicts ( git status )
Resolve the conflicts from files 1 by 1 and add back to rebase operation ( git add 'file name' )
Once all conflicts are resolved and files are added, continue rebasing ( git rebase --continue )
If there are some more conflicts from other commit it will interrupt again, follow the same steps again. ( from bullet 2 )
Once successful, you are ready to push.
Step-6: Push changes to github
-------------
```
git push origin branch_name
```
Step-7: Create a PR for this branch on github

Git clone, add, commit, pull, push

git clone https://.....git
git clone -b <branchname> https://.....git
git status -s
git add README.md | git add lib/* log/* | git add .
git rm README.rdoc
git status -s commit -m "updated readme"
git pull origin master
git push origin master | git push https://.....git master

Git Branch

git branch -a  /** To see the available branches **/
git pull origin <branch-name>  /** to pull from specific branch **/
git checkout <local-branch-name>  /** to switch to another branch **/
git checkout -b <local-branch-name> origin/<remote-branch-name>  /** to create a local branch **/
git checkout config/locales/*.yml  /** revert changes **/
git clone -b <branch-name> https://github.....  /** clone from specific branch **/
git fetch --all  /** Unhide the hidden branch **/

/** to revert **/
git checkout app/scss/precompiles/application/_base.scss
git checkout config/routes.rb
git checkout config/routes.txt

Git delete & pull remote branch

git status -s
git reset --hard
git branch
git branch -D testing
git branch -a
git checkout -b testing
git pull origin testing

Git restore branch in local as in remote

git fetch origin
git reset --hard origin/<branch name>

Git Difference

git differ <filename>
git mergetool -t gvimdiff
git diff <yourfile.txt>

/** after git add **/
git diff --cached <yourfile.txt>

Find your remote URL (remote.origin.url) with

git config -l thanks to Sergio Morstabilini

Your remote URL will be like this : https://{USERNAME}@github.com/{USERNAME}/{REPONAME}.git

Execute this command :

git config remote.origin.url https://{USERNAME}:{PASSWORD}@github.com/{USERNAME}/{REPONAME}.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment