7 Noob Git Tips
#Git_Noob_Tip is a title for a set of beginner-friendly git tips that I tweet every week. At the time this post is being published, I have tweeted 7 of those, and I'm going to compile them together into this post. I'll also add context and more details.

Tip 1. Delete Remote Branch
Deleting a local branch is rather easy:
git branch -d <branch>
The -d flag checks if the branch is merged and then deletes it. To delete a local branch no matter what, we have to use the big D:
git branch -D <branch>
Then, it comes to deleting a remote branch from git's CLI:
git push origin --delete <branch>
Of course, one can delete a remote branch using the UI application that manages the remote repo, e.g. GitHub or GitLab. But this is handier.
Also, to check what remote branches there are, you can list them using:
git branch --remote
Tip 2. Rename or Move a File
So, once I went to work and my colleague told me: "We should tell John to change his script to do that." Of course, the script was written by me. What did John do? He moved the script to a subdirectory and changed two lines of it.
Git usually gets confused when you rename/move a file and change its content at the same time. Git would think the old file was deleted and a new file was created. All of the version history is simply lost.
To prevent such things from happening, one should rename or move, using git:
git mv <src> <dest>
Instead of doing:
mv <src> <dest>
Tip 3. Rebase When Pulling Master
When pushing, your local branch must be ahead of the remote branch, otherwise, the push is rejected. This is called the "fast-forward rule". In the case of a feature branch, one can force-push, but one should never force-push to master.
So, always keep your local master ahead.
That is done by rebasing. When you want to update your master branch with the remote repo, and especially when you have local changes, do a rebase pull:
git pull --rebase origin master
Otherwise, a merge commit might be created on your local repo and you can never push to master again.
Tip 4. Git Default Branch
A bit of context: Until some 2 years ago, the default branch of every git repository was called "master". It was a synonym for "the default branch". Then there was an initiative to change this because it was offensive to some people. GitHub was the first one to react and changed the default branch name to "main". On git, the default branch name stayed "master", but an option was added to change it.
So, until recently, if you initialize a git repo locally, the default branch name would be master:
git init
This behavior was changed in the last version, and now it actively asks you to "set" a default branch name before it allows you to init. This is done as follows:
git config --global init.defaultBranch <name>
Some popular names are the following:
- master: the original name
- main: the one popularized by GitHub
- trunk: the name used by the older version control tool, SVN
- development: used in the repos with a certain workflow
Tip 5. Stash Message
Git stash is a place to store your unfinished work to do things like changing the branch or pulling the latest changes. Then one can pop the changes and continue working.
Although the stash is designed not to become too large, it might. I usually end up having 20 different stashed changes and not knowing what is what and finally dropping them all.
This can be avoided by adding a message to your stash:
git stash push -m <message>
Then, when you want to look at your stash, you also see the messages:
git stash list
Tip 6. Auto-Stash
This one is a child of tips 3 and 5. First of all, if we want to rebase every time we pull, why not make it the default? Also, if we want to stash our uncommited changes every time we pull/rebase, why not make it automated? That's what this tip is about:
git config --global pull.rebase true
git config --global rebase.autoStash true
By setting these config values, next time you have some changes in your local, you can still do a pull. The changes will be stashed, a rebase will happen on your branch, and the changes will be poped from the stash.
Tip 7. Push Default Branch
Let's say you created a new branch locally, named my-fantastic-branch, and you want to push it to the remote repo. The first time you're pushing, you need to specify the name again and instruct git that this is your "upstream" branch from now on so that git creates the branch on the remote repo:
git push --set-upstream origin my-fantastic-branch
This is usually tedious and prevents people from using descriptive branch names. To avoid this and set the remote branch to have the same name as the local one by default:
git config --global push.default current
Next time on push, there is no need to repeat yourself.


