I Am Losing My …Git

B Tram Vu
5 min readFeb 11, 2021

--

“Uh… what happened? I swear everything was functioning when I pushed it.” Sound familiar? Welcome to the Git-Happened-Club, where you had to explain to your peers that you didn’t do anything to git, or how you spent hours to work on your code, pushed it, merged it, and now right after the git pull, nothing worked anymore.

Well, to get out of this club, first, you need to know what Git is. Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. You can commit new changes, make new branches, merge and compare past versions, and manage the content of the files as well as the true relationships between files and directories, versions, tags and commits.

Most of us are familiar with the basic commands:

git add. — to add changes

git commit -m “message of commitments” — to commit the changes to main

git push origin main — to push the code to your repo

We were okay and comfortable there for a while, until you have to work with the team, and the dynamic of creating branches and merging and pulling just made you want to pull out your hair. Here are a few commands that may be useful to save that beautiful head of hairs and may save your team’s project. First, you need to work on a new branch to ensure that the features works before messing with the main code that everyone is working on

git branch branchname — to create a branch

git checkout branchname — to switch between branches

git branch — list — To view see the list of branches the project has

You worked your magic, and everything worked, you were ready to show your team what you got, yet now your friend, git push origin main command, won’t let you do that. The reason most likely is because someone put a protection rule on this main branch to protect it from Mister-Know-It-All who thought that his codes were working perfectly fine, and pushed it to main only to find out that they were not and messed up with everyone else’s codes. (No, not you of course, I am sure your codes worked perfectly fine, otherwise you will not try to push it to the main branch). In this case, you will want to push to your the branch you were working on first

git push origin branch name — to push the code to your branch.

Then go ahead and ask for a merge request, get your teammate to approve, and voila! You are done!

Only if life was that easy huh? This is where the conflicts start to get worse, someone pushed, someone else approved, and you were in your zone and now your branch is behind.

The easiest way to fix this problem is to create a new branch for each task. It is not the best way but surely the easiest, so even if there are conflicts, they are easier to solve than pushing and merging a big block of codes.

For smaller project, after pushing your code, you would want to do a

git checkout main — to get back to your main branch, then

git checkout -b branchname — to create a new branch that comes out of main, and jump right to it to work on.

Once you are done with that, do the add-commit-push virtual.

Here for bigger projects, you would want to know what branch you are in, and create a sub branch out of that branch.

git branch to see what branch you are on

git branch -a to show remote and local branches

If you are on main: git pull origin main to make sure your main branch is up to date

If you are on another branch that is different than main: git pull origin main (yes, in that branch) to make sure that your branch is up to date, so when you do a merge pull request, the errors “your branch is behind main” won’t happen.

I found the best way to fix these conflicts is communication. Keep your teammates in the loop of what you are doing, and how you are going to do it. What are you changing, what file, add a clear commit message, and try not to push broken code into main, keep working on your branch until things are pretty and smooth, then it is ready to merge. If you need help getting your code to work, pair programming with your teammates to work on that same branch without creating problems.

Well, with all that said and done, of course there will be even more problems. Someone has to go to work, someone isn’t very communicative, or Mister-Know-It-All won’t talk to you because he didn’t think you know jack-git! Here are some tricks :

to see a list of everything you have done in git in all branches: git reflog

If you has been working on your code, and realize that you never checkout to a new branch, you are on main, and you know the protection is there, don’t cry just yet, do this:

git checkout -b newbranchname — to create a new branch from the current state of your code, just like that and you have your changes in the new branch now. Go on with your work.

to move back to one commit earlier: git reset — hard HEAD^

to push the commit forcefully to remote: git push -f

to fix your commit message: git commit — amend

to fetch the remote branches git fetch

Also check out gitkraken https://www.gitkraken.com/, if you are a visual person, you will really appreciate this. It shows you the map of your branches, who did what, commit where and what.

Oh, did I mention it is free? You got a Git, you got a Git, you got a Git! Go Git it! YOU GOT THIS!

--

--