Languages
[Edit]
EN

Git - how push local changes into new branch?

13 points
Created by:
Kate_C
2952

Using Git sometimes it is necessary to make new branch for locally changed files. In this article we want to show how to do it.

Quick solutuion:

git checkout -b "my_new_brach_with_local_changes"
git add -A
git commit -m "My first commmit to my new branch"
git push --set-upstream origin "my_new_brach_with_local_changes"

1. Branch creation steps

1.1. (Optionally) Check changes and list all branches

Run command:

git status

Output: 

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.txt

nothing added to commit but untracked files present (use "git add" to track)

Note: readme.txt file has been found with available only master branch (git branch -a returned nothing).

1.2. Create new brach:

Run command:

git checkout -b "my_new_brach_with_local_changes"

Output:

Switched to a new branch 'my_new_brach_with_local_changes'

1.3. (Optionally) Check active branch and changes:

Run command:

git status

Output:

On branch my_new_brach_with_local_changes

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.txt

nothing added to commit but untracked files present (use "git add" to track)

Note: active brunch name is my_new_brach_with_local_changes with listed changed/added files.

1.4. Add, commit and push changes:

Run commands:

git add -A
git commit -m "My first commmit to my new branch"

Output: 

[my_new_brach_with_local_changes (root-commit) 5e87717] My first commmit to my new branch
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt

Run command:

git push --set-upstream origin my_new_brach_with_local_changes

 Output:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 240 bytes | 120.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To C:/Users/qurek/Desktop/git test/TestRepository.git/
 * [new branch]      my_new_brach_with_changes -> my_new_brach_with_changes
Branch 'my_new_brach_with_changes' set up to track remote branch 'my_new_brach_with_changes' from 'origin'.

Notes:

  • git add -A added all created/changed¬†files to list to send,
  • git commit -m "My first commmit to my new branch" sent added files to local repository (my_new_brach_with_changes),
  • git push --set-upstream origin my_new_brach_with_changes created remote repository¬†with same name (my_new_brach_with_changes) and sent local repository changes into remote one. --set-upstream set up remote reposotory where in future all local changes will be send - next time just¬†git push is needed to send changes. origin describes default remote repository saved inside .git/config file.

1.5. (Optionally) Check active branch and changes:

Run command:

git branch -a

Output:

  master
* my_new_brach_with_changes
  remotes/origin/my_new_brach_with_local_changes

 Run command:

git status

Output:

On branch my_new_brach_with_changes
Your branch is up to date with 'origin/my_new_brach_with_changes'.

nothing to commit, working tree clean

 Note: to switch branch to master git checkout master should be used.

Hey ūüĎč
Would you like to know what we do?
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.