Github - basics and fixing mistakes

Github - basics and fixing mistakes

·

11 min read

1. Setup

First, I will create a new gitHub repository, then I will add the link for remote using Git Extensions(You can do this using sourceTree or GitKraken as well). After, I will commit a simple file test.txt. From any interface you can commit/pull/push by pressing buttons, I'll show you the operations using git Bash since the other method is very suggestive on its own.

2. Checking the current branch

We can check the current branch name on Git via the command:

git branch

This will help us later when we will have more branches.

3. Commit changes

First we need to add the file(because it is a new file, it's not yet on GitHub)

git add test.txt

Then we will commit:

git commit -a -m "first file added"

-a stands for "all" (changes) and -m for message (they are optional, of course, you can see more here

After we commit, we still have the changes only on our local, if we want to have them on remote(which we do), we must push the changes into the remote branch(from GitHub)

git push master --all

-all stand for all changes and master is the name of the branch from GitHub we want to push in. That was pretty simple, but in real life, there are a lot of branches and people are working simultaneously on the same project (maybe even the same file), and here is where the conflicts appear. This is what we'll discuss next.

4. Create two more branches

git checkout -b secondBranch
git checkout -b thirdBranch

Now we have the last version of our "project" (same file) on each branch, but what if we change the file from each branch?

So now, this is how our file looks like:

x = 3
y = 5
z = 10

I'll check by branch and make sure I am on thirdBranch and I will change x value from the file.

git branch

this is how my file looks like:

x = 2
y = 5
z = 10

Now I will commit and push, switch to the secondBranch and do the same, but modify value of y now,so the file on this branch will look like this:

x = 3
y = 2
z = 10
  1. Push, Pull, resolve conflicts

Now, our changes are made only locally, so we need to push the branches. First we need the URL for our repository:

git remote -v

Then we will push both branches:

git push -u  yourUrl thirdBranch
git push -u  yourUrl secondBranch

Replace yourUrl with the Url returned by git remote -v command. Now, paste the link into the browser and go to "branches" section, you'll see both of them added. Also, you'll see 2 messages " thirdBranch had recent pushes 3 minutes ago" and "secondBranch had recent pushes 3 minutes ago", of course not with the same time, but you got the idea. So assuming that secondBranch and thirdBranch are actually two real branches on which 2 different developers worked and changed the same file, the next step would be to open a PR. We'll do that for both of these by pressing the button "Compare and pull request" and selecting to base "Master" (Because this is where our stable file is. Now, the next step would be code review and then merge. We'll go and merge the branches, first secondBranch and then thirdBranch. We actually merged both of them without any conflict (we get the message "This branch has no conflicts with the base branch" for each pull request). Now let's pull the changes and see how our file looks after both changes.

x = 2
y = 2
z = 10

So the file has both of changes (x and y values are updated), but we had no conflicts. Why? Well, because they are at different lines and we didn't changed the same line. Let's checkout at secondBranch and change x value again, then checkout at thirdBranch again. So we'll have 2 different version of file:

x = 10
y = 2
z = 10

and

x = 20
y = 2
z = 10
git checkout secondBranch
git commit -a -m "x changed into 10"
git push -u  yourRepository secondBranch

git checkout thirdBranch
git commit -a -m "x changed into 20"
git push -u  yourRepository thirdBranch

Now, after we create the Pull Requests, when we want to merge, this message shows up at the second PR: This branch has conflicts that must be resolved This is a simple conflict, so we will solve this easier in GitHub with the editor. After choosing the right value, we can merge the second PR as well.

Mistake number 1: You made a commit on your local and you've realized that something is wrong and you must update one or more files to have a good commit

Let's go back in our folder and edit a value and then commit the change( without do a push).

git branch
git checkout
git commit -a -m "value updated"
git checkout

Now we don't have anything after checkout because we already made our commit. So how do we "Delete" our commit?Well, there are 2 options:

1. RESET This option deletes commits (This should be used when you want the commit to fully disappear)

2.REVERT This option preserves history, it doesn't delete commits

So, if we want to get the files from the commit back (not discarding them), we will use:

git reset --soft HEAD~1
git checkout

And you'll see after checkout that our file is there, so it hasn't been deleted. Let's commit it again.

git commit -a -m "value updated"
git checkout

and now we will use hard option:

git reset --hard HEAD~1
git checkout

And you'll see after checkout that our file is not there, so it has been deleted. Take care with this option, the “hard” reset is permanent and all changes saved in the commit will be lost forever. HEAD~1 means last committed change. You can replace 1 with any number fit for your problem.

Mistake number 2: You made a commit on your local, you pushed it to GitHub and you've realized that something is wrong and you must update one or more files to have a good commit

Let's do again the changes, commit, but this time push it to GitHub also.

git branch
git checkout
git commit -a -m "value updated"
git push -u yourRepository  3branch
git checkout

In this case, we can't use reset because this will delete commits and we don't want that. Instead we will use revert because this option preserves commit History. We'll check the last commits (In our case, last 2 commits):

git log --pretty=format:"%h %s" HEAD~2..HEAD

we'll get something like this:

34f5833 value updated
8600af4 x changed into 200

Now we take the commit ID of our last commit (34f5833 ) and we'll revert this(so we will delete our commit).

git revert 34f5833

6. Tips

If you still work on a ticket and you want to checkout to another branch, you can create a stash that will save your changed files, but without any commits, so when you'll get time, you can come back and finish what you were working on.

git stash save
git stash
git stash list

And you can "apply the stash" when you're ready, that means that the changes from the stash will be merged again into the branch (without any commits)

git stash apply stash@{0}

If you find yourself in a situation when you're on the wrong branch (and you already made commits), you can cherry-pick the commits you need to "move" into another branch.

git cherry-pick commitID

7. Interview Questions

  • How can conflict in git resolved?
  • To delete a branch what is the command that is used?
  • What is the function of ‘git diff ’ in git?
  • What is the function of ‘git stash apply’?
  • How can you fix a broken commit?