Skip to content

Instantly share code, notes, and snippets.

@aguilarcarlos
Last active May 20, 2020 17:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aguilarcarlos/dc8ff22845991c860e67 to your computer and use it in GitHub Desktop.
Save aguilarcarlos/dc8ff22845991c860e67 to your computer and use it in GitHub Desktop.
Fixing untracked files

Some tips for Git to Save your Life 👍

Rest branch to spefic commit or last commit

Firstly you need select th commit desired or simply reset the HEAD in current branch, (can check if you are in current branch using git branch.

git reset HEAD^ --hard // Or git reset +[commit]^ --hard

After that we need to force push doing this way:

git push -u origin [branch] -f

Squash commits, preparing a pull request with a single commit

Sometimes, we have a branch with many commits of a possible new feature or fix, and maybe there's commits that we don't want that commits are shown in main or dev branch. For this reason, I've found through searching in internet tips and I reached the best solution for this:

1.- First you have your feature or fix branch with some changes commited, for seeing a cleaner landscape about this we have to run this command:

git log --oneline [origin/[branch] or [branch]]..[origin/[branch] or [branch]] // dots (..) are important
73bbc09 Feature implemented
f33b240 removing comments

As we can see in previous output, we only have two commits ahead between branches (supossing that branch left is previos command is dev and right branch is new_feature), that ahead changes are in branch of our feature implementation. Now here we go with the process:

git checkout feature/colors // Is necessary to be in branch of the new feature
git rebase -i [origin/[branch] or [branch] // You can do it locally or remotely

This will display the editor with a message similar to these contents:

pick 73bbc09 Feature implemented
pick f33b240 removing comments

# Rebase e54a9a9..73bbc09 onto e54a9a9
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

Now we need to change pick with squash in commits that you want to merge in the pick commit, in this case, we will merge all in the first commit, some like this:

pick 73bbc09 Feature implemented
squash f33b240 removing comments

# Rebase e54a9a9..73bbc09 onto e54a9a9
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

After these, git will display to you a new editor window to create your own description or leave the message with same text:

# This is a combination of 2 commits.
# The first commit's message is:

Feature implemented

# This is the 2nd commit message:

removing comments

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# ...

Now if we can to run the same command of the beginning, we will see some like this:

git log --oneline [origin/[branch] or [branch]]..[origin/[branch] or [branch]] // dots (..) are important
e020524 <the first line of your modified commit message>

Finally, we need to push our branch of the new feature with FORCE:

git push -u origin [new_feature] -f

And we will have a single commit. :)

Adding sublime text as default editor on Windows

  • Sublime Text 2
git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe' -w"
  • Sublime Text 3
git config  --global core.editor "'C:/Program Files/Sublime Text 3/sublime_text.exe' -w"

Ignored Files Pushed

Even if you haven't tracked the files so far, git seems to be able to "know" about them even after you add them to .gitignore. Run the following commands from the top folder of your git repo. Note: Commit your current changes, or you will lose them.

git rm -r --cached . 
git add --all 
git commit -m "Fixed untracked files" 
git push [origin] [branch]

Correcting the last commit

You made a mistake in last commit, you can fixed doing this:

  • Step 1: This command will open your default editor, and brings the last commit to edit
git commit --amend -a 
  • Step 2: Once you have edited commit, then you can run this:
git push --force [origin] [branch]
@mustafiz012
Copy link

it helps me....thanks

@aguilarcarlos
Copy link
Author

It's good know about it.

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