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.
- Clear Unstaged Changes
- Stage Changes on Path
- Change Commit Message
- Squash staged changes into the last commit
- Rebasing from Develop
- Revert and Clear Wrong Commits
- Create or Delete or Push Branch
- Create and Delete a Stash
- Check and delete untracked files
- Delete untracked directories
- Delete ignored files
- Rename Branches
When you want to discard all the unstaged changes:
git checkout -- .
When you want to stage some changes for a specific entire folder or some file:
git add path/to/folder/or/file
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
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
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
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
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
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
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
To delete untracked directories execute the last command:
git clean -f -d
// Or
git clean -fd
To delete ignored files execute the last command:
git clean -f -X
// Or
git clean -fX
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