Skip to content

Instantly share code, notes, and snippets.

@BrunoMiguens
Last active September 19, 2019 16:11
Show Gist options
  • Save BrunoMiguens/58d0fd18b79d984a9a43e10fde71af77 to your computer and use it in GitHub Desktop.
Save BrunoMiguens/58d0fd18b79d984a9a43e10fde71af77 to your computer and use it in GitHub Desktop.

Git Documentation

This documentation have an objective to demonstrate the git command line tool with some context and use cases.

See this Git Documentation website to understand more how git works or run git --help to get some input.

Index:

Clear Unstaged Changes

When you want to discard all the unstaged changes:

git checkout -- .

Stage Changes on Path

When you want to stage some changes for a specific entire folder or some file:

git add path/to/folder/or/file

Change commit message

Run this commands to change the commit message locally or already pushed to git.

git commit --amend -m "Some text to be changed."

Now you only need to push the rebase, use `-f` to force the push (sometimes is needed).
git push -f

Squash staged changes into the last commit

If you want to squash and insert your staged changes into the last commit without prompts or commit messages, run the following:

git commit --amend --no-edit

Rebasing from Develop

First of all, go to the branch that you want to update.

git checkout your_branch

Now, do a rebase from develop (or a branch that you want to rebase) to your branch.

git rebase develop

If you get some conflicts, solve them and add the changes.

git add .

After you solve the conflits continue the rebase, if appears more conflits solve them and run the previous command continue the rebase.

git rebase --continue

Now you only need to push the rebase, use -f to force the push (sometimes is needed).

git push -f

Revert and Clear Wrong Commits

The following steps are very easy but very powerful, please be careful when you're using this commands.

When you want to clear some commits, that you already pushed or not, you need to find the hash of the commit representing the place that you want to go, something like the small hash 7044cfe or the bigger one 7044cfe5f5f304f12f718e0845055293cd3a18a0.

Now you need to execute the following command.

git reset --hard 7044cfe

I'm using --hard to force the reset without any warning or error. Now you need to push the changes using -f to force.

git push -f

Create or Delete or Push Branch

To create a branch locally is really simple, you only need to execute the command bellow.

git checkout -b your_branch

After doing everything you need, you probably need to push the branch to the remote. origin represents you remote repository name.

git push -u origin your_branch

If you know what you are doing and you want to delete a branch execute this to delete locally.

git branch -d your_branch

To delete remotely you need to execute this one.

// Use `origin` or any remote name you've configured
git push origin --delete branch-name

// Or

// Use `origin` or any remote name you've configured
git push origin :branch-name

Create and Delete a Stash

To create a new stash with your changes execute this command:

git stash

To recover the changes you'll have two possibilities, or you apply the most recent one:

git stash pop

or you choose one from the list of stash (perform this command to check the available stashes git stash list):

git stash apply stash@{0}

To delete a stash you'll need to perform this command:

git stash drop stash@{0}

or if you want to clear all use this one:

git stash clear

Check and delete untracked files

To delete untracked files execute the last command:

// Will check what files will be deleted with the next command
git clean -n

// Delete untracked files!
git clean -f

Delete untracked directories

To delete untracked directories execute the last command:

git clean -f -d

// Or

git clean -fd

Delete ignored files

To delete ignored files execute the last command:

git clean -f -X

// Or

git clean -fX

Rename Branches

To rename a local branch run the following:

// Use this if you're renaming the current branch
git branch -m new-branch-name

// Or

// Use this if you're renaming from another branch
git branch -m old-branch-name new-branch-name

To rename the branch on your remote:

// Use `origin` or any remote name you've configured
git push origin :old-branch-name new-branch-name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment