How to generate a patch with edited files? How to zip latest committed changes only

zip archive.zip $(git diff-tree --no-commit-id --name-only -r latest-commit-id)

Reference: https://stackoverflow.com/a/42971972/2229148

sometimes you need all the files that were updated so you can patch an external app which doesnt uses same git project or many other scenarios in which we need a zip or tar of those files. Use the above command to do so

There is a smarter way also, if you want all the changes from last 5 commits, use the command like this:

zip archive.zip $(git diff-tree --no-commit-id --name-only -r HEAD~5 HEAD)

How to push into master or any other branch after removing last n commits in git?

  1. git log to find out the commit you want to revert
  2. git push origin +daee17:master while daee17 is the commit before the wrongly pushed commit.+ was for force push
  3. Finally use git push origin master to sync your local with your git repo

And that’s it.

Below is my log aka example. One of the teammates had committed to the repo (he was not supposed to) after which I committed on server and when I tried to push it on github.com, I received REJECTED error because the remote contained 2 commits which the server did not have.

[ttc@aws www]$ git push origin master
To github.com:Organization/ttc.git
 ! [rejected]          master -> master (fetch first)
error: failed to push some refs to '[email protected]:Organization/ttc.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[ttc@aws www]$ git history
git: 'history' is not a git command. See 'git --help'.
[ttc@aws www]$ git log
commit 8168d26efcd2cc9aa7ddaa47ce3c11d61a134813 (HEAD -> master)
Author: Your Name <[email protected]>
Date:   Mon Jan 21 18:57:55 2019 +0000

    filters css

commit daee17568269bb517870bba5aa75031dbd4f4554 (origin/master)
Author: Your Name <[email protected]>
Date:   Mon Jan 21 06:02:18 2019 +0000

    ravi shopyby resynced api and classes

commit 423c77c82bfb88d0febd5735bae3795740aa5b72
Author: Your Name <[email protected]>
Date:   Mon Jan 21 05:56:24 2019 +0000

    plugin defaulted and collapsible removed

commit bbd39f5a124261af64d015a55d5bd8ba9fdec94a
Author: Your Name <[email protected]>
Date:   Sun Jan 20 13:57:24 2019 +0000

    hirens new design layouts

commit 988524d8d3e90265977355ea738c262c7249a8e0
Author: Your Name <[email protected]>
Date:   Fri Jan 18 15:52:32 2019 +0000

    shubham landing page code improved

https://stackoverflow.com/a/35291514/2229148

How to learn Git in 15 minutes? List of most basic git commands

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance. Every dev has a working copy of the code and full change history on their local machine

Here are some most important and useful GIT commands that will surely help you.

Git Commands:

  • This command sets the author name and email address respectively to be used with your commits.
$ git config –global user.name "[name]" 
$ git config –global user.email "[email address]"
  • This command is used to start new repository.
$ git init [repository name]
  • This command is used to obtain a repository from an existing URL.
$ git clone [url]  
  • This command records or snapshots the file permanently in the version history.
$ git commit -m "[Type in the commit message]"
  • This command lists all the files that have to be committed.
$ git status 
  • This command shows the metadata and content changes of the specified commit.
$ git show [commit]
  • These commands lists, creates and delete branch respectively.
$ git branch  
$ git branch [branch name]  
$ git branch -d [branch name]  
  • This command is used to connect your local repository to the remote server.
$ git remote add [variable name] [Remote Repo Link]  

  • This command sends the committed changes of master branch to your remote repository.
$ git push [variable name] master  
  • This command sends the branch commits to your remote repository.
$ git push [variable name] [branch]
  • This command pushes all branches to your remote repository.
$ git push –all [variable name]
  • This command deletes a branch on your remote repository.
$ git push [variable name] :[branch name]
  • This command fetches and merges changes on the remote server to your working directory
$ git pull [Repository Link]

If you want more commands with examples, please let us know in the comments below.

More Useful commands:

  • Checks if sha is in production.
$ git tag --contains [sha]
  • Number of commits by author.
$ git shortlog -s --author 'Author Name'
  • List of authors and commits to repository sorted alphabetically.
$ git shortlog -s -n
  • See differences in file before committing / compare recently edited file with its last committed state
$ git diff path/to/file.txt
  • Undo local changes to a file.
$ git checkout -- filename
  • Undo/revert last commit
git revert HEAD^
  • Remove last commit from history (WITHOUT keeping changes)
$ git reset --hard HEAD~
  • Remove last commit from history (WITH keeping changes)
$ git reset HEAD~
  • Shows number of lines added or removed from repository by an author since some time in the past.
$ git log --author="Author name" --pretty=tformat: --numstat --since=month | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

referenceS:

https://gist.github.com/davfre/8313299#undoing-previous-actions

https://stackoverflow.com/questions/8903953/how-to-revert-last-commit-and-remove-it-from-history

https://github.com/bpassos/git-commands#committing-files

How to host your web page on Github?

Hello Everyone!

Let’s talk about hosting, once again. I have talked quite a lot about hosting in other articles, but this one is different.

If you want to launch a simple website but don’t want to go through the dull process of setting up yet another hosting package. There is an easier solution. If you just want to launch a simple static website you can use GitHub Pages to host your site for free.

GitHub is not only a great place to store and share your code with others but they also offer free web hosting of your HTML, CSS, and JavaScript projects!

Steps to host your page on GitHub:

  1. Signup for GitHub account: Visit github.com, and you’ll see a signup form on their front page. (If you don’t, congrats, you already have an account!)
  2. Download and install the GitHub desktop app: This is the app that we’ll use to get our code up on GitHub. It’s easy to use so don’t fret; the user interface is easy to learn!
  3. Create a Git repository: Open up the GitHub Desktop app and click the “Create New Repository” button.

    When the “Create a New Repository” dialog window appears, fill in the “Name” text input as:

    [username].github.io

    Name your repository in this manner will tell GitHub to host the files in this project automatically and display them when someone points their browser to:

    https://[username].github.io/

    This will be the web address you will share when your site is ready to publish and go live! Congratulations, you have a new project! But it has no files yet. As a quick way to create the first file, click the “README” link:

  4. Copy your files to the new Repository file: Copy everything in your working folder (/Documents/my-site) into your GitHub Repository folder (/Sites/[username].github.io).
  5. Scroll down the page until you find the Commit area, and click “Commit new file”. Every time you create a new version of a file, you are making a “commit” to record that in the file version history. Each commit has a message to describe the change.
  6. Type YOUR_USERNAME.github.io in the browser, and reload until your webpage appears. Github takes from 1-10 minutes to upload your project changes to the user-facing webpage URL, so you may need to exercise patience. You may also need to “hard reload” (hold shift while reloading), to clear the browser cache.

Whenever you want to make changes to your website, you just need to commit the changes and then push the files up to your GitHub repository. Your changes will be published automatically!

If you want to use your own domain you can do that too. We will talk about it in some other near future article.

Let me know if I missed any steps, if something didn’t work out quite right for you, or if this guide was helpful! Thank you for reading!

Happy Coding.