Skip to content

Instantly share code, notes, and snippets.

@murilomothsin
Created January 29, 2018 11:01
Show Gist options
  • Save murilomothsin/c3b7996e0ffcc79bc5947181e0074401 to your computer and use it in GitHub Desktop.
Save murilomothsin/c3b7996e0ffcc79bc5947181e0074401 to your computer and use it in GitHub Desktop.
Some useful Git commands I use in a regular basis

Basic Commands

Updating your project with new information from Github

This command will update the references/branches locally on your machine and notify about deleted branches from origin

git fetch -p

Creating a new working branch

Before creating a branch, update your project, after that, go to master branch and then run the command:

git checkout -b [your_new_branch]

Viewing the list of modified/created/deleted files on your current work branch

git status

Viewing the differences/modifications you make on your branch

git diff # you can specify a file path here to see just that file diff

Creating a new commit with the changes you made

First, you need to stage the files that you want git add [path_of_the_file] # of --all to add all files modified

Them you write the name/message of this new commit

git commit -m "first commit"

Pull updates from Origin for your branch

git pull origin [name_of_your_branch]

Change from your current branch to another

git checkout [the_other_branch]

Going back to previous branch

git checkout -

Renaming your current branch

git branch -m [old_name] [new_name]

Intermediary Commands

Putting aside your modifications to use another time

We call this STASH. You can "save" your current modifications for another time. For example, let's say you're modifying inde.html, but need to change branches and do some other fixes on another files. In this case you can "save" your modifications without needing to commit them

git stash -> will save the modifications

git stash apply -> will apply the modifications

git stash clear -> clean the modifications

Rollback the current modifications but KEEP INFORMATION

git reset --soft [commit sha1]

If you want to delete all things modified and don't wanna keep them, just change --soft for --hard

Update your branch based on another branch using REBASE

This command will get the informations from [the_branch_with_updates] and insert

git rebase [the_branch_with_updates]

Update your branch based on another branch using MERGE

git merge [the_branch_with_updates]

Search for an specific file on Git History (see this)

git log --all --full-history -- **/the_file.html

If you want to show just the modifications for this file in a specific commit

git show <SHA of the commit> -- **/the_file.html

Advanced Commands

Accessing commit history and making modifications to them

"10" here will grab the current commit and the next 10 previous commits

git rebase -i HEAD~10

With this command you can change messages, "merge" commits into each other delete, squash and some other commands (by default will open in VI terminal)

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